This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package" Watch "Package" New topic
Author

Package

Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
In the past I did not have to deal with packages. Now I do and need you help please.
The class path is set as follows in autoexec.bat:
set PATH=%PATH%;F:\j2sdk1.5.0\bin
set JAVA_HOME=F:\j2sdk1.5.0
set CATALINA_HOME=F:\Tomcat5
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\common\lib\servlet-api.jar
cd %CATALINA_HOME%\webapps\ATS\ WEB-INF\classes\wrox
Both my classes are in the wrox directory and one of them refers the other. When I run (at the command prompt F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox javac HelloWWW3.java) the one that refers the other both of them should be compiled automatically but instead I get an error:
Cannot find symbol variable ServletUtilities
Location wrox.HelloWWW3
out.println(ServletUtilities.headWithTitle(title)
HelloWWW3:
package wrox;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWWW3 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Hello WWW again";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY>\n" +
"<H1>Hello WWW</H1>\n" +
"</BODY></HTML>");
}
}
ServletUtilities:
package wrox;
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");
}
}
Please note that I have no difficulty compiling code that does not refer to a custom class. For example I was able to run ServletUtilites and it did compile with no problem. Your help is very much appreciated.
Maru
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Mulugeta Maru,
------ Originally posted by Mulugeta Maru: ------
The class path is set as follows in autoexec.bat:
set PATH=%PATH%;F:\j2sdk1.5.0\bin
set JAVA_HOME=F:\j2sdk1.5.0
set CATALINA_HOME=F:\Tomcat5
set CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\common\lib\servlet-api.jar
cd %CATALINA_HOME%\webapps\ATS\ WEB-INF\classes\wrox
Both my classes are in the wrox directory and one of them refers the other. When I run (at the command prompt F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox javac HelloWWW3.java) the one that refers the other both of them should be compiled automatically but instead I get an error:
Cannot find symbol variable ServletUtilities
Location wrox.HelloWWW3
out.println(ServletUtilities.headWithTitle(title)
HelloWWW3:


I have to say that I am surprised to see that you have already posted 54 times and still do not use code tags.
[ April 04, 2004: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mulugeta Maru
Ranch Hand

Joined: Jan 20, 2003
Posts: 68
I am sorry. How do I use tags? I never tried it before and please forgive me if it caused a problem.
Regrards,

Maru
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The reason you are getting this is the class wrox.ServletUtilities is not in your classpath. Keep in mind that the full name of a class is its package name plus class name. The directory that holds the package (not the directory that holds all the classes of the package) must be in your classpath.
I�m going to assume that your default classpath has an entry of current directory (i.e. a period). Therefore since you are executing your javac command from within the directory F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox, your classpath has the directory that contains the classes of the wrox package, but not the directory that contains the wrox package. In other words, your classpath has the following entries:
  • F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox (the current working directory)
  • F:\Tomcat5\common\lib\servlet-api.jar
  • Any other directories in you default Classpath (i.e. %CLASSPATH%)


  • When the compiler is compiling the HelloWWW3.java file, it encounters the wrox.ServletUtilities class in your code and tries to find the corresponding wrox.ServletUtilities.class file in your classpath, or the .java source file so it can compile it. However, neither file is in your classpath. Since your current working directory is the wrox directory, as it transverses that directory to look for the ServletUtilities class or source code, it looks for a file of:
    F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox\wrox\ServletUtilities.class
    Or
    F:\Tomcat5\webapps\ATS\WEB-INF\classes\wrox\wrox\ServletUtilities.java
    and does not find the class or its source code. (notice the double nested wrox name in the paths) That�s because the full name of your class is wrox.ServletUtilities, so it looks for the ServletUtilities file inside a wrox directory inside one of your classpath directories (or archives such as the servlet-api.jar you have included).
    The solution is that you want the "classes" directory in your classpath, not the "wrox" directory. So do not cd into the wrox directory to compile your code. Instead cd into the directory that contains the wrox package and then compile your code. In other words:
    cd %CATALINA_HOME%\webapps\ATS\ WEB-INF\classes
    javac wrox/HelloWWW3.java
    This will allow the compiler to find the source code for the ServletUtilities class and compile it at the same time it compiles your HelloWWW3 class.
    Alternatively, you could add the %CATALINA_HOME%\webapps\ATS\ WEB-INF\classes directory to your classpath.
    [ April 04, 2004: Message edited by: Mark Vedder ]
    Mark Vedder
    Ranch Hand

    Joined: Dec 17, 2003
    Posts: 624

    In regards to using [code] tags, take a look at the information on this page: Using UBB Tags
    It is as simple as placing your code between a set of [code] and [/code] tags.
    So if in the posting window I type:
    [code]
    public class SomeClass
    {
        more code typed here
    }
    [/code]
    It will be displayed as:

    [ April 04, 2004: Message edited by: Mark Vedder ]
    Mulugeta Maru
    Ranch Hand

    Joined: Jan 20, 2003
    Posts: 68
    I added the %CATALINA_HOME%\webapps\ATS\ WEB-INF\classes directory to my classpath and it works.
    Thank you very much for your clear answer to my question.
    Regards,
    Maru
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Package
     
    Similar Threads
    static method accessing
    Testing Servlet3.java - Errors
    package problem
    Problem with Packages / Utility Classes in Servlets
    Cannot resolve symbol