| Author |
Package does not exist / Cannot find symbol message
|
Crace Selvage
Greenhorn
Joined: Jan 14, 2005
Posts: 9
|
|
Hi all, the search function on here is down, so I apologize for posting what I'm sure is a repeat... I'm working on the Ch3 MVC tutorial in Headfirst Servlets & JSP. I have created my folder structure in Tomcat as this... webapps > Beer > WEB-INF > classes > com > example. The example directory stores the model and web directories. I have two java files in this project. My first file, BeerExpert.java compiles fine. When I try compiling the second file, BeerSelect.java, I receive the following output... webapps\Beer\WEB-INF\classes\com\example\web>javac BeerSelect.java BeerSelect.java:3: cannot find symbol symbol : class model location: package com.example import com.example.model; ^ BeerSelect.java:4: package javax.servlet does not exist import javax.servlet.*; ^ BeerSelect.java:5: package javax.servlet.http does not exist import javax.servlet.http.*; ^ BeerSelect.java:10: cannot find symbol symbol : class HttpServletRequest location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ^ BeerSelect.java:10: cannot find symbol symbol : class HttpServletResponse location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ^ BeerSelect.java:10: cannot find symbol symbol : class ServletException location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ I am running j2se 1.6 and my classpath seems to be fine, as I am able to compile in command line. But I can't figure why my imports aren't working. Do I need to install j2ee, as well? And if so, what do I need to add to my classpath? Thanks for your help, Crace [ January 07, 2007: Message edited by: Ben Souther ]
|
 |
gaurav abbi
Ranch Hand
Joined: Jan 05, 2007
Posts: 108
|
|
hi Crace, the problem seems to be because you have not included servlet-api.jar in your CLASSPATH. this will be present in your TOMCAT in the following path common\lib\servlet-api.jar. add the absolute path for this jar in your CLASSPATH variable and try compiling it..
|
thanks,<br />gaurav abbi
|
 |
Crace Selvage
Greenhorn
Joined: Jan 14, 2005
Posts: 9
|
|
Thanks for the quick response. My CLASSPATH is now set to this... .;JAVA_HOME\bin;C:\Program Files\Apache Tomcat\apache-tomcat-5.5.17\common\lib\servlet-api.jar; However, I am getting the same messages I stated earlier. I have also tried adding just the path (the path up to the lib folder). Anyone see what I am doing wrong? Thanks, Crace
|
 |
gaurav abbi
Ranch Hand
Joined: Jan 05, 2007
Posts: 108
|
|
hi crace, can you send me the error message you are getting now.
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
If you want to import an entire class, you need to use the wild card symbol. Change your import directive to:
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Crace Selvage
Greenhorn
Joined: Jan 14, 2005
Posts: 9
|
|
Ben, thanks for pointing that out. I have modified the import statement to import com.example.model.*; However, it is still not compiling. Here is the output I receive... web>javac BeerSelect.java BeerSelect.java:3: package com.example.model does not exist import com.example.model.*; ^ BeerSelect.java:4: package javax.servlet does not exist import javax.servlet.*; ^ BeerSelect.java:5: package javax.servlet.http does not exist import javax.servlet.http.*; ^ BeerSelect.java:10: cannot find symbol symbol : class HttpServletRequest location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ^ BeerSelect.java:10: cannot find symbol symbol : class HttpServletResponse location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ ^ BeerSelect.java:10: cannot find symbol symbol : class ServletException location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ Thanks for looking, Crace
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Your classpath is not set up properly. See: http://faq.javaranch.com/view?CompilingServlets I'm going to move this thread to our Java In General (Beginner) forum where you will find lots of help for classpath and javac issues. Good-Luck -Ben
|
 |
John Adkins
Greenhorn
Joined: Jan 23, 2007
Posts: 4
|
|
Hi Crace... This is my program following, check whether you have followed the way i did... This is an example in Chapter3 of HFSJ the directory structure is as follows... C:\Tomcat\MyProject\beerV1\src\com\example\web\BeerSelect.java C:\Tomcat\MyProject\beerV1\src\com\example\model\BeerExpert.java C:\Tomcat\MyProject\beerV1\classes\com\example\web\ C:\Tomcat\MyProject\beerV1\classes\com\example\model\BeerExpert.class and my tomcat directory is... C:\Tomcat\$CATALINA_HOME\apache-tomcat-5.5.20\common\lib\servlet-api.jar My BeerSelect.java code is... package com.example.web; import com.example.model.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class BeerSelect extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice<br>"); String c = request.getParameter("color"); BeerExpert be = new BeerExpert(); List result = be.getBrands(c); Iterator it = result.iterator(); while(it.hasNext()) { out.println("<br>try: " + it.next()); } } } My BeerExpert.java code is... package com.example.model; import java.util.*; public class BeerExpert { public List getBrands(String color) { List brands = new ArrayList(); if (color.equals("amber")) { brands.add("Jack Amber"); brands.add("Red Moose"); } else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return(brands); } } I have set my CLASSPATH in (C:\AUTOEXEC.bat) set as... set PATH=C:\Program Files\Java\jdk1.5.0_10\bin;C:\WINDOWS\java;C:\WINDOWS\java\classes;C:\WINDOWS\java\trustlib;%PATH% set JAVA_HOME=C:\Program Files\Java\jdk1.5.0_10 Also in Environment Variables under User Variables, I have set my CLASSPATH as...(right click on my computer, select properties, goto advanced tab and select environment variables) CLASSPATH C:\Program Files\Java\jdk1.5.0_10;C:\WINDOWS\java;C:\WINDOWS\java\classes;C:\WINDOWS\java\trustlib; Also in Environment Variables under System Variables, I have appended the following to the CLASSPATH as... CLASSPATH C:\Program Files\Java\jdk1.5.0_10\bin; compile the BeerSelect.java program as... C:\Tomcat\MyProjects\beerV1>javac -classpath classes;C:\Tomcat\$CATALINA_HOME\apache-tom cat-5.5.20\common\lib\servlet-api.jar -d classes src/com/example/web/BeerSelect It will compile without any errors.
|
 |
 |
|
|
subject: Package does not exist / Cannot find symbol message
|
|
|