views:

3517

answers:

7

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

+4  A: 

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

java -cp . myfirstjavaprog
Pace
I am so depressed that I didn't spot this.
Huguenot
Happens to the best of us.
Pace
A: 

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.
duffymo
this will be a bad idea .. because everytime he has to give the full path
lakshmanan
Why are the classpath environment variables ignored?
Huguenot
@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
This answer is like gold. However - DOS people are screwed. 255 character command-lines. Thanks again, Bill!
NDP
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
A: 

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

lakshmanan
These variables have been set, but they are being ignored for some reason. I must have some sort of syntax problem.
Huguenot
A: 

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

Thorbjørn Ravn Andersen
+2  A: 
Carl Smotricz
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
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!
Carl Smotricz
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
+1  A: 

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.

Steve B.
A: 

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.

Huguenot