*
The moose likes Beginning Java and the fly likes Compiling a servlet which uses class from a package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling a servlet which uses class from a package" Watch "Compiling a servlet which uses class from a package" New topic
Author

Compiling a servlet which uses class from a package

Abhishk Singh
Ranch Hand

Joined: Aug 19, 2010
Posts: 43
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.
Paul Witten
Ranch Hand

Joined: Oct 10, 2012
Posts: 86
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

Joined: Aug 19, 2010
Posts: 43
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
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

Joined: Oct 10, 2012
Posts: 86
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

Joined: Oct 10, 2012
Posts: 86
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
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
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

Joined: Aug 19, 2010
Posts: 43
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

Joined: Oct 10, 2012
Posts: 86
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

Joined: Aug 19, 2010
Posts: 43
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

Joined: Oct 10, 2012
Posts: 86
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compiling a servlet which uses class from a package
 
Similar Threads
Problem in accessing model in diffeerent package(Servlet-HFS)
Package doesn't exists - error!!- How to resolve?
servlet compiling errors
package com.example.model does not exist
can't import own model