java.lang.NoClassDefFoundError

发布于 2024-10-30 18:52:39 字数 1248 浏览 0 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

庆幸我还是我 2024-11-06 18:52:39

不要在命令中包含 .class

java GoodDogTestDrive

Don't include the .class in the command:

java GoodDogTestDrive
星軌x 2024-11-06 18:52:39

值得注意的是,虽然这个问题的解决方案很简单(只需从 Java 命令行中删除 .class),java.lang.NoClassDefFoundError 在 Java Web 应用程序开发的上下文中很难查明。

此 Java 异常意味着无法在当前线程上下文类加载器中加载和/或找到“运行时”Java 类。这通常是由于以下原因造成的:

  • 运行时类路径中缺少库。
  • 静态 {} 块代码初始化程序执行失败(阻止受影响类的类加载)。
  • JAR 文件打包/类加载器树问题,例如部署在子级和子级的 JAR 文件混合使用父类加载器。

建议任何 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:

  • Missing library in your runtime classpath.
  • Static {} block code initializer execution failure (preventing class loading of the affected class).
  • JAR file packaging / class loader tree problems such as mix of JAR files deployed at the child & parent class loader.

It is recommended for any Java beginner to properly understand this type of problem in anticipation of future troubleshooting episodes.

怂人 2024-11-06 18:52:39

只需使用“java yourClass”运行程序即可。

最后不要使用.class。

尝试一下并告诉我们。

just run the program with "java yourClass".

Do not use .class in the end.

Try it and let us know.

郁金香雨 2024-11-06 18:52:39

如果 GoodDog 类文件不在当前工作目录中,您将需要在 java 命令上设置类路径......

java GoodDogTestDrive -cp ./path/to/gooddog/

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 GoodDogTestDrive -cp ./path/to/gooddog/
萝莉病 2024-11-06 18:52:39

问题-java.lang.NoClassDefFoundError

根本原因:环境变量部分中的 Java 路径设置不正确

解决方案:设置正确的 JAVA_HOME 路径

步骤 -> 环境变量设置(我的Comp-右键->属性->环境变量->高级选项卡->变量)

  1. 创建新的JAVA_HOME环境变量。

    JAVA_HOME **.;C:\Program Files (x86)\Java\jdk1.6.0_14**
    
  2. 在 PATH 变量部分设置 JAVA_HOME 变量。

    路径%JAVA_HOME%\bin
    
  3. 在 CLASSPATH 变量中设置 JAVA_HOME 变量

    CLASSPATH %JAVA_HOME%\jre\lib
    
  4. 重新启动系统

  5. 验证所有变量

    echo %CLASSPATH%
    
    回显%JAVA_HOME%
    
    回显%路径%
    
  6. 编译 java class javac Test.java

  7. 运行 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)

  1. Create new JAVA_HOME Environment Variable.

    JAVA_HOME    **.;C:\Program Files (x86)\Java\jdk1.6.0_14**
    
  2. Set JAVA_HOME variable in PATH Variable section.

    PATH  %JAVA_HOME%\bin
    
  3. Set JAVA_HOME variable in CLASSPATH Variable

    CLASSPATH  %JAVA_HOME%\jre\lib
    
  4. Restart System

  5. Verify all variable

    echo %CLASSPATH%
    
    echo %JAVA_HOME%
    
    echo %PATH%
    
  6. Compile java class javac Test.java

  7. Run Java program java Test

如若梦似彩虹 2024-11-06 18:52:39

如果 JVM 或 ClassLoader 实例尝试加载类的定义(方法调用或创建新实例)并且找不到该类的定义,则抛出该错误。

NoClassDefFoundError 的原因是特定的类在运行时不可用,但在编译时可用。

1.该类属于缺少 JAR 或 JAVA 文件或者 JAR 未添加到类路径中。

2.类在 Java 类路径中不可用。

3.由于静态初始化程序块中出现异常,Java 中出现 NoClassDefFoundError。当你的类在 static 中执行一些静态初始化时
块,如果静态块抛出异常,则该类是
在Java中引用这个类会得到NoclassDefFoundError。

4.您的Classpath、PATH或JAVA_HOME设置不正确或JDK安装不正确。可以通过重新安装JDK来解决。

5.执行ma​​ven 全新安装,然后使用必要的 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.

遇到 2024-11-06 18:52:39

我遇到了和你一样的问题。

我这样解决了问题:

  1. 检查环境变量中的类路径
  2. 我在 eclipse 的导航器中打开了项目,并检查了类文件。我的类文件位于不同的文件夹中,而不是位于 bin 文件夹中。我只是将丢失的文件复制到 bin 文件夹中,然后问题就解决了。

只需将丢失的 .class 文件复制到 eclipse 项目的 bin 目录即可解决问题。

I was facing the same problem as you.

I resolved the problem like this:

  1. Check the class path in evironment variables
  2. I opened the project in navigator of eclipse and I checked the class files. My class files were in different folders not in the bin folder. I just copied the missing files into the bin folder then the problem was resolved.

Simply copying the missing .class files to bin directory of eclipse project resolved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文