Linux 线程“main”中的异常 java.lang.NoClassDefFoundError: HelloWorld

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14132602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:10:56  来源:igfitidea点击:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

javalinuxcompilationterminal

提问by retrohacker

I've been working on this for about an hour and thumbing through Q&As on stackoverflow but I haven't found a proposed solution to my problem. I'm sorry if this is a duplicate, but I couldn't find any duplicate question with an answer that solved my specific problem.

我已经为此工作了大约一个小时,并在 stackoverflow 上浏览了问答,但我还没有找到解决我的问题的建议方案。如果这是重复的,我很抱歉,但我找不到任何重复的问题的答案可以解决我的特定问题。

I am trying to write and compile a java program from terminal for the first time (up until this point I have been using Eclipse for java and VIM for everything else, but I feel its time to switch entirely to VIM). Here is my current HelloWorld code:

我第一次尝试从终端编写和编译一个 java 程序(到目前为止,我一直将 Eclipse 用于 java,将 VIM 用于其他一切,但我觉得是时候完全切换到 VIM 了)。这是我当前的 HelloWorld 代码:

package main;

public class HelloWorld {
    public static void main(String args[]) {
        System.out.println("Hello World!");
    }
}

I compile and run using the following commands (specifying the classpath to ensure that isn't the problem):

我使用以下命令编译并运行(指定类路径以确保这不是问题):

javac -cp "./" HelloWorld.java
java -cp "./" HelloWorld

This gives me the following error message:

这给了我以下错误消息:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: main/HelloWorld)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.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:480)

I know it is seeing the file HelloWorld.class and trying to access the class HelloWorld because if I change the run command to:

我知道它正在查看文件 HelloWorld.class 并尝试访问类 HelloWorld 因为如果我将运行命令更改为:

java -cp "./" Foo

I get an entirely different error message:

我收到一条完全不同的错误消息:

Error: Could not find or load main class Foo

I have tried several dozen pages worth of troubleshooting and come up short, including the following:

我已经尝试了几十页的故障排除和总结,包括以下内容:

Exception in thread "main" java.lang.NoSuchMethodError: main

线程“main”中的异常 java.lang.NoSuchMethodError: main

http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html

http://introcs.cs.princeton.edu/java/15inout/mac-cmd.html

java -versionyields:

java -version产量:

java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode)

My operating system is LinuxMint and uname -ayields:

我的操作系统是 LinuxMint 并uname -a产生:

Linux will-Latitude-D620 2.6.38-8-generic #42-Ubuntu SMP Mon Apr 11 03:31:50 UTC 2011 i686 i686 i386 GNU/Linux

采纳答案by Sergiu Dumitriu

package main;

包主;

This means that your class resides in the mainpackage, and its canonical name is main.HelloWorld.

这意味着您的类驻留在main包中,其规范名称是main.HelloWorld.

Java requires that package names should also be mirrored in the directory structure. This means that:

Java 要求包名也应该反映在目录结构中。这意味着:

  1. Your HelloWorld.javafile should be in a directory named main
  2. You should execute javacand javafrom the directory containing main, not from mainitself
  3. The classpath should contain the directory where the maindirectory is, not mainitself
  4. javaexpects the canonical name of the class to execute, so main.HelloWorld
  1. 您的HelloWorld.java文件应该位于名为main
  2. 你应该执行javac,并java从目录中包含main,而不是从main自身
  3. 类路径应该包含目录所在的main目录,而不是main它本身
  4. java期望类的规范名称执行,所以 main.HelloWorld

So, to recap:

所以,回顾一下:

You should have something like myproject/main/HelloWorld.java

你应该有类似的东西 myproject/main/HelloWorld.java

From myproject, run javac main/HelloWorld.java

myproject,运行javac main/HelloWorld.java

From myproject, run java -cp ./ main.HelloWorld

myproject,运行java -cp ./ main.HelloWorld

回答by Dixit Patel

Problem:Basically, the Exception in thread "main" java.lang.NoClassDefFoundError:

问题:基本上,线程“main”中的异常java.lang.NoClassDefFoundError

means, that the class which you are trying to run was not found in the classpath.

意味着,在类路径中找不到您尝试运行的类。

Solution: you need to add the class or .jarfile which contains this class into the java classpath. When you are running a java class from the command line, you need to add the dot (.)

解决方案:您需要将.jar包含此类的类或文件添加到 java 类路径中。从命令行运行 java 类时,需要添加点 (.)

java YourSingleClass -cp .

into the classpath which tells the JVM to search for classes in actual directory.

进入告诉 JVM 在实际目录中搜索类的类路径。

If you are running a class from a .jar file, you need to add this jar file into the classpath:

如果您从 .jar 文件运行类,则需要将此 jar 文件添加到类路径中:

java org.somepackage.SomeClass -cp myJarWithSomeClass.jar

回答by Ryan Stewart

You've put your class in a package named "main", but you're trying to treat it like it isn't in a package. Since you put package main;at the top of your source file, you need to put HelloWorld.java in ./main, then run javac ./main/HelloWorld.java, followed by java -cp . main.HelloWorld.

您已将类放在名为“main”的包中,但您试图将其视为不在包中。因为你放在package main;源文件的顶部,所以你需要把 HelloWorld.java 放在 ./main 中,然后运行javac ./main/HelloWorld.java,然后是java -cp . main.HelloWorld.

These commands will get you the working example you're trying to build:

这些命令将为您提供您正在尝试构建的工作示例:

mkdir main
echo 'package main; public class HelloWorld { public static void main(String... args) { System.out.println("Hello World"); } }' > main/HelloWorld.java
javac main/HelloWorld.java
java -cp . main.HelloWorld

回答by Reed Richards

As a beginner you might encounter a very similar scenario where the error output is the same. You try to compile and run your simple program(without having any package set) and you do this:

作为初学者,您可能会遇到非常相似的情况,其中错误输出相同。您尝试编译并运行您的简单程序(没有设置任何包),然后执行以下操作:

javac HelloWorld.java
java HelloWorld.class

This will give you the same java.lang.NoClassDefFoundError since java thinks HelloWorld is your packageand class your class name. To solve it just use

这会给你同样的 java.lang.NoClassDefFoundError 因为 java 认为 HelloWorld 是你的和类你的类名。要解决它,只需使用

javac HelloWorld.java
java HelloWorld

See the Java page - Lesson: Common Problems (and Their Solutions)

请参阅 Java 页面 -课程:常见问题(及其解决方案)