为什么错误名称与NoClassDefFoundError [英] why wrong name with NoClassDefFoundError
问题描述
我在文件夹 UtilityPack
中创建了一个 List.java
文件,其中包含此代码
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){}
}
/ p>
To compile i did
C:\UtilityPack>javac List.java
但是当我尝试运行
C:\UtilityPack>java -classpath . List
或
C:\UtilityPack>java List
我收到错误
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)
我一直在尝试执行这个程序从最近3个小时,但没有任何工作。请帮助
I have been trying to execute this program from last 3 hours but nothing worked..please help
推荐答案
您需要完全合格名称例如
You need the fully qualified name e.g.
java -cp . Utilities.List
ie。你正在告诉JVM从一个类 Utilities.List
的当前直接( -cp。
)中查看,在文件 Utilities\List.class
中将会期待。
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
.
为了更一致,您应该将.java文件在实用程序
目录下(是 - 这是tautologous - 包指定这个,但它是一贯的做法)。
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).
我也会避免调用你的课程 List
。在某些阶段,您将导入一个 java.util.List
,这一切都会令人困惑!
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!
最后,一旦您获得了几个课程,请调查 ant 或另一个构建工具,并单独您的来源
和目标
目录。
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.
这篇关于为什么错误名称与NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!