9

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

1

8 Answers 8

6

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

java -cp . myfirstjavaprog
1
  • I am so depressed that I didn't spot this.
    – Huguenot
    Dec 29, 2009 at 17:08
5

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

6
  • You are quite right, it turns out the problem was when I try and compile anything which imports swing components it gives exactly the same error. whereas the HelloWorld.java example is fine... This seems bizarre.
    – Huguenot
    Dec 29, 2009 at 17:43
  • 1
    Oh... note that Windows ships by default with a badly crippled Java, with executables sitting in the Windows system classpath. Please do "java -version" from a DOS prompt and see whether the version of Java you're running is the version you want! Dec 29, 2009 at 18:39
  • 1
    same thing can be said of Ubuntu; because it ships with the GNU java, (at least the couple of Ubuntu systems I've seen), not Sun's java. 'k, it's not "badly crippled" - but it's not Sun's java.
    – NDP
    Dec 29, 2009 at 23:44
  • i am upvoting b/c there is not much stuff about "setting java CLASSPATH" on google. i was under the mistaken impression that you needed to setup the CLASSPATH still... but i was wrong. Feb 2, 2011 at 23:59
  • what about JAVA_HOME... do you still need to setup that path variable? Feb 2, 2011 at 23:59
1

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

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.
5
  • this will be a bad idea .. because everytime he has to give the full path
    – Anand
    Dec 29, 2009 at 17:07
  • Why are the classpath environment variables ignored?
    – Huguenot
    Dec 29, 2009 at 17:09
  • @lakshamanan - wrong. He'll have to learn how to do it in a way that's appropriate for future projects, which aren't likely to be compiled and run on the command line.
    – duffymo
    Dec 29, 2009 at 17:25
  • This answer is like gold. However - DOS people are screwed. 255 character command-lines. Thanks again, Bill!
    – NDP
    Dec 29, 2009 at 23:19
  • The point is that nobody uses command shells to compile or run anything meaningful once they get past "Hello, World" and tutorials. It's not nearly the problem you're making it out to be. Desk top apps use scripts and Ant; deployed apps use the conventions built into them. If it was such a big deal Java would have died out years ago.
    – duffymo
    Dec 29, 2009 at 23:34
1

Use

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

It sets a lot of classpath stuff.

0

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

1
  • These variables have been set, but they are being ignored for some reason. I must have some sort of syntax problem.
    – Huguenot
    Dec 29, 2009 at 17:20
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.