This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ClassNotFoundException at runtime 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 "ClassNotFoundException at runtime" Watch "ClassNotFoundException at runtime" New topic
Author

ClassNotFoundException at runtime

Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
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
Sheriff

Joined: Sep 28, 2004
Posts: 18138
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
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

Joined: Oct 13, 2005
Posts: 36579
    
  16
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassNotFoundException at runtime
 
Similar Threads
NoClassDefFoundError
Running Javaflow
Could not compile JTidy (jtidy-04aug2000r7-src) with JDK1.6
Could not find the main class: com.sun.tools.javac.Main.
Packaging question