• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

problem calling method on another class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2410
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so very much for your time and patience with me. It works!!
 
It looks like it's time for me to write you a reality check! Or maybe a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic