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

Community
  • 1
  • 1
Huguenot
  • 2,427
  • 2
  • 17
  • 14

8 Answers8

6

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

java -cp . myfirstjavaprog
Pace
  • 41,875
  • 13
  • 113
  • 156
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™.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • 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 '09 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! – Carl Smotricz Dec 29 '09 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 '09 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. – Trevor Boyd Smith Feb 02 '11 at 23:59
  • what about JAVA_HOME... do you still need to setup that path variable? – Trevor Boyd Smith Feb 02 '11 at 23:59
  • @Trevor: There's a few tools that like to see it; I seem to remember Tomcat and ant being among those. But even those manage to do without, and in the "normal" case you shouldn't need it. – Carl Smotricz Feb 04 '11 at 07:05
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.
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • this will be a bad idea .. because everytime he has to give the full path – Anand Dec 29 '09 at 17:07
  • Why are the classpath environment variables ignored? – Huguenot Dec 29 '09 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 '09 at 17:25
  • This answer is like gold. However - DOS people are screwed. 255 character command-lines. Thanks again, Bill! – NDP Dec 29 '09 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 '09 at 23:34
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.

Steve B.
  • 55,454
  • 12
  • 93
  • 132
1

Use

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

It sets a lot of classpath stuff.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
haasdas
  • 11
  • 1
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

Anand
  • 10,310
  • 24
  • 90
  • 135
  • These variables have been set, but they are being ignored for some reason. I must have some sort of syntax problem. – Huguenot Dec 29 '09 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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
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.

Huguenot
  • 2,427
  • 2
  • 17
  • 14