3

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

Damien
  • 1,161
  • 2
  • 19
  • 31
naiquevin
  • 7,588
  • 12
  • 53
  • 62

6 Answers6

9

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
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

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.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • http://meta.stackexchange.com/questions/15775/do-you-delete-your-own-answer-when-its-a-duplicate – BalusC Dec 17 '10 at 12:34
  • 1
    @BalusC: There is a difference between an *answer* and an *explanation*. The bit about "Java command line needs to know the fully qualified class name" is an explanation. I don't consider this to be a duplicate. – Cameron Skinner Dec 17 '10 at 12:37
1

Go to the package root directory (the parent directory of org) and run:

java -cp .:$CLASSPATH org.kodeplay.kodejava.NewHelloWorld

Also I wouldn't put . to my CLASSPATH permanently (in .bashrc, .bash_profile or /etc/profile) it may lead to undesired behavior.

khachik
  • 28,112
  • 9
  • 59
  • 94
0

cd up to the root package. Most of the cases it will be src folder in the Project if created from eclipse IDE.

java -cp . org.kodeplay.kodejava.NewHelloWorld should work

java org.kodeplay.kodejava.NewHelloWorld should also work. I tried both the things and it works fine in both the case

carols10cents
  • 6,943
  • 7
  • 39
  • 56
Chirag Pahuja
  • 31
  • 1
  • 6
0

I had a similar problem running a HelloWorld program I had written with a text editor on Mac OS X. It ran fine on a remote Linux box, but running it from home directory I got the dreaded NoClassDefFoundError.

Found that I could fix it either by running as:

java -cp . HelloWorld

or, without the classpath qualifier, after adding the current directory to my bash CLASSPATH for the current session:

export CLASSPATH=.
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
David
  • 1,018
  • 13
  • 22
0

FOR MAC USERS : java -classpath /Users/apple/IdeaProjects/Folder_name/out/production/folder_Name com.Package_name.class_name

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 15 '22 at 08:34