Any idea why this will compile fine but will not run? The classpath and code are below. 7 hours on this so far and am not making any progress.
Exception in thread "main" java.lang.NoClassDefFoundError: Atest
Caused by: java.lang.ClassNotFoundException: Atest
at java.security.AccessController.doPrivileged(Native Method)
Could not find the main class: Atest. Program will exit.
Volume in drive C has no label.
Volume Serial Number is 4449-5A9A
Directory of C:\JAVA\lib
11/19/2010 11:40 AM <DIR> .
11/19/2010 11:40 AM <DIR> ..
09/24/2010 02:05 PM <DIR> asjava
11/19/2010 11:39 AM 14,817,259 ct.sym
11/19/2010 11:39 AM 145,442 dt.jar
11/19/2010 11:39 AM 218,066 htmlconverter.jar
11/19/2010 11:39 AM 18,396 ir.idl
11/19/2010 11:39 AM 1,692 jawt.lib
11/19/2010 11:39 AM 388,074 jconsole.jar
11/19/2010 11:39 AM 296,524 jvm.lib
11/19/2010 11:39 AM 429 orb.idl
11/19/2010 11:40 AM 12,636,184 tools.jar
11/19/2010 11:40 AM <DIR> visualvm
9 File(s) 28,522,066 bytes
4 Dir(s) 58,107,191,296 bytes free
Why did you add C:\JAVA\LIB to your classpath? Note that Java will look in that directory for *.class files - it will not automatically pick up *.jar files from the directories in the classpath. Also, if this is the lib directory of your JDK - it is not necessary to add that to the classpath.
It is best to not set your CLASSPATH environment variable at all. So, delete the variable. Note that you need to close and re-open the Windows command prompt after deleting the variable.
If you need to specify a classpath (for example, because you need to include JAR files), it's best to do so on the command line, using the "-cp" switch. You should also include the current directory "." if you do that. For example:
Thanks for the feedback. I deleted the CLASSPATH environment variable, moved the asjava directory that holds the classes I am trying to use to my current directory, and compiled/ran successfully with just:
So, is there a more better way to structure a project that includes another persons classes? Is it best practice to put them into in my current working directory? I was attempting to isolate them into a common shared location (c:\java\lib, probably not the best choice after all).
Also, when is it proper to set the CLASSPATH environment variable, if ever, and use it instead of using the command line options at compile/run time?
It is only worthwhile setting a CLASSPATH environment variable when some other application sets it; I have been told (but am not certain) that QuickTime sets a CLASSPATH. If that happens, you may lose access to your "current directory", and ought to amend the CLASSPATH by adding . as a single additional value.
Gil Steidle wrote:So, is there a more better way to structure a project that includes another persons classes? Is it best practice to put them into in my current working directory? I was attempting to isolate them into a common shared location (c:\java\lib, probably not the best choice after all).
Don't put your source code or other files in the JDK installation directory (or a subdirectory). You don't want to mess up the JDK directory with your own files. Put them in a separate directory somewhere.
If you start a project and need to organise your source code, compiled classes etc. then at some point you don't want to manage all of that manually. There are tools like Ant and Maven to manage your project directory and the build process. You could also use an IDE such as NetBeans or Eclipse, that will manage all of this for you.