Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic problem

 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....."
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic