I had recently reinstalled my system and was trying to run a simple class that consisted
of a “Hello World” program in Java. I received the following:
Compile:
$> javac LottoMain.java
Run:
$> java LottoMain
Exception in thread “main” java.lang.NoClassDefFoundError: LottoMain
Caused by: java.lang.ClassNotFoundException: LottoMain
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: LottoMain. Program will exit.
The compile line completed without errrors and the LottoMain.class file was there, but
what I didn’t know was that I no longer had the current directory “.” in my CLASSPATH variable.
After adding “.” to my CLASSPATH variable the run command gave me what I was expecting.
Run:
$> java LottoMain
Hello Eric!
Updating the CLASSPATH variable in Bash:
in my home directory/.bashrc file
export CLASSPATH=$CLASSPATH:.