Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFoundException at runtime

 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



C:\JAVA\MyApps>javac Atest.java

C:\JAVA\MyApps>java Atest
Exception in thread "main" java.lang.NoClassDefFoundError: Atest
Caused by: java.lang.ClassNotFoundException: Atest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: Atest. Program will exit.



C:\JAVA\lib>javac -version
javac 1.6.0_21


CLASSPATH=C:\JAVA\LIB


C:\JAVA\lib>DIR
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





 
Henry Wong
author
Marshal
Pie
Posts: 21391
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You forgot to add the current directory to the classpath. Without it, the JVM can't find the class file that the java compiler just created.

Henry
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

javac -cp SomeLibrary.jar;. MyProgram.java

java -cp SomeLibrary.jar;. MyProgram
 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
javac Atest.java
java Atest

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?

Thanks again, I appreciate your help!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49761
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic