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 Basic problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic problem" Watch "Basic problem" New topic
Author

Basic problem

Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
It's a quite a time that I didn't code in java, and now it seems like i can't make a HelloWorld.
I get always the following error
java.lang.NoClassDefFoundError: test
Exception in thread "main"

Here is the code

Then i do
javac test.java <=== No problem
java test<=== java.lang.NoClassDefFoundError: test
Exception in thread "main"
It happened to me sometimes and the problem escaped with a bat file. Could somebody explain me what is wrong?
Thx a lot


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Make sure that the path to test.class is in your classpath.

for example, C:\java\...\classes\test.class should be in your classpath as:

CLASSPATH=C:\java\...\classes\


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Thx Marylin for your answer, with my classpath set properly, it works. Shall i do that for all my applications? I mean, if I install an application on another pc I will have to set his class and classpath?
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
If he did javac test.java with no problem, I assume test.class ended up in the same directory as test.java and in the same directory which he ran javac.
When you run "java test" won't it check the current directory first for the class files before checking the CLASSPATH?
I write a lot of test programs and I can compile/run them without explicitly adding each one to the CLASSPATH so I'm just confused.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
When you run "java test" won't it check the current directory first for the class files before checking the CLASSPATH?

No, you must have the current directory (".") in your CLASSPATH.
For example, my CLASSPATH starts with ".;%JAVA_HOME%\jre\lib\rt.jar....."


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by David Duran:
If he did javac test.java with no problem, I assume test.class ended up in the same directory as test.java and in the same directory which he ran javac.
When you run "java test" won't it check the current directory first for the class files before checking the CLASSPATH?
I write a lot of test programs and I can compile/run them without explicitly adding each one to the CLASSPATH so I'm just confused.

If javac is in his PATH (and I think nowadays it adds itself to the PATH), he can compile from any subdir. He should be able to "java" (run) the class from the subdir containing the *.class file he is trying to run.

Supposedly the current directory (".") is in the CLASSPATH by default. However, in my opinion, it is better if you specify the CLASSPATH to contain the subdir with the *.class files in them -- at least until you get to more advanced levels of packages and jars. You can put all your *.class files into the same subdir for a while so you don't have to keep changing/adding to your classpath. Until you get to a big project. But by then, you'll know about other options.

I have most of my beginning java source files as well as my compiled *.class files in the same directory. C:\java
So my classpath was very simple.
CLASSPATH=C:\java
because everything was there.
[ November 26, 2002: Message edited by: Marilyn de Queiroz ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Younes Essouabni:
Thx Marilyn for your answer, with my classpath set properly, it works. Shall i do that for all my applications? I mean, if I install an application on another pc I will have to set his class and classpath?

If you install your app on another PC you will need to be sure that PC has at least the JRE installed and that the CLASSPATH environment variable points to your class.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Thx david for your answer, it is exactly what confused me. I mean, I've done a lot of code, and I've never take care about the classpath, I set only the path to the javac.exe. Since I was in the directory containing the .class, I don't understand why the classpath shall be set. and why, doesn't the problem appear when I use a .bat?
Your classpath is set to c:/java, if you have some code in c:/java/myappz/*.java do you have to add an extension to your classpath or does it check the subdirectories?
Anyway thx for your answers, my classpath is set correctly now.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Originally posted by Blake Minghelli:

No, you must have the current directory (".") in your CLASSPATH.
For example, my CLASSPATH starts with ".;%JAVA_HOME%\jre\lib\rt.jar....."

Great, I think that it is a good solution.
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic problem