Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError when i run java file from terminal

Tags:

java

I am a java newbie. I have been using Eclipse to test a simple java class (named NewHelloWorld) and it runs fine in the console. When I try to do the same thing from a terminal, it compiles properly (creates a HelloWorld.class without giving any error) , but then java NewHelloWorld shows the following error

Exception in thread "main" java.lang.NoClassDefFoundError: NewHelloWorld (wrong name: org/kodeplay/kodejava/NewHelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: NewHelloWorld. Program will exit.

I also tried java -classpath . NewHelloWorld but that doesnt work as well giving the same error.

These are the values of the environment variables:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-6-openjdk"
CLASSPATH="/usr/lib/jvm/java-6-openjdk/lib:."

Is anything else required or am I missing anything here? Thanks

PS: using Ubuntu 10.04 desktop

like image 531
naiquevin Avatar asked Nov 29 '25 11:11

naiquevin


2 Answers

wrong name: org/kodeplay/kodejava/NewHelloWorld

cd up to the package root, so that you're in the folder containing org folder and then do

java -cp . org.kodeplay.kodejava.NewHelloWorld
like image 118
BalusC Avatar answered Dec 01 '25 02:12

BalusC


The error message gives you a clue:

(wrong name: org/kodeplay/kodejava/NewHelloWorld)

It looks like your class is called org.kodeplay.kodejava.NewHelloWorld. The Java command line needs to know the fully qualified class name:

java -cp . org.kodeplay.kodejava.NewHelloWorld

should do the trick.

like image 38
Cameron Skinner Avatar answered Dec 01 '25 01:12

Cameron Skinner