I am getting the error
Exception in thread "main" java.lang.NoClassDefFoundError:
When I try and run a compiled class on Ubuntu. I am using a very simple Helloworld example, and the millions of responses which already exist on the internet suggest that my CLASSPATH and JAVA_HOME variables have been incorrectly set.
However, I have edited the etc/environment to the correct folders as well as the current folder:
PATH=".:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-1.5.0-sun/"
CLASSPATH=".:/usr/lib/jvm/java-1.5.0-sun/lib"
and they appear when I type the set command. In any case, even when I set the classpath manually using
sudo java -cp . myfirstjavaprog.class
I get the same error. Where else should I look? This must be a configuration problem.
Many thanks
You want to drop the .class from the end. Just type...
java -cp . myfirstjavaprog
I strongly recommend getting rid of the CLASSPATH environment variable, or at least taking your JRE/JDK out of it.
"."
is implicitly in the classpath unless otherwise specified. And since about Java 1.3, Java has been smart enough to find its own runtime and libraries based on the execution path of the javac/java executables. Since then, it's been redundant, if not outright wrong, to specify those on the classpath. Certainly .../lib
is incorrect, as there are only jars there, no classes, and those aren't picked up off the classpath if they're not individually and explicitly named.
Modern javas are smart enough that you can just type java <classname>
when standing in the root directory of the classpath, and it will Just Work™.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With