Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to compile a package !!!  RSS feed

 
Raj Yelnare
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying the the first example from the 'more servlets '. I have two files one compiles independently ServletUtilities.java. But not the other one HelloServlet3.java. I am using jdk 1.4..
This is the error
-------------------------------------------------
c:\devel\HelloServlet3.java:24: cannot resolve symbol
symbol : variable ServletUtilities
location: class moreservlets.HelloServlet3
out.println(ServletUtilities.headWithTitle(title) +
^
1 error
----------------------------------------------
File - ServletUtilities.java
-------------------------------------------------
package moreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
/** Some simple time savers. Note that most are static methods.
* <P>
* Taken from More Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
*/
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");
}
....
-----------------------------------------------------
File - HelloServlet3.java
-------------------------------------------------
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.
* <P>
* Taken from More Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.moreservlets.com/.
* © 2002 Marty Hall; may be freely used or adapted.
*/
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>");
}
}
-------------------------------------------------
SET UP ON WINDOWS NT 4.0
-------------------------------------------------
CLASSPATH=.;c:\devel;c:\tomcat\jak\common\lib\servlet.jar;
JAVA_HOME=c:\jdk
______________________________________________
Any ideas ???
Raj
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had this problem as well. When I changed the name of the class to ServletUtil and change it all of my classes it worked. I am not sure why it was doing this.
 
Raj Yelnare
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried ..did not work..
C:\tomcat\jak\webapps\ROOT\WEB-INF\classes\moreservlets\HelloServlet3.java:24: cannot resolve symbol
symbol : variable ServletUtil
location: class moreservlets.HelloServlet3
out.println(ServletUtil.headWithTitle(title) +
^
1 error
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed this before. You need to have your classpath pointing the folder that contains the ServletUtilities class. Since this is in the web application it is generally recommended that you do not permanently set the classpath variable to do this.
 
Raj Yelnare
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have pointed the classpath permanently
like this
CLASSPATH=.;c:\devel;c:\tomcat\jak\common\lib\servlet.jar;
In this c:\devel is my directory where I am trying to compile the HelloServlet3.java. I got the same error. I think I am missing something
_______________________________________________
C:\devel\HelloServlet3.java:24: cannot resolve
symbol
symbol : variable ServU1
location: class moreservlets.HelloServlet3
out.println(ServU1.headWithTitle(title) +
^
1 error
Tool completed with exit code 1
________________________________________________
This time I changed the file name to ServU1 and the public class also to ServU1.
Raj
 
Michael OBrien
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raj,
Don't you have to create a new instance of your class before you can call methods?
HttpUtils util = new HttpUtils();
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael OBrien:
Raj,
Don't you have to create a new instance of your class before you can call methods?
HttpUtils util = new HttpUtils();

I have looked at the class he is referring to, and the methods should be static.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this directory structure:
<any folder>
|- src
| |- ServletUtilities.java
| |- HelloServlet3.java
|
|- cls

When you compile ServletUtilities.java, the command should be:
<any folder>\src:\>javac -classpath .;..\cls;<pathto servlet.jar> -d ..\cls ServletUtilities.java

Your ServletUtilities.class file should then be here: <any folder>/cls/moreservlets/ServletUtilities.class

Then complile the second file:
<any folder>\src:\>javac -classpath .;..\cls;<pathto servlet.jar> -d ..\cls HelloServlet3.java

I've tried this out and it works fine, without modifying any of the code you originally posted. It's that tricky classpath stuff that always drives people crazy that is tripping you up. My own feeling: " -d is your friend ". Type "javac" by itself at the command prompt for details on -d
In case anyone is interested, the error message if you're using jdk1.3.x or jdk1.2.xis:


Why this is happening:
In the second file, you are not explicitly importing your ServletUtilities class. And why would you, because HelloServlet3 and ServletUtilities are actually in the same package. But this is where the problem comes in. The ServletUtilities.class file is at the same directory level as HelloServlet3.java. When the compiler tries to find the ServletUtilities.class file, it tries to look in a package called 'moreservlets' (the same package that HelloServlet3.java is in), which translates to: "I'm looking for ./moreservlet/ServletUtilities.class". This file does not exist, ie: it is undefined, and thus the error message.

Which is why I always use -d when compiling from command lines, and then ensure that the directory I sent my class file to, is *also* in the classpath.
[ May 03, 2002: Message edited by: Mike Curwen ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!