I am attempting to mirror the HeadFirst Servlets & JSP example - on page 85, Ch3. Somehow my java 1.5 compiler cannot find the package com.example.model even though i state import, and have already compiled that package's class.
My info: --- File: BeerSelect.java (in src/com/example/web/) --- package com.example.web;
response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Beer Selection Advice <br>"); String c = request.getParameter("color"); /* String advice = getBrands(c); out.println("<br>Recommended beer is " + advice); */ BeerExpert be = new BeerExpert(); List result = be.getBrands(c);
Iterator it = result.iterator(); while (it.hasNext()) { out.print("<br>try: " + it.next() ); } } }
--- 2nd File: BeerExpert.java (in src/com/example/model -- compiled successfully - copy of class in classes/com/example/model) --- package com.example.model; import java.util.*;
public class BeerExpert { public List getBrands(String color) { List<String> brands = new ArrayList<String>(); /* added <String> param to comply with 1.5 compiler */ if (color.equals("amber")) { brands.add("Jack Amber"); brands.add("Red Moose"); } else { brands.add("Jail Pale Ale"); brands.add("Gout Stout"); } return (brands); } }
----- Command Prompt entry and error responses: ---- C:\JAVA\servlets\beerV1>javac -classpath /program~1/apache~1/tomcat/common/lib/servlet-api.jar:class es:. -d classes src/com/example/web/BeerSelect.java src/com/example/web/BeerSelect.java:3: package com.example.model does not exist import com.example.model.*; ^ src/com/example/web/BeerSelect.java:4: package javax.servlet does not exist import javax.servlet.*; ^ src/com/example/web/BeerSelect.java:5: package javax.servlet.http does not exist import javax.servlet.http.*; ^ src/com/example/web/BeerSelect.java:10: cannot find symbol symbol: class HttpServlet public class BeerSelect extends HttpServlet { ^ src/com/example/web/BeerSelect.java:11: cannot find symbol symbol : class HttpServletRequest location: class com.example.web.BeerSelect public void doPost(HttpServletRequest request, ^ src/com/example/web/BeerSelect.java:12: cannot find symbol symbol : class HttpServletResponse location: class com.example.web.BeerSelect HttpServletResponse response) ^ src/com/example/web/BeerSelect.java:13: cannot find symbol symbol : class ServletException location: class com.example.web.BeerSelect throws IOException, ServletException { ^ src/com/example/web/BeerSelect.java:21: cannot find symbol symbol : class BeerExpert location: class com.example.web.BeerSelect BeerExpert be = new BeerExpert(); ^ src/com/example/web/BeerSelect.java:21: cannot find symbol symbol : class BeerExpert location: class com.example.web.BeerSelect BeerExpert be = new BeerExpert(); ^ 9 errors
--------- And got the following response: --------
com/example/web/BeerSelect.java:3: package com.example.model does not exist import com.example.model.*; ^ com/example/web/BeerSelect.java:21: cannot find symbol symbol : class BeerExpert location: class com.example.web.BeerSelect BeerExpert be = new BeerExpert(); ^ com/example/web/BeerSelect.java:21: cannot find symbol symbol : class BeerExpert location: class com.example.web.BeerSelect BeerExpert be = new BeerExpert(); ^ 3 errors -----------------
Thanks to anyone who could figure this one out. I've been studying this problem for hours...
[ December 02, 2005: Message edited by: Rob Rohrer ]
[ December 02, 2005: Message edited by: Rob Rohrer ]
[ December 02, 2005: Message edited by: Rob Rohrer ] [ December 02, 2005: Message edited by: Rob Rohrer ]
This is likely a classpath setting and has been discussed here many times. Make sure your upper level directory containing com.example.model is added to your classpath settings.
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Okay. I had partial success. I understand now it is essential to define the CLASSPATH by specifying the path up to but not including the name of the package the classes are in.
When I did this, I was able to compile my servlet with a simple command of...
...from the src directory. That WORKED and also passed the client test in my browser when copied over to my WEB-INF/classes/com/example/web dir.
HOWEVER, my friends, I still don't understand why the more complex command (as shown in HeadFirst on page 85) does NOT work and gives me the same nine errors (as originally reported - above).
Here's how my command reads (appended from page 85 in HeadFirst Servlets & JSP):
Does anyone see anything wrong here? Thanks so much for helping me with this amatuer problem. I've struggled with classpaths for years and keep reading and running into problems everytime I try resetting up my java environment. One day I'll get this down right.
Rob Rohrer
Greenhorn
Joined: Jan 23, 2004
Posts: 10
posted
0
Okay -- I figured it out and it's worth mentioning to other users I'm sure, since I couldn't find the answer in my search at JavaRanch (found the answer from Google on a doc called Using_CLASSPATH_and_Other_APIs.doc). This is it:
"The class path can be set using either the -classpath option when calling an SDK tool (the preferred method) or by setting the CLASSPATH environment variable. The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value."\
I never realized that you can't do both. I've always just set my classpath in the past and had never used the -classpath option from my command processor before today...
Hope this helps someone else. [ December 02, 2005: Message edited by: Rob Rohrer ]