I've been working on a project in Netbeans. Now I'd like to submit it and allow the markers to compile it with a script. However, I get the NoClassDefFoundError when I try to run via the command line. Even when setting the classpath to the current directory manually.
javac Main.java works fine
then calling java -classpath . Main gives:
java -classpath . Main
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: pro
ject2/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.Secur开发者_Go百科eClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
You appear to have a package named project2
. If that is intentional, you should use project2.Main
as the class name on the command line, and the classpath contain the parent of the project2
directory. If it is not intentional, remove or replace the package project2;
at the top of the source file.
The keyword here is the "wrong name" in the stack trace. You can google that and find out that there is a problem with packages.
精彩评论