aspose file tools*
The moose likes Beginning Java and the fly likes Classpath issue? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath issue?" Watch "Classpath issue?" New topic
Author

Classpath issue?

Randall W. Smith
Greenhorn

Joined: Jul 28, 2001
Posts: 9
The following code is from the Bruce Eckel book Thinking In Java. If I rem the package statement the code will run which tells me I probably have a problem with my classpath.
I don't know what I'm doing wrong. Here's my classpath:
classpath=.;c:\java
I created an "c08" sub under Java and placed the file Month2.java there. It will compile but if I don't rem the package statement I get the crazy errors. Running jdk 1.3 in a c:\jdk sub.
Here's the code:

[This message has been edited by Cindy Glass (edited July 29, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

rem the package statement

WHAT does that mean?


"JavaRanch, where the deer and the Certified play" - David O'Meara
Randall W. Smith
Greenhorn

Joined: Jul 28, 2001
Posts: 9
//package
I commented it out so it wouldn't take effect.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
I am going to take a guess that you mean remove or comment out the package statement.
Lets get this straight.
The class file for Month2 is sitting in c:/java/c08/Month2.java .
It has a package statement in it saying that the package is in c08. This is to allow a class file sitting in the class path (c:/java) to be able to find it by using import co8.Month2 .
You don't mention where YOU are sitting while you are testing this. If you are sitting in c:/java and try
>java Month2
of course the system can't find it for you because it has to FIND it before it can read that package statement.
If you are sitting in the c08 sub-directory, the dot reference in you classpath will include the c08 directory as part of the classpath. This is good. So now the system can FIND the class file. Then it reads it and sees that it is supposed to be in a c08 sub-directory of the classpath directory - which is c08 (because you are sitting in it).
So it is looking for c:/java/c08/c08 and can't find it. It gets confused. I would guess when you read those error messages you will understand its confusion. When you get rid of the package statement then it just looks in the current directory and all is good.
Whatever class that you are calling to invoke your app needs to be in the classpath not in a sub-directory (or package). The classes that it USES can be in other packages.

[This message has been edited by Cindy Glass (edited July 30, 2001).]
Randall W. Smith
Greenhorn

Joined: Jul 28, 2001
Posts: 9
You are correct about the //. That will comment out the package statement. That is what I meant.
As for the import statement. Give a classpath that includes c:\java, wouldn't the proper import statement be:
import c08.*;
or
import c08.Month2;
???

I am running the file in the c08 directory. That may be my problem. I took the . out of my classpath for a test but that didn't work either. Uhmmm... frustrating these classpath issues.
Randall W. Smith
Greenhorn

Joined: Jul 28, 2001
Posts: 9
Is it possible that I'm not finding the default java packages if I specify a package?

Maybe this error listing will help:
C:\java\c08>java Month2
Exception in thread "main" java.lang.NoClassDefFoundError: Month2 (wrong name: c08/Month2)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yeah you were correct about the import statement, I fixed it above.
You error message just says that since c08 is in the classpath since you are invoking java from there, then "package c08;" is incorrect because it is NOT in a directory down from there. There is no c:/java/c08/c08/Month2.
Go back and read the point about not putting the driver class in a package below the classpath. It won't work.
If you want Month2 to be in that package - fine. Just pull the main method out and put it in a DIFFERENT class that sits in the c:/java directory.

then get into the c:/java directory and type
>java Driver
Driver will then be able to find Month2 in it's proper package.
Randall W. Smith
Greenhorn

Joined: Jul 28, 2001
Posts: 9
Hey that worked! Sort of.
I was able to put the Driver.java method as you suggested up one level which put in a directory that was in my classpath and I could then use
import c08.Month2;
or

import c08.*;
I could also move the file to a directory other than my class path and it worked.
I'm still not sure why it works other than I'll be sure NOT to create a "packaged" object from the same file because that would mean I'd be in the same directory. If . is in the classpath it will look for that import reference to be off the current directory or "." which I'd be in. Hey... that's it isn't it? If I put the Driver.java in c08 it would fail just as if I had left the main() in place because it would be looking for c08 off of the "." directory! I know... you've been trying to tell me that for some time now... I think...
In any case, thank you very much Cindy Glass for helping me.
Randall
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath issue?