wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Oh No. Not Classpaths AGAIN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Oh No. Not Classpaths AGAIN" Watch "Oh No. Not Classpaths AGAIN" New topic
Author

Oh No. Not Classpaths AGAIN

Steven Manfred
Greenhorn

Joined: Oct 02, 2006
Posts: 4
Hey All,

I was working my way through Kathy's HF book and got as far as compiling the first BeerSelect servlet but it won't work because it can't find the servlet packages. Now I know what you're going to say 'Set the Classpath stoopid' but hold your horses a second.

Here's the Layout....

C:/Tomcat
C:/MyProjects/beerV1

so from the BeerV1 directory...

javac -classpath /Tomcat/common/lib/servlet-api.jar:classes:. -d classes src/com/example/web/BeerSelect.java

..should work right? after all the jar file is right there under Tomcat so whether I have my CLASSPATH env. variable set or not should be irrelevant as I've explicitly stated where the jar lives on the command line right? (BTW it is set anyway)

So why do I keep getting the 'javax.servlet does not exist' stuff for my imports?

Thanks
Steve
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Moved to Java In General (Beginner) where classpaths, and javac are discussed.
You may also want to see http://faq.javaranch.com/view?CompilingServlets


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Remember on Windows, the path seperator is ; not :, so if you are getting javax.servlet.* not found, and the servlet-api.jar class is in the correct place, then I think what's probably happening is that it's looking for the file servlet-api.jar:classes:.
Steven Manfred
Greenhorn

Joined: Oct 02, 2006
Posts: 4
OK I think I understand what you're saying with regard to semicolons as seperators, my classpath variable looks like this....

.;C:\Tomcat\common\lib\servlet-api.jar

I'm not sure that the formatting should matter though as I've used the -classpath option on the command line and the command is just what Kathy has in her book (with my path in obv.)
Steven Manfred
Greenhorn

Joined: Oct 02, 2006
Posts: 4
Ooo,I take it back. When I drop the :classes:. bit from the commandline it works! Not sure I understood what that bit was doing anyway.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Unless I miss my guess most of the examples in that book are from OS X, the OS I also use for Java development.

Under Windows, the separator must be semi-colon.

And the :classes: clause (;classes; on Windows) adds a sub-folder named classes to the classpath.
[ October 02, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Ooo,I take it back. When I drop the :classes:. bit from the commandline it works! Not sure I understood what that bit was doing anyway.

I was just having problems with that same example, and the first thing we have to realize is that the book uses UNIX commands. So, we Windows users have to be on the lookout for syntax we've never seen before, e.g.:

servlet-api.jar:classes:.

(note the period at the end). The unfamiliar syntax will most likely be Unix specific.

As you probably know, you can set the Windows CLASSPATH environment variable to tell java where to hunt for files, or you can use the classpath compiler option, e.g.

javac -classpath C:\Tomcat\common\lib\servlet-api.jar
...

You are also probably aware that you can tell either Windows or the javac compiler to hunt for files in more than one directory. Your CLASSPATH environment variable currently probably points to more than one directory. You can tell the javac compiler to look in more than one directory using the Windows syntax:

javac -classpath directoryA;directoryB;directoryC ...

Notice how a semicolon is used to separate each directory. Also, a dot(.) means to look in the current directory. After a bit of digging around, I discovered that in UNIX, a colon is used to seperate the directories. So, the following classpath option found in the example:

javac -classpath C:\Tomcat\common\lib\servlet-api.jar:classes:.(note the dot at the end)

tells javac to look for files in the following directories:

1) C:\Tomcat\common\lib\servlet-api.jar

2) classes

3) . (the current directory)

Your post demonstrates that you know what #1 does. As for #2, remember that the Windows prompt that you are at is the starting point, so the javac compiler will look for the "classes" directory in the MyProjects/beerV1 folder and then look in the "classes" folder for the necessary files. #3 says to simply look in the current directory, which is MyProjects/beerV1.

As far as I can tell, the classes development directory should be empty, so telling javac to look there for files is unnecessary. Also, I can't see how the current directory has any files that are used by the servlet. So, I think the command in the book should simply be:

javac -classpath /Tomcat/commonlib/servlet-api.jar

which would have been less confusing to at least the two of us.

Note also that slashes go the other way in Windows, although I believe either a forward slash or backslash works.
[ October 02, 2006: Message edited by: sven studde ]
Steven Manfred
Greenhorn

Joined: Oct 02, 2006
Posts: 4
Yeah thanks for the info, very good stuff. I did mess around with Solaris many years ago but I have to confess I've forgoten it all now.

I don't mind the problems too much as I think you learn more trying to fix a problem than if it all goes smoothly. Up to a point anyway.

Now if I can just workout why it claims 'import com.example.model.*;' directory doesn't exist on the page 84's compile I can goto bed happy
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
1) What command did you use to compile?

2) What prompt were you at when you typed in that commmand?

Now if I can just workout why it claims 'import com.example.model.*;' directory doesn't exist on the page 84's compile I can goto bed happy


I looked a little more closely at the example on p.84, and the example doesn't contain that import statement. First off, trying to import files in a directory called com.example.model is ambiguous: there are two com.example.model directories in the development directory. Normally, you would solve that ambiguity when you used javac to compile the program: you would use the -classpath option to specify the full path to the particular directory that you meant.

In any case, the BeerExpert class in the book doesn't reference any classes in either of the duplicate com.example.model directories, so your import statement doesn't do anything. As a test, I added your import statement and compiled a different, but simialr, BeerExpert class that I came up with. I used the compile command in the book, and my BeerExpert.java file compiled fine. javac apparently ignored your import statement since it wasn't required.
[ October 03, 2006: Message edited by: sven studde ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3681
    
  16
Originally posted by sven studde:

I was just having problems with that same example, and the first thing we have to realize is that the book uses UNIX commands. So, we Windows users have to be on the lookout for syntax we've never seen before, e.g.:

servlet-api.jar:classes:.

(note the period at the end). The unfamiliar syntax will most likely be Unix specific.


The period at the end represents the current directory in both Unix and Windows.


Joanne
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
Originally posted by Joanne Neal:


The period at the end represents the current directory in both Unix and Windows.


Originally posted by sven studde:

So, the following classpath option found in the example:

javac -classpath C:\Tomcat\common\lib\servlet-api.jar:classes:.(note the dot at the end)

tells javac to look for files in the following directories:

1) C:\Tomcat\common\lib\servlet-api.jar

2) classes

3) . (the current directory)


[ October 03, 2006: Message edited by: sven studde ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Oh No. Not Classpaths AGAIN