It's not a secret anymore!*
The moose likes Servlets and the fly likes Problem with Packages / Utility Classes in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with Packages / Utility Classes in Servlets" Watch "Problem with Packages / Utility Classes in Servlets" New topic
Author

Problem with Packages / Utility Classes in Servlets

Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
Hello!
I am using the Java 2 SDK 1.4.0 and Tomcat 4.0.3 (Catalina) for my development tools...
I am running this on Windows 2000 Professional, and have set all of my user environmental variables as:
CLASSPATH = .;%JAVA_HOME%\jre\lib\rt.jar;.;%JAVA_HOME%\lib\tools.jar;.;%CATALINA_HOME%\common\lib\servlet.jar;.;c:\Projects;
JAVA_HOME = c:\j2sdk1.4.0
CATALINA_HOME = c:\jakarta-tomcat-4.0.3
*Note*: The c:\Projects is the top level name of my development directory...
--------------------------------------------------
I bought Marty Hall's book, "More Servlets & JavaServer Pages", and have been following the examples... The problem that I have is that my javac compiler doesn't recognize my ServletUtilities class inside another Java source that utilizes it... Here are the two files... Marty Hall repeatedly mentioned to place the top-level servlet development directory into my CLASSPATH (which I did with c:\Projects), but I get an unrecognized symbol message every time I attempt to compile HelloServlet3.java... I will be very appreciative if anyone can help me with this issue... Here are the two source files (please note that they are in c:\projects\classes\moreservlets)-- I just copy and paste the compiled .class files into c:\jakarta-tomcat-4.0.3\webapps\ROOT\web-inf\classes\moreservlets
--------------------------------------------------
package moreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet used to test the use of packages
* and utilities from the same package.
*/
public class HelloServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Hello (3)";
out.println(ServletUtilities.headWithTitle(title) +
"<body bgcolor=\"#fdf5e6\">n" +
"<h1>" + title + "</h1>\n" +
"</body></html>");
}
}
--------------------------------------------------
package moreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<html>\n" +
"<head><title>" + title + "</title></head>\n");
}

}
--------------------------------------------------
Any tips and suggestions and I will be very grateful!
-Unnsse M. Khan
Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
Also please note that the error that I am getting is: (when I try to compile HelloServlet3. java)
C:\Projects\classes\moreservlets>javac HelloServlet3.java
HelloServlet3.java:20: cannot resolve symbol
symbol : variable ServletUtilities
location: class moreservlets.HelloServlet3
out.println(ServletUtilities.headWithTitle(title) +
^
1 error
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The answer is to use the -d switch when compiling.

Please see my comment in this thread (at the bottom of the page), where another user has the exact same problem as you.

http://www.coderanch.com/t/354453/Servlets/java/Unable-compile-package
[ May 05, 2002: Message edited by: Mike Curwen ]
Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
Mike,
From the link you put for the solution, I didn't understand your directory structure when you used ../cls ??? My development directory is c:\Projects\classes\moreservlets, that is where ServletUtilities.java and HelloServlet3.java files are. When I compile ServletUtilities.java by using:
javac -verbose ServletUtilities.java
It automatically turns it into a ServletUtilities.class file...
The problem is with the HelloServlet3.java... I would appreciate it if you could show me that command line (accordingly to my directory structure)....
Sincerely yours,
Unnsse
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Unnsse,

I'm not sure where to begin.

The problem with the HelloServlet3.java, is that you compiled the ServletUtilities.java improperly. Yes it worked, but when packages and importing are involved, you have to take extra care.

The reason you use "..\cls" is to move the 'automatically' generated ServletUtilities.class file to a place where the javac command will find it, and use it appropriately, when it is time to compile HelloServlet3.java.

If you're not sure what exactly "..\cls" means, then the '..\' part means 'go up one directory' and 'cls' of course means 'the cls directory in the current level'

So if I'm in a\b\c\d directory and I say "cd ..\..\" that should take me to the 'b' directory.

Back to java compiling. If I'm in c:\anyfolder\src, and I want my class files to be placed in c:\anyfolder\cls, then I have two choices:
c:\anyfolder\src> javac -d c:\anyfolder\cls
- or -
c:\anyfolder\src> javac -d ..\cls

option 2: see how I'm in 'src' already? i'm telling it to go back up one (to 'anyfolder') and then back down to 'cls'

Now ... I think you can extrapolate how to make this work in your own environment. You can name the directories anything you want (for example, your version of 'src' is c:\Projects\classes\moreservlets ). So now choose some other directory to be 'cls'.

The main point is: your class files should NOT be in the same directory that your source files are being actively compiled in. If this is confusing, you can just move the class file by hand, and forget '-d' switch.

And as always.. make sure the directory you move them into is in your classpath. (which is why I had "..\cls" in the -classpath switch as well).
[ May 05, 2002: Message edited by: Mike Curwen ]
Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
Mike,
I really appreciate your help, but I am still getting the same error. Here is what I did.
I now have two directories:
c:\Projects\classes\moreservlets\classes
c:\Projects\classes\moreservlets\source
Now, when I compile ThereParams.java like this:
C:\Projects\classes\moreservlets\source>javac -d ..\classes ThreeParams.java
ThreeParams.java:21: cannot resolve symbol
symbol : variable ServletUtilities
location: class moreservlets.ThreeParams
out.println(ServletUtilities.headWithTitle(title) +
^
1 error
Both of the source files, ServletUtilities.java & ThreeParams.java are in the c:\Projects\classes\moreservlets\source directory.
-----------------------------------------------------------------------------------------------------------------------------
package moreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Simple servlet that reads three parameters from the
* form data.
*/
public class ThreeParams extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading Three Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>param1</b>: "
+ request.getParameter("param1") + "\n" +
" <li><b>param2</b>: "
+ request.getParameter("param2") + "\n" +
" <li><b>param3</b>: "
+ request.getParameter("param3") + "\n" +
"</ul>\n" +
"</body></html>");

}
}
-----------------------------------------------------------------------------------------------------------------------------
package moreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<html>\n" +
"<head><title>" + title + "</title></head>\n");
}
}
-----------------------------------------------------------------------------------------------------------------------------
Again thanks for your help,
Unnsse M. Khan
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

you forgot -classpath. Read the original linked thread again, and my last paragraph in my last post in this thread.
[ May 05, 2002: Message edited by: Mike Curwen ]
Unnsse Khan
Ranch Hand

Joined: Nov 12, 2001
Posts: 511
It worked! It made a new moreservlets directory in c:\Projects\classes\moreservlets\classes\moreservlets and when I copied the .class files into the Tomcat 4.0.3\webapps\root\web-inf\classes\moreservlets directory, they worked!
Thanks!
If I bookmarked both of these links, would they be viewable, in the future? What happens to these answers after they have been solved?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You can bookmark these pages, and as long as we don't have some sort of disaster on the server-side, this thread will remain for a long time.


*knocks on wood*
 
jQuery in Action, 2nd edition
 
subject: Problem with Packages / Utility Classes in Servlets
 
Similar Threads
Package
Unable to compile a package !!!
how to compile utility class
Testing Servlet3.java - Errors
how to compile with utilityclass