Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError: wrong name

I wrote a java program to test RESTful web services by using Netbeans7.0.1 and it works fine there. Now I wrote the build.xml file to compile the code and when I try to run the generated .class file I always got this exception:

Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST (wrong name: clientrest/ClientREST)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ClientREST. Program will exit.

The name and path are correct, so any thoughts why I'm getting this exception?

like image 876
thinkBig Avatar asked Sep 22 '11 03:09

thinkBig


3 Answers

Exception in thread "main" java.lang.NoClassDefFoundError: ClientREST

So, you ran it as java ClientREST. It's expecting a ClientREST.class without any package.


(wrong name: clientrest/ClientREST)

Hey, the class is trying to tell you that it has a package clientrest;. You need to run it from the package root on. Go one folder up so that you're in the folder which in turn contains the clientrest folder representing the package and then execute java clientrest.ClientREST.

You should not go inside the clientrest package folder and execute java ClientREST.

like image 148
BalusC Avatar answered Oct 18 '22 16:10

BalusC


I encountered this error using command line java:

java -cp stuff/src/mypackage Test

where Test.java resides in the package mypackage.

Instead, you need to set the classpath -cp to the base folder, in this case, src, then prepend the package to the file name.

So it will end up looking like this:

java -cp stuff/src mypackage.Test

like image 40
Gaʀʀʏ Avatar answered Oct 18 '22 14:10

Gaʀʀʏ


To further note on Garry's reply: The class path is the base directory where the class itself resides. So if the class file is here -

/home/person/javastuff/classes/package1/subpackage/javaThing.class

You would need to reference the class path as follows:

/home/person/javastuff/classes

So to run from the command line, the full command would be -

java -cp /home/person/javastuff/classes package1/subpackage/javaThing

i.e. the template for the above is

java_executable -cp classpath the_class_itself_within_the_class_path

That's how I finally got mine to work without having the class path in the environment

like image 5
Slaphead Avatar answered Oct 18 '22 16:10

Slaphead