• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Compiling a servlet which uses class from a package

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement the examples given in HeadFirst Servlet and JSP.
Currently I am on 3rd chapter: Mini MVC tutorial.

I have one servlet named BeerSelect.java at following location:
F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src\com\example\web

When trying to compile the servlet from F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src directory, I am using following command:
javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar" com/example/web/BeerSelect.java

The program compiles successfully till the time when I am not using a class from a separate package.

The name of the separate class is BeerExpert.java , and it is placed at following location:
F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src\com\example\model

When I add following statement inside BeerSelect.java
import com.example.model.*;
and try to compile the program from F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src in command prompt using following command:
javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar" com/example/web/BeerSelect.java
I am getting following error:
package com.example.model does not exist.

Please suggest if I am making some obvious mistake. Do I need to add the path to BeerExpert in the classpath? If that is the case, please help me with the correct syntax to be followed on windows for the same.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhishek singhania wrote:Please suggest if I am making some obvious mistake. Do I need to add the path to BeerExpert in the classpath? If that is the case, please help me with the correct syntax to be followed on windows for the same.


Disclaimer: I have not worked with servlets for years. There are others here that are far more qualified than me to advise you (since I can find no fault of your actions.) That being said:

Check BeerSelect.java to make sure you have declared the package "com.example.web".

Since you are executing from the src dir I don't think you need the path in classpath but it will never hurt to do that.
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, declaration of package, com.example.web is present at the starting of the class.

Following is the code for BeerExpert.java:


-------------------------------------------------------------------------------------------------------------------------------------------------------------
And following is the code for BeerSelect.java
 
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really are at the beginner’s stage, you need to know that compiling classes in packages is a little more complicated than compiling in the unnamed package. I seem to ahve written about that before, so if you search for “compiling (or compile) package”, you get this sort of thing, and this, this, this, this and probably more. There is also a packages section in the Java Tutorials, maybe two sections.
I hope those links will be helpful, and I hope they don’t all point to the same post in the end!
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhishek singhania wrote:Yes, declaration of package, com.example.web is present at the starting of the class.


Good - eliminate that possibility.

Now try adding the path to the missing file to classspath. I would start with the absolute path from the root of the drive first. If that works then you can fiddle with trying to make a relative path work.

You have not done anything obviously wrong. My guess is that it's not using Current Dir the way we would expect (to find the relative path to the missing file.) Why, I do not know, but try the cp thing next.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhishek singhania wrote:javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar" com/example/web/BeerSelect.java


What happens if you try com/example/web/*.java ?
 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have changed the bold and red text to ordinary black in two posts because red text is difficult for some people to read. And looks nasty.
 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Witten wrote:

abhishek singhania wrote:javac -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar" com/example/web/BeerSelect.java


What happens if you try com/example/web/*.java ?




Thanks for the inputs Paul. But the behavior on my system is same irrespective of '/' or '\'. Also, using the * wildcard dosen't make any difference.
Currently I am going through links provide by Campbell.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhishek singhania wrote: But the behavior on my system is same irrespective of '/' or '\'. Also, using the * wildcard dosen't make any difference.


Abhishek, I think you will have success when you do the following:

1. Compile BeerExpert first. It's a helper class (does not have dependencies), and then compile BeerSelect (which does have dependencies, and now that the dependency exists in class form it can be found.)

2. When you compile BeerSelect you must include BeerExpert in the -cp statement.

3. In javac statements make sure to use the -verbose arg as the first arg. This will provide an exact description of any failures and will be much better than guessing.

4. Generally all methods in the helper class need to be declared public in order for the consumer class to find them outside of its own package. I'm not sure about servlet doPost() but if you can make it public, do so. Otherwise your verbose output would tell you exactly if there is no such method found, or something to that effect.

The above assumes that there are no typos or files found in the wrong directory, etc, but those are issues you can see happening and correct them. Well, sometimes it's easy and sometimes not. I had a typo with an uppercase letter being lowercase and it make the class not found. Even verbose output can't tell you that it found a class that looks a bit like the one you wanted. :-D

Good luck. Let us know how you make out.

p


 
Abhishk Singh
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Witten wrote:
When you compile BeerSelect you must include BeerExpert in the -cp statement.
The above assumes that there are no typos or files found in the wrong directory, etc, but those are issues you can see happening and correct them. Well, sometimes it's easy and sometimes not. I had a typo with an uppercase letter being lowercase and it make the class not found. Even verbose output can't tell you that it found a class that looks a bit like the one you wanted.


You were right when you mentioned about including the classpath.
I could compile the servlet successfully using following command:
javac -verbose -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar;";"F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src\com\example\model" com\example\web\BeerSelect.java

while the same result could not be achieved using following command:
javac -verbose -cp "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar";"F:\handsOn\scwcd\devEnv\MyProjects\beerV1\src\com\example\model" com\example\web\BeerSelect.java

Notice the effect of not placing the semicolon <;> at the end of servlet-api.jar.
Thank you for the help.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abhishek singhania wrote:Notice the effect of not placing the semicolon <;> at the end of servlet-api.jar.


Good catch! I have tried using semi-colon outside of the quotation marks. That is one of those Windows platform "gotchas" that you never see on Nix boxes.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic