jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Compile error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compile error" Watch "Compile error" New topic
Author

Compile error

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");
^
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);
^
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.



[ November 30, 2005: Message edited by: Timothy Eric ]

[ November 30, 2005: Message edited by: Timothy Eric ]

[ November 30, 2005: Message edited by: Timothy Eric ]
[ November 30, 2005: Message edited by: Timothy Eric ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Have you tried changing into the src/com/example directory and doing

javac -cp c:\myproj~1\listen~1\src ListenerTester.java
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
Eric,

Dog.java was compiled because it don't have any external dependency than jdk. With ListenerTester the case is different. ListenerTester references Dog which is not in the classpath. You put it under classes folder using the -d option which is right.

See
http://www.redbrick.dcu.ie/help/reference/JDK.1.1.8-docs/tooldocs/win32/javac.html
on how to set the classpath.

Try

javac -classpath .;%classpath%\classes; -d classes src\com\example\ListenerTester.java
[ November 30, 2005: Message edited by: jiju ka ]
Timothy Eric
Greenhorn

Joined: Nov 10, 2005
Posts: 5
Thanks again for all your help. You were both right and it was an issue w/ the classpath as suspected. Your suggestions however at the command line resulted in other errors again...

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.
[ December 01, 2005: Message edited by: Timothy Eric ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compile error
 
Similar Threads
Symbol not found
Error Compiling a Servlet
Package Problem
Head First Servlets and JSp problem
Problem importing Dog class