9
votes

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

8

8 Answers

6
votes

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

java -cp . myfirstjavaprog
5
votes

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

1
votes

You are mixing apples and oranges. A raw java or javac invocation on the command line needs a classpath to know where it can access its classes. When you run

java -cp pathelement1:pathelement2... MyClass

you're giving java the list of places to find runnable classes. It's not going to look anywhere else, including ".", unless you tell it to. So "CLASSPATH" doesn't help you unless you run

java -cp $CLASSPATH MyClass

Inotherwords, its just a shortcut to keep having to retype the classpath.

Many programs are configured to use JAVA_HOME, but ultimately running java programs just need a configured classpath and the path to java (they find it through the JAVA_HOME variable, so you still need it for things like ant, but its' still conceptually just a shortcut as well).

your PATH is the path where the system looks for binaries. If java is not on your path (type "which java", it will show you which, if any, java is on your path) running /full/path/to/java is identical to just running "java" and having the system find the binary in the PATH variable.

1
votes

No, I think it's that CLASSPATH environment variables are ignored.

The right way to do it is to use the -classpath option when you compile and run. Set it for each and every project. The evidence you have before your eyes tells you it's so.

Why is CLASSPATH ignored? Several reasons:

  1. It's a Java 1.0 artifact that's fallen out of favor.
  2. The JVM has no guarantee that you've set one as an environment variable.
  3. IDEs have their own requirements, so they don't rely on it.
  4. Java EE app servers have their own requirements, so they don't rely on it.
  5. You have to give the whole path every time because every project is likely to be different. Once you progress past "Hello, World" you'll find yourself scripting it or using tools like Ant and Maven that will help you set the CLASSPATH for your project.
1
votes

Use

sudo update-java-alternatives -s java-6-openjdk

It sets a lot of classpath stuff.

0
votes

for setting java_home variable, here are instructions.

http://luckydev07.blogspot.com/2009/08/setting-javahome-in-ubuntu-linux.html

and

classpath can be set similarly

0
votes

I would strongly recommend you spend some time looking on the Sun tutorial. It will help you later - class paths are notorious trouble makers.

http://java.sun.com/docs/books/tutorial/getStarted/TOC.html

0
votes

Ok I was looking for the problem in the wrong place. It turned out that Java was fine and I was the victim of getting the same error for two separate problems.

I was originally trying to run a swing example which I got from the Java website, but I hadn't noticed that it had a package definition. I've set up the correct folder structure and now it runs fine.

When I tried to run a HelloWorld example, I had accidentally included the .class extension.

Both of these problems gave me ClassNotFound errors.

Thank you very much for all your help.