I am working on a java project in NetBeans and it runs perfectly when I press the run project button. However, I need to run the program from the terminal. I opened up the directory which contains the class file, search.class, and tried running it with
java search
However I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: search (wrong name: search/search)
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(Launche开发者_开发技巧r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I cannot for the life of me figure out why it says "wrong name" since search.class exists in that directory. My only thought is that there are .jar files which I call from my program (they are in that same directory) and perhaps I need to be including them at runtime?
Any help would be greatly appreciated!
EDIT: So it turns out that it probably was a problem with not including the .jar files. I fixed it by going into the dist/ folder where the entire project is packaged as a jar and running it as java -jar search.jar
Thanks for all the feedback!
If your class is in package, then you must pass the full name of the class.
package org.example;
public class Search {
...
}
You will have to run it as
java org.example.Search
The current directory must contain org
subdirectory, which in its turn contains example
directory where Search.class
is located. You can use -cp
parameter to specify where your class files are located.
The classical reason for NoClassDefFoundError is an exception arising out of the class's static initializer.
精彩评论