java.lang.NoClassDefFoundError
我正在学习 Java,但在运行示例程序时遇到问题。
我有两个文件:
GoodDog.java:GoodDogTestDrive.java
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if (size > 60) {
System.out.println("Wooof! WoooF!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
:
class GoodDogTestDrive {
public static void main (String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one: " + one.getSize () );
System.out.println("Dog two: " + two.getSize () );
one.bark();
two.bark();
}
}
它们的键入方式与书中的方式完全相同,并且编译没有问题。当我尝试运行 GoodDogTestDrive 时,我得到以下信息:
nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class
java.lang.NoClassDefFoundError: GoodDogTestDrive/class
Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$
我做错了什么?
I'm learning Java am having trouble running an example program.
I have two files:
GoodDog.java:
class GoodDog {
private int size;
public int getSize() {
return size;
}
public void setSize(int s) {
size = s;
}
void bark() {
if (size > 60) {
System.out.println("Wooof! WoooF!");
} else if (size > 14) {
System.out.println("Ruff! Ruff!");
} else {
System.out.println("Yip! Yip!");
}
}
}
GoodDogTestDrive.java:
class GoodDogTestDrive {
public static void main (String[] args) {
GoodDog one = new GoodDog();
one.setSize(70);
GoodDog two = new GoodDog();
two.setSize(8);
System.out.println("Dog one: " + one.getSize () );
System.out.println("Dog two: " + two.getSize () );
one.bark();
two.bark();
}
}
They are typed out exactly the way they are in the book and compile without issue. When I try to run GoodDogTestDrive I get this:
nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$ java GoodDogTestDrive.class
java.lang.NoClassDefFoundError: GoodDogTestDrive/class
Exception in thread "main" nephi-shields-mac-mini:/Developer/MyProjects/GoodDog nephishields$
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不要在命令中包含
.class
:Don't include the
.class
in the command:值得注意的是,虽然这个问题的解决方案很简单(只需从 Java 命令行中删除 .class),java.lang.NoClassDefFoundError 在 Java Web 应用程序开发的上下文中很难查明。
此 Java 异常意味着无法在当前线程上下文类加载器中加载和/或找到“运行时”Java 类。这通常是由于以下原因造成的:
建议任何 Java 初学者正确理解此类问题,以应对未来的故障排除。
It is important to note that while the resolution was simple for this problem case (simply removing .class from the Java command line), java.lang.NoClassDefFoundError can be hard to pinpoint in the context of Java Web application developments.
This Java exception means that a “runtime” Java class could not be loaded and/or found in the current Thread context classloader. This is normally the results of:
It is recommended for any Java beginner to properly understand this type of problem in anticipation of future troubleshooting episodes.
只需使用“java yourClass”运行程序即可。
最后不要使用.class。
尝试一下并告诉我们。
just run the program with "java yourClass".
Do not use .class in the end.
Try it and let us know.
如果 GoodDog 类文件不在当前工作目录中,您将需要在 java 命令上设置类路径......
If the GoodDog class file is not located at the current working directory you will need to set the classpath on your java command....
问题-java.lang.NoClassDefFoundError
根本原因:环境变量部分中的 Java 路径设置不正确
解决方案:设置正确的 JAVA_HOME 路径
步骤 -> 环境变量设置(我的Comp-右键->属性->环境变量->高级选项卡->变量)
创建新的JAVA_HOME环境变量。
在 PATH 变量部分设置 JAVA_HOME 变量。
在 CLASSPATH 变量中设置 JAVA_HOME 变量
重新启动系统
验证所有变量
编译 java class javac Test.java
运行 Java 程序 java Test
Issue-java.lang.NoClassDefFoundError
Root Cause: Incorrect Java path set in Environment Variable Section
Solution: Set correct JAVA_HOME Path
Steps->Environment Variable Setting (My Comp-Right Click ->Properties->Env Variable->Advance Tab ->Variable)
Create new JAVA_HOME Environment Variable.
Set JAVA_HOME variable in PATH Variable section.
Set JAVA_HOME variable in CLASSPATH Variable
Restart System
Verify all variable
Compile java class javac Test.java
Run Java program java Test
如果 JVM 或 ClassLoader 实例尝试加载类的定义(方法调用或创建新实例)并且找不到该类的定义,则抛出该错误。
NoClassDefFoundError 的原因是特定的类在运行时不可用,但在编译时可用。
1.该类属于缺少 JAR 或 JAVA 文件或者 JAR 未添加到类路径中。
2.类在 Java 类路径中不可用。
3.由于静态初始化程序块中出现异常,Java 中出现 NoClassDefFoundError。当你的类在 static 中执行一些静态初始化时
块,如果静态块抛出异常,则该类是
在Java中引用这个类会得到NoclassDefFoundError。
4.您的Classpath、PATH或JAVA_HOME设置不正确或JDK安装不正确。可以通过重新安装JDK来解决。
5.执行maven 全新安装,然后使用必要的 Source(.java) 文件重新启动服务器。
Thrown if the JVM or a ClassLoader instance tries to load in the definition of a class (method call or creating a new instance) and no definition of the class could be found.
The reason for NoClassDefFoundError is that a particular class is not available on runtime but was available during compile time.
1.The class belongs to a missing JAR or JAVA file or JAR was not added into classpath.
2.Class is not available in Java Classpath.
3.NoClassDefFoundError in Java due to Exception in Static Initializer block. when your class perform some static initialization in static
block, and if static block throw an Exception, the class which is
referring to this class will get NoclassDefFoundError in Java.
4.Your Classpath, PATH or JAVA_HOME is not setup properly or JDK installation is not correct. which can be resolved by re-installing JDK.
5.Do a maven clean-install and simply restart your server with necessary Source(.java) files.
我遇到了和你一样的问题。
我这样解决了问题:
只需将丢失的 .class 文件复制到 eclipse 项目的 bin 目录即可解决问题。
I was facing the same problem as you.
I resolved the problem like this:
Simply copying the missing .class files to bin directory of eclipse project resolved the problem.