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 Classpath  Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath  Problems" Watch "Classpath  Problems" New topic
Author

Classpath Problems

Joe Scholbotnick
Greenhorn

Joined: Feb 04, 2006
Posts: 4
I just downloaded and installed the JDK1.50_06 with net beans 4.1 in Windows XP on a 2.0GHz AMD-64 Machine. I decided to try to write my first java code in an editor and execute from the command line. I get the error "Exception in thread "main" java.lang.NoClassDefFoundError:". Supposedly this problem is caused by not having the CLASSPATH set correctly, so I dutifully add . and the full path to the directory where my HelloWorldApp.class file is located to the user and system CLASSPATH environment variables and reboot. The error message is the same. I decide to reboot into SuSE Linux 10.0 where I have installed the linux version of the JDK/IDE into my home directory. I add the appropriate paths to the PATH and CLASSPATH variables in bash.rc w/ export statement (This is where you set Environment Variables for individual users in SuSE 10.0)and reboot. When I try to run the app I get the same error message as in Windows. Any suggestions as to why this might be happening? Is this problem somehow caused by the IDE?

Thanks for your time.
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
1- Get rid of the CLASSPATH system environment variable. It could cause you a lot of problems.
2- When you compile using javac, set the class path with -classpath command-line argument. From the command prompt:
> javac -classpath someFolder;someJar;someZip SomeClass.java

By the way, if you don't specify the -classpath command-line argument and there is NO CLASSPATH environment variable set, then the class path is the current working directory. Exactly like doing:
> javac -classpath . SomeClass.java

Regards
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
please see this post.
Joe Scholbotnick
Greenhorn

Joined: Feb 04, 2006
Posts: 4
Thanks for the suggestion Jean-Francois, but I am still having the same problem. As you suggested got rid of the system CLASSPATH environment variable.

My environment variables are currently set as follows:
user CLASSPATH {.;C:\1java}
user PATH {C:\Program Files\Java\jdk1.5.0_06\bin}
system CLASSPATH - deleted
system Path {SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;c:\Python22;C:\Program Files\PC-Doctor for Windows\services;c:\matlab_sv13\bin\win32;C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.5.0_06\bin}
the system Path is just that Path, not PATH I assume that is correct?

my HelloWorldApp.java and HelloWorldApp.class are in C:\1java\
my windows java installation is in C:\Program Files\Java\jdk1.5.0_06\bin\

In order to eliminate the possibility that the code was incorrect I downloaded the example file from java.sun.com

First I compiled using :
C:\1java>javac HelloWorldApp.java
and then attempted to run using
C:\1java>java HelloWorldApp.class
only to receive "Exception in thread "main" java.lang.NoClassDefFoundError:"

I deleted the .class file and tried to compile using
C:\1java>javac -classpath . HelloWorldApp.java
and then try to run, same error

I deleted the .class file and tried to compile using
C:\1java>javac -classpath C:\1java HelloWorldApp.java
and then try to run, same error

Some other potentially useful information.
There are two older jre installations in the Java folder
jre1.4.1_02 and jre1.4.2
There is a system environment variable QTJAVA {C:\Program Files\Java\j2re1.4.2\lib\ext\QTJava.zip} (which I assume has to do with Quicktime)
I installed jdk1.5.0_06 with the netbeans 4.1 bundle.
I am running on an AMD Athalon 64 processor.
I am running Windows XP Home Edition 2002 version SP2 or SuSE 10.0 Linux

My suspicion is that this problem is a side effect of installing the IDE. I may try to uninstall the bundle and re-install without netbeans, but since I am on dialup downloading is six hour long process and won't happen immediately.

I am still having the exact same problem in my Linux installation.

I am going to try to learn how to use Netbeans to see if I can run code from there, but I was hoping to avoid having to learn how to use the IDE at the same time as learning the language. The first page of the netbeans manual does say "readers of this guide are assumed to have at least a basic understanding of the Java programming language and related technologies."
which doesn't excite me considering I am at the HelloWorldApp stage.

Thanks again for your help.
Joe Scholbotnick
Greenhorn

Joined: Feb 04, 2006
Posts: 4
Thanks for the link wise ou. The respondant to this post does seem to be saying the same thing that Jean-Francois Briere did about setting the classpath manually. Still the fact that the original poster was able to run in the IDE (Eclipse in his case) does suggest that it may be related to both his and my problems.
Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101
In a Windows XP command prompt, please do the following:
> javac -version

Then do:
> java -version

Are the versions the same?

Also, when you run the program, please also use the -classpath command-line argument (exactly like javac):
> java -classpath c:\1java HelloWorldApp

By the way, you didn't get rid entirely of CLASSPATH (user environment variable).

Regards
Joe Scholbotnick
Greenhorn

Joined: Feb 04, 2006
Posts: 4
Jean-Francois, thanks for the suggestions, but I figured out the cause of the problem.

I went back and re-read the tutorial for the eighth time and noticed what I have been missing.

It seems I am the victim of the tab key. I am so used to using tab to auto complete in the linux bash shell that it didn't occur to me that when I type

C:\1java>java He (Tab)
and get
C:\1java>java HelloWorldApp.class
that It wouldn't run properly if I just hit enter.

C:\1java>java HelloWorldApp

without .class does the trick

in linux if you type $tar -zxvf He (tab) to unpack the tarball HelloWorldApp.tar the bash shell knows that you want HelloWorldApp.tar and not HelloWorldApp.rpm which happens to be in the same directory and autocompletes with the correct file for the command you typed. It doesn't surprise me that this misfires in windows, but it does surprise me that it happened in linux too.

Thanks again for the help, sorry for wasting your time. FWIW I did try to do my homework before posting here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath Problems
 
Similar Threads
Cannot Find javac
Having Trouble Compiling
Getting compile error for AdviceClient.java from Head First EJB
Intermediate/Advanced Java Users. Pl help with static profilers (PMD)
JRE problem.