aspose file tools*
The moose likes Servlets and the fly likes Error Compiling a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error Compiling a Servlet" Watch "Error Compiling a Servlet" New topic
Author

Error Compiling a Servlet

Timothy Eric
Greenhorn

Joined: Nov 10, 2005
Posts: 5
Let me start off by thanking you all in advance.

I am trying to get started with learning JSP & Servlets. I have purchased the Head First Servlets & JSP book by Basham, Sierra, & Bates. In following an example i have come across an issue that is preventing me from progressing in my learning. Basically i have created three classes and packaged them as such:

Dog.java (com.example)
ListenerTester.java (com.example)
MyServletContextListener (com.example)

Both packages reside under the same folder "/src"

I am able to compile my Dog.java code, but not the other two. The following is the code for all 3 of my classes:

package com.example;

public class Dog {

private String breed;

public Dog(String breed) {
this.breed = breed;
}

public String getBreed() {
return breed;
}
}


package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class ListenerTester extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("test context attributes set by listener<br>");

out.println("<br>");

Dog dog = (Dog) getServletContext().getAttribute("dog");

out.println("Dog's breed is: " + dog.getBreed());
}
}


package com.example;

import javax.servlet.*;

public class MyServletContextListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
Dog d = new Dog(dogBreed);
sc.setAttribute("dog", d);
}

public void contextDestroyed(ServletContextEvent event) {
//nothing to do here
}
}


Here is the error i am receiving when trying to compile the last 2 classes:

C:\MYPROJ~1\LISTEN~1>javac -d classes src\com\example\Dog.java

C:\MYPROJ~1\LISTEN~1>javac -d classes src\com\example\ListenerTester.java
src\com\example\ListenerTester.java:20: cannot find symbol
symbol : class Dog
location: class com.example.ListenerTester
Dog dog = (Dog) getServletContext().getAttribute("dog");
^
src\com\example\ListenerTester.java:20: cannot find symbol
symbol : class Dog
location: class com.example.ListenerTester
Dog dog = (Dog) getServletContext().getAttribute("dog"); //hat symbol for the error is focused underneath the second dog

2 errors

C:\MYPROJ~1\LISTEN~1>javac -d classes src\com\example\MyServletContextListener.j
ava
src\com\example\MyServletContextListener.java:12: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d = new Dog(dogBreed);
^
src\com\example\MyServletContextListener.java:12: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d = new Dog(dogBreed); //hat symbol for the error is focused underneath the second dog


2 errors

C:\MYPROJ~1\LISTEN~1>


I thought maybe my CLASS_PATH environment variable needed to be changed, but i tried setting it to these 2 and each time it did not help:

C:\MyProjects\listenerTest\classes\com\example
C:\MyProjects\listenerTest\classes\

Please help me. I am running Tomcat 5.5.12 and have JDK1.5.0_05 installed.


Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Tomothy,

It is just a classpath error. See below with screen-scrap of the command line where I compiled your three programs where C:\Test equivalent to your C:\MYPROJ~1\LISTEN~1>.

Note that to compile successfully you need to place the Servelet related classes in the classpath so I have just put the weblogic.jar (which would have all these classes) into the classpath.

Timothy Eric
Greenhorn

Joined: Nov 10, 2005
Posts: 5
Thanks again for all your help. Your explanation is very clear and it gave me insight as to where to look to solve my compile dilemma.

With help from your explanation, what i ended up doing was looking at my environment variables again (Windows XP). I had added the folder for my servlet related classes to the CLASS_PATH variable under the System Variables but not to the CLASSPATH variable under the User Variables section. Sorry to sound clueless about this, but what is the difference? After i added the folder of my classes to the CLASSPATH variable, i was able to compile.
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
It is just the name of the environment variable used by the compiler (i.e. javac). This name would have been hardcoded into the compiler itself in particular platform. So in windows for javac the variable is CLASSPATH. I think in some flavor of Unix, it can be specified as CLASS_PATH, but Im not sure which ones though.

Any comments by others on this?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error Compiling a Servlet