For newbie, this problem is almost common in java. While compile java using javac it was successful. But whenever you execute the compiled file it fails with "Exception in thread "main" java.lang.NoClassDefFoundError". as below.
E:\Documents and Settings\Arju\Desktop\Rotimi New\Java>java Palindrome.class
Exception in thread "main" java.lang.NoClassDefFoundError: Palindrome/class
Caused by: java.lang.ClassNotFoundException: Palindrome.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Palindrome.class. Program will exit.
The solution is add -classpath and a dot (.) which means, that it will search for classes bytecode in the current directory. Like below.
E:\Documents and Settings\Arju\Desktop\Rotimi New\Java>java -classpath . Palindrome
Enter number
345
Number:
345
After reversing the number:
543
Number is not palindrome!
Also note that, while executing if you add .class extension then similar error will throw. So while executing simply write,
E:\Documents and Settings\Arju\Desktop\Rotimi New\Java>java Palindrome
Enter number
3
Number:
3
After reversing the number:
3
Number is palindrome!
And adding .class will throw exception as below.
E:\Documents and Settings\Arju\Desktop\Rotimi New\Java>java Palindrome.class
Exception in thread "main" java.lang.NoClassDefFoundError: Palindrome/class
Caused by: java.lang.ClassNotFoundException: Palindrome.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Palindrome.class. Program will exit.
No comments:
Post a Comment