my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Package problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package problems" Watch "Package problems" New topic
Author

Package problems

B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Hi Guys,

I am following a tutorial from the Head First servlets & JSPs book (not very well it would appear ).

I am trying to compile this code:


When i do the compile i get these errors:


This is really frustrating as i am following everything word for word in the book.

Does anyone see where i am going wrong?
If i have not given enough info please tell me what you need to help me sort this problem out

Your help would be greatly appreciated.
Thanks,
-Ben
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
My package directory is:

com
|
example
|
model

aaaahhhh !

Why is this not working? Pleaase Help me

Thanks,
-Ben
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

In what folder are you when you try to compile the class?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
Hi Ben,

if you try to compile your classes in the console window move into the src directory.

Type

javac com/example/web/BeerSelect.java

into the console and press enter.


That should work.

Kind regards


SCJP 5, SCWCD 1.4, SCBCD 5
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Hi thanks for your reply.

My Directory is set up like this (sorry if its a bit confusing this was the best way i could lay it out)

MyProject
|
beerV1
|
classes------|-----src
|-------------------|
com----------------com
|-------------------|
example-----------example
/-----\-------------/----\
web-model-------web-model

The way I am compiling is this:

cd ~/MyProject/beerV1
javac -d classes src/com/example/web/BeerSelect.java

So i have my .java files in the src part of the directory and when i compile them the .class file gets sent to the classes part of the directory

-Ben
[ November 15, 2008: Message edited by: B Crosthwaite ]
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Hey Reinhard,

Thanks for your reply, however i was presented with the same errors when i tried what you said.

Any other ideas?

-Ben
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
Do yo have a file named

BeerExpert.java

in the directory scr/com/example/model?
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Yes.

scr/com/example/model/BeerExpert.java



-Ben
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
Check your directory and file names. Correct spelling, capitalized letters etc..
I get the same errors when I remove BeerExpert.java.
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
I have checked and double checked

This is very disheartening

Have you managed to compile my code?

-Ben
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
Yes but I replaced the Servlet class with a normal class. How did you set the classpath of the servlet.jar file and what OS do you use?
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
my classpath is set like this:
CLASSPATH /Library/Tomcat/Home/lib/servlet-api.jar

I am on Mac OS X (version 15.5.5)

Im sorry please can you explain what you mean by "replaced the Servlet class with a normal class" i don't fully understand.

again, thanks for you time. I really appreciate it

-Ben
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
The errors you get are normal environment errors, meaning there is something wrong with
your directory structure or your file names. It is not a special Servlet problem. The problem would occur also with a "normal" (non Servlet) class. I did not use a servlet class to simplify things. If you want to compile a servlet you must have the servlet jar files in your classpath settings otherwise the complier would not recognize the HttpServlet class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
It is worth trying compiling the classes one at a time, leaving the class with the main method until last. When you import a class from another package the compiler seems to think that class is already compiled and ready to go.

Or (if there aren't too many classes) compile a list of classes all together

javac mypackage/Class1.java myotherpackage/Class2.java

Or even
javac mypackage/*.java
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
You are right. (See compile Xlint Errors). The class file must be in the wrong directory or not existing.
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
Ben,

it is the best to simply your steps.

1) make sure that BeerExpert.java is in src/com/example/model
2) make sure that BeerExpert.class is src/com/example/model
3) move to the src directory
4) compile with javac -classpath /Library/Tomcat/Home/lib/servlet-api.jar com/example/model/BeerExpert.java com/example/web/BeerSelect.java
5) move the class files (manually) from the src subdirectories into the classes subdiretories

I tried this successfully. This time with a real Servlet class.

Good luck.
Vaibhav Jaiswal
Greenhorn

Joined: Jan 17, 2008
Posts: 12
instead of this
cd ~/MyProject/beerV1
javac -d classes src/com/example/web/BeerSelect.java

try out
cd ~/MyProject/beerV1/src
javac -d classes -cp ../classes com/example/web/BeerSelect.java

For the error in the first post
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Hey Rienhard, Campbell and Vaibhav,

Thanks for all your replys.

Rienhard your steps worked for me, thank you.

-Ben
Reinhard Horn
Ranch Hand

Joined: Jun 04, 2007
Posts: 31
This might be the better solution:

javac -classpath /Library/Tomcat/Home/lib/servlet-api.jar:.
-d classes src/com/example/web/BeerSelect.java
src/com/example/model/BeerExpert.java

Must be execute in beerV1.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Well done
 
jQuery in Action, 2nd edition
 
subject: Package problems
 
Similar Threads
Package does not exist
help for HFSJ - CH 3 Page 84
Mistakes in HFSJ
Trouble compiling servlet HFSJ pg 85
Compiling servlet version two code in HFSJ