File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problems with the java command and -classpath concerning the current (.) directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with the java command and -classpath concerning the current (.) directory" Watch "Problems with the java command and -classpath concerning the current (.) directory" New topic
Author

Problems with the java command and -classpath concerning the current (.) directory

Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi,

when I try to run a file from the console (vi-editor) typing
java MyClass,
it works fine.

Instead, when I add a "-classpath" to the java command, I always get a NoClassDefFoundError, although I include the currrent directory in the classpath:
java -classpath .:subdir MyClass

I'm wondering why, because the current directory is always present, so why does the JVM not find my class file?
Same problem with the windows console, I've even tried out ";" instead of ":" as delimiter.

Any ideas?


SCJP 6 (88%), SCWCD (89%)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30918
    
158

Ulrich,
Could there be something in that classpath that is breaking it?

Does this work? java -classpath . MyClass

If so, try adding one directory at a time to the classpath.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Moving to Beginning Java.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi Jeanne,

thank you for your reply!
Yes, indeed:

java -classpath . MyClass

works out fine, but as I add only one valid subdirectory (-classpath .:subdir), I always get the same error message!
I'm not sure if this problem is related to the underlying OS.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Hi Urlich,

what is the name of 'notfound' class displayed in the stacktrace, is it MyClass or maybe some other class ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please show the complete file hierarchy, indicate from where you are running the command, and which OS you're actually using.
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi,

attached please find a screenshot of my different commands (java versus java -xlasspath) - so you can see what the exact error message is.
I run the java command from the dev directory, and x is a valid subdirectory of dev.
My OS is Windows Vista, and as I've written, I use Cygwin in order to emulate Unix.

Same problem when I use the Windows console (instead of Cygwin).



[Thumbnail for classpath.jpg]

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ugh; please don't use screenshots to capture textual information. Just text is fine.
as I've written, I use Cygwin in order to emulate Unix.

First time Cygwin's been mentioned in the thread, and it matters.

Are you using the Windows version of Java, or a Cygwin version? If Windows, in Cygwin, you still need to use a semi-colon as the path separator. And, because it's Unix-like, you need to escape the semicolon in the path.
Ulrich Vormbrock
Ranch Hand

Joined: Apr 15, 2010
Posts: 73
Hi David,

sorry for that and thank you very much for your hint concerning escaping the semicolon.

Now, I've typed
$ java -classpath .\;x SerializeTest

and it works out fine.
Besides, I use the Windows version of java ...

Cheers!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Glad to hear you got it worked out :) Yeah, that's kind of a pain, but for me the Windows version of Java is the most reliable and has the least bugs--I do the same thing sometimes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with the java command and -classpath concerning the current (.) directory