Main Content

Finding your java program - the CLASSPATH variable

Archive - Originally posted on "The Horse's Mouth" - 2009-04-02 17:02:23 - Graham Ellis

When you run a Java 'program' (strictly, the main method of a class), it looks for that class in a file with the name given and with a .class extension in the places listed in the CLASSPATH environment variable. Failure to have the right CLASSPATH set results in a spectacular series of error messages!

[trainee@easterton ~]$ java Nother
Exception in thread "main" java.lang.NoClassDefFoundError: Nother
Caused by: java.lang.ClassNotFoundException: Nother
  at java.net.URLClassLoader.run(URLClassLoader.java:200)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:289)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
[trainee@easterton ~]$ export CLASSPATH=/home/trainee/a9tom
[trainee@easterton ~]$ java Nother
*** I love Trowbridge ***
*** I am a bit crazy ***
*** 1 => 2 ***
[trainee@easterton ~]$


Note:

CLASSPATH may include a series of : separated directories (; on windows)

CLASSPATH may contain the names of jar files, in which case the class will run straight from the jar