File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes a rare (to me) compilation problem & solution worth mentioning - see final solution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "a rare (to me) compilation problem & solution worth mentioning - see final solution" Watch "a rare (to me) compilation problem & solution worth mentioning - see final solution" New topic
Author

a rare (to me) compilation problem & solution worth mentioning - see final solution

Rob Rohrer
Greenhorn

Joined: Jan 23, 2004
Posts: 10
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;

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");
/* 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

----
My Environmental Variables:
CLASSPATH (reads)C:\JAVA\servlets;C:\Program Files\Apache Group\tomcat\common\lib\servlet-api.jar;C:\Program Files\Apache Group\tomcat\common\lib\jsp-api.jar
JAVA_HOME (reads) C:\Program Files\Java\jdk1.5.0_02


Using XP, Tomcat5, jdk1.5.0_02


------------------
BTW, I also tried a simpler command from my src directory:

C:\JAVA\servlets\beerV1\src>javac -d ../classes com/example/web/BeerSelect.java

---------
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 ]
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You may want to check out this FAQ entry for some tips:
http://faq.javaranch.com/view?CompilingServlets


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rob Rohrer
Greenhorn

Joined: Jan 23, 2004
Posts: 10
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...

javac -d ../classes com/example/web/BeerSelect.java com/example/web/BeerSelect.java

...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):

javac -classpath /program~1/apache~1/tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java

The "servlet-api.jar" is *definitely* in my common/lib dir. The full path to it is: C:\Program Files\Apache Group\tomcat\common\lib

My CLASSPATH as a User Variable now reads:

Variable name: CLASSPATH
Variable value: C:\JAVA\servlets\beerV1;C:\JAVA\servlets\beerV1\src;C:\Program Files\Apache Group\tomcat\common\lib\servlet-api.jar;C:\Program Files\Apache Group\tomcat\common\lib\jsp-api.jar

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
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 ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Thanks, we always appreciate it when people foolow up once they've found a solution!
 
 
subject: a rare (to me) compilation problem & solution worth mentioning - see final solution
 
Similar Threads
Compiling problems
Mistakes in HFSJ
can't compile a servlet
can't import own model
Compiling servlet version two code in HFSJ