why wrong name with NoClassDefFoundError

混江龙づ霸主 提交于 2019-12-30 15:02:28

问题


I created a List.java file in folder UtilityPack which contains this code

package Utilities;
public class List
{
    private class node{}
    public void insert(int data){}
    public void print(){}
    public static void main(String[] s){}
}

To compile i did

C:\UtilityPack>javac List.java

But when I try to run with

C:\UtilityPack>java -classpath . List

OR

C:\UtilityPack>java List

I get error

Exception in thread "main" java.lang.NoClassDefFoundError: List (wrong name: Uti
lities/List)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
2)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

I have been trying to execute this program from last 3 hours but nothing worked..please help


回答1:


You need the fully qualified name e.g.

java -cp . Utilities.List

i.e. you're telling the JVM to look from the current direct (-cp .) for a class Utilities.List, which it will expect in the file Utilities\List.class.

To be more consistent you should put the .java file under a Utilities directory (yes - this is tautologous - the package specifies this, but it's consistent practise).

I would also avoid calling your class List. At some stage you're going to import a java.util.List and it'll all get very confusing!

Finally, as soon as you get more than a couple of classes, investigate ant or another build tool, and separate your source and target directories.




回答2:


Use the complete name of the class to lauch your program :

 java Utilities.List

But the folder name should also match the package name.




回答3:


Your directory structure needs to follow your Java package pathing. IOW, if the class Listis in the package Utilities, you need to situate it in a directory called Utilities, which should be at the root level of your project, i.e. the path of the source file should be C:\UtilityPack\Utilities\List.java. When you are in C:\UtilityPack (project root), you compile and run List by referencing it as Utilities.List.

You might also consider using Eclipse, it will prevent this sort of things from happening, or any other Java IDE.



来源:https://stackoverflow.com/questions/14221045/why-wrong-name-with-noclassdeffounderror

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!