• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem calling method on another class

 
Harsh Chandola
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am about to finish the book Head First Java, I am at the last chapter. There's an example of how servlets can be used to call methods on other classes, so I wrote the corresponding code provided in the book; the code for the servlet goes like this :




and code for the method that's being called goes like this :



I am using Tomcat 8, my directory where I am keeping my classes is like this :

C:/Tomcat8/webapps

In webapps folder the structure of the folder where my class lands is like this

webbapps > PhraseOMatic

PhraseOMatic has two folders src and WEB-INF,

WEB-INF folder has the classes folder where the both the classes should land.
WEB-INF folder also has the XML file.

now when i compile the PhraseOMatic.java file, it compiles without any error and the class lands into the classes folder.
when I compile the KathyServlet.java the compiler shows the following error :

C:\Tomcat8\webapps\PhraseOMatic\src>javac -classpath ..\..\..\lib\servlet-api.jar -d ../WEB-INF/classes KathyServlet.java
KathyServlet.java:16: error: cannot find symbol
out.println("<P>" + PhraseOMatic.makePhrase());
^
symbol: variable PhraseOMatic
location: class KathyServlet
1 error

I have no Idea why am I getting this error. Why isn't it being able to call the method in PhraseOMatic class, any help will be greatly appreciated.

Thank You
Harsh Chandola
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you only include the servlet-api.jar file as your CLASSPATH. But you don't include the classes directory where PhraseOMatic.class is found
 
Harsh Chandola
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith Lynn wrote:Note that you only include the servlet-api.jar file as your CLASSPATH. But you don't include the classes directory where PhraseOMatic.class is found


Yes, I did think of that so what I did was that I pasted PhraseOMatic.class in every directory obviously that didn't help but I thought it was worth trying. So how do I get around this problem? how do I include the classes directory in cmd command?
What I also did was I had both the .java files inside the classes directory and tried to compile from that directory but that didn't help either
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simple include the directory containing the .class file in the CLASSPATH
 
Harsh Chandola
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So would the syntax be :

javac -classpath <location of servlet-api.jar>, <location of PhraseOMatic.class> -d<location of where to drop the class> KathyServlet.java ?
 
Harsh Chandola
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so this is how I did it now, it still didn't work.

C:\Tomcat8\webapps\PhraseOMatic\src>javac -classpath "..\..\..\lib\servlet-api.jar;..\WEB-INF\classes\PhraseOMatic.class" -d ../WEB-INF/classes KathyServlet.java
KathyServlet.java:16: error: cannot find symbol
out.println("<P>" + PhraseOMatic.makePhrase());
^
symbol: variable PhraseOMatic
location: class KathyServlet
1 error


What am I doing wrong?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't include the .class file in the CLASSPATH. Just include the directory it's in.
 
Harsh Chandola
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so very much for your time and patience with me. It works!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic