emanuele
emanuele

Reputation: 2589

yet another: Exception in thread "main" java.lang.NoClassDefFoundError

I have a common problem but the common solutions seems don't work for me.

I have a file called test.java in my current directory with others class files in the myclasses directory. Namely if i type

ls:
myclasses  test.java

I type:

  javac test.java

and all compile fine.

When i try to execute test.class and then type:

  java test

i get:

Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: myclasses/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: test.  Program will exit.

I guess that this means that java can't locate the test.class file. But why? the file is in the current directory and all classes are in the myclasses directory. I have tried different combinations of flags (-cp , -d -sourcepath) and i have moved the test.class around the directories.

Where is the mistake?

Upvotes: 0

Views: 1140

Answers (3)

James Raitsev
James Raitsev

Reputation: 96391

Make sure that test.java contains public static void main method.

Also, if your class is inside myclasses, you should java myclasses.test, i believe

Upvotes: 2

sigito
sigito

Reputation: 96

You should specify classpath for searching the Class.

java -cp . test

Upvotes: 0

fredo
fredo

Reputation: 325

In addition to the above, make sure there is no package name (myclasses) declared in your .java file.

Upvotes: 0

Related Questions