Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up java classpath and java_home correctly in Ubuntu

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

like image 890
Huguenot Avatar asked Dec 29 '09 17:12

Huguenot


2 Answers

You want to drop the .class from the end. Just type...

java -cp . myfirstjavaprog
like image 156
Pace Avatar answered Oct 14 '22 02:10

Pace


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™.

like image 29
Carl Smotricz Avatar answered Oct 14 '22 04:10

Carl Smotricz