Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Servlets and JSp problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
176 ,chapter-5

-----------------------------------------
Dog.java location==f:\SC\tomcat\webapps\listenerTest\classes\com\example


----------------------------------------------------------------------
MyServletContextListener .java location==f:\SC\tomcat\webapps\listenerTest\classes\com\example


At cmd,i got :
f:\SC\tomcat\webapps\listenerTest\classes>javac -classpath f:\SC\tomcat\co
mmon\lib\servlet-api.jar com/example/MyServletContextListener.java
com\example\MyServletContextListener.java:12: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d=new Dog(dogBreed);
^
com\example\MyServletContextListener.java:12: cannot find symbol
symbol : class Dog
location: class com.example.MyServletContextListener
Dog d=new Dog(dogBreed);
^
com\example\MyServletContextListener.java:12: cannot find symbol
symbol : variable dogBreed
location: class com.example.MyServletContextListener
Dog d=new Dog(dogBreed);
^
3 errors

 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like MyServletContextListener.java class is not able to find the Dog class, have you placed your Dog.class properly in classes directory. please check and try.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog.class,MyServletContextListener.java and Dog.java are in the same directory
i have checked it
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure, but shouldn't you add the current directory to the classpath of javac when you override the default settings?
Also, (not related to the question directly but just a notice) you are missing the WEB-INF directory if you want to deploy the application as a web app:

f:\SC\tomcat\webapps\listenerTest\classes\com\example
f:\SC\tomcat\webapps\listenerTest\WEB-INF\classes\com\example

Cheers!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i placed Dog.class,MyServletContextListener.java and Dog.java in a separate directory and then tried to compile MyServletContextListener.java .
still getting the same error

if both the classes are in the same package then,why is MyServletContextListener.java not able to create instance of Dog in it's method.
Dog and MyServletContextListener.java are declared public too.
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohitkumar, as I said - you didn't add your source directory to the javac classpath.

My directory structure is as follows:

temp
   src
      com
         example
            MyServletContextListener.java
            Dog.java
   bin
      ....


assume 'temp' is your working directory.
When I invoke

javac -cp src:/home/pedro/apache-tomcat-6.0.29/lib/servlet-api.jar src/com/example/MyServletContextListener.java -d bin/

I can compile your example. Please notice the src: which points to the source files directory which are classpath needed for compilation of MySerlvetContextListener class.

Also, check your compilator errors beside the one you posted - you've got
dogBreed variable name at line 11, but you defined the dogbreed variable name at line 10.

Cheers!
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Pedro
Please notice the src: which points to the source files directory which are classpath needed for compilation of MySerlvetContextListener class.


i didn't get what you mean by theabbove statement
why it is needed to add src directory to classpath ?
 
Piotr Nowicki
Ranch Hand
Posts: 611
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use the -classpath (or -cp) switch explicitely, you are overriding the default settings not adding them.

Please refer to javac command reference guide especially the fragment with says:

The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.


Cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic