I'm trying to return to running Java commands from the command line, but I'm getting either "NoClassDefFoundError" or "Cannot read xxxxx.java file" messages when I do so. I've tried various additions to the CLASSPATH or PATH environment variables, but I still don't have it right, as I used to.
The files I want to compile/run are in C:\Documents and Settings\jsandma\workspace\SCJP\scjp
The SDK is in C:\Program Files\Java\jre1.5.0_12\bin
What do I need to do to my CLASSPATH or PATH environment variables to fix this ? I'd like to make it so that I can run Java commands from anywhere in the directory.
Current Java version:
C:\Program Files\Java\jdk1.5.0_12\bin>java -version java version "1.5.0_12" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04) Java HotSpot(TM) Client VM (build 1.5.0_12-b04, mixed mode, sharing)
Here's the Env Variables' current contents:
.; c:\sql\ASEP\3pclass.zip; c:\sql\ASEP\monclass.zip; C:\Eclipse\workspace\LossPrevention\src\com\lifeway\aspectLP; C:\Java\SCP\Java Packages; C:\j2sdk1.4.2_04\bin; C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip; C:\Documents and Settings\jsandma\workspace\SCJP\scjp; C:\Program Files\Java\jre1.5.0_12\bin; C:\Documents and Settings\jsandma\SCJP\build\classes
c:\sql\ASEP; c:\sql\OCS-12_0\dll; c:\sql\OCS-12_0\bin; C:\WINNT\SYSTEM32; C:\WINNT; C:\WINNT\SYSTEM32\WBEM; C:; C:\SQL\ADAPTIVE SERVER IQ 12\WIN; C:\SQL\ADAPTIVE SERVER IQ 12\WIN32; C:\SQL\BIN;C:\SQL\DLL; C:\DMI\WIN32\BIN; C:\sun\sdk; C:\sun\sdk\lib\ant\bin; C:\Projects\Cosmos\src; C:\Program Files\Microsoft SQL Server\80\Tools\BINN; C:\PROGRA~1\IBM\CLIENT~1; C:\PROGRA~1\IBM\CLIENT~1\Shared; C:\PROGRA~1\IBM\CLIENT~1\Emulator; c:\sql\SQLRemote\dll; C:\Program Files\QuickTime\QTSystem\; C:\Program Files\Java\jdk1.5.0_12\bin
Originally posted by Jim Sandman: The SDK is in C:\Program Files\Java\jre1.5.0_12\bin
No, it's not; that's the JRE (Java Runtime Environment), which is not the SDK / JDK.
What do I need to do to my CLASSPATH or PATH environment variables to fix this ?
You'll have to add the directory C:\Documents and Settings\jsandma\workspace\SCJP\scjp to your classpath. Note that that directory name has spaces in it, so you'll have to put it in quotes if you set it on the command line:
set CLASSPATH="C:\Documents and Settings\jsandma\workspace\SCJP\scjp"
Note that if you're setting it in the Windows environment variables settings (Start / right-click My Computer / Properties / Advanced / Environment Variables) you need to close and re-open the command prompt window after changing the environment variables there - the command prompt window does not immediately pick up the changes. [ September 27, 2007: Message edited by: Jesper Young ]
Originally posted by Jim Sandman: I've just added C:\Program Files\Java\jdk1.5.0_12\bin to CLASSPATH (it was already in PATH) and was able to compile a file while in the C:\Documents and Settings\jsandma\workspace\SCJP\scjp directory.
The CLASSPATH is what Java uses to find class files. Adding the bin directory of the JDK to the CLASSPATH has no effect.
But when running the file (java ToStringDemo) I got:
C:\Documents and Settings\jsandma\workspace\SCJP\scjp>java ToStringDemo Exception in thread "main" java.lang.NoClassDefFoundError: ToStringDemo (wrong name: scjp/ToStringDemo)
Do you have a 'package' statement in your source file?
If that's the case, then you should add the directory "C:\Documents and Settings\jsandma\workspace\SCJP" (note, leave off the last "\scjp") to your CLASSPATH, and you should compile it and run it like this:
C:\Documents and Settings\jsandma\workspace\SCJP> javac scjp\ToStringDemo.java
C:\Documents and Settings\jsandma\workspace\SCJP> java scjp.ToStringDemo