线程“main”中出现异常java.lang.NoClassDefFoundError:

发布于 2024-11-15 03:24:58 字数 329 浏览 5 评论 0原文

以下程序抛出错误:

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

CLASSPATH C:\Program Files\Java\jdk1.6.0_18\bin\

Path C:\Program Files\Java\jdk1.6.0_18\bin\

JAVAHOME C:\Program Files\Java\jdk1.6.0_18\bin

您能告诉我根本原因吗?

The following program is throwing error:

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

CLASSPATH C:\Program Files\Java\jdk1.6.0_18\bin\

Path C:\Program Files\Java\jdk1.6.0_18\bin\

JAVAHOME C:\Program Files\Java\jdk1.6.0_18\bin

Can you please tell me the root cause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(21

浅浅 2024-11-22 03:24:58

我发现了另一个常见原因。如果您使用 Eclipse 等 IDE 在包内创建 java 文件,您将在 java 文件的顶部找到包名称,如“package pkgName”。如果您尝试从命令提示符运行此文件,您将收到 NoClassDefFoundError 错误。从 java 文件中删除包名称并使用命令提示符中的命令。为此浪费了3个小时。

I found one other common reason. If you create the java file inside a package, using IDE like Eclipse, you will find the package name at the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this.

牵强ㄟ 2024-11-22 03:24:58
Exception in thread "main" java.lang.NoClassDefFoundError  

java 尝试查找您的 .class 文件的地方之一是您的当前目录。因此,如果您的 .class 文件位于 C:\java 中,您应该将当前目录更改为该目录。

要更改目录,请在提示符处键入以下命令,然后按 Enter:

cd c:\java  

. 告诉 java 您的类路径是本地目录。

Executing your program using this command should correct the problem:
java -classpath . HelloWorld  
Exception in thread "main" java.lang.NoClassDefFoundError  

One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that.

To change your directory, type the following command at the prompt and press Enter:

cd c:\java  

This . tells java that your classpath is your local directory.

Executing your program using this command should correct the problem:
java -classpath . HelloWorld  
缺⑴份安定 2024-11-22 03:24:58

如果您的包是 helloworld 您将转到包的父目录然后运行:

java helloworld.HelloWorld

If your package is helloworld you would go to parent dir of your package then run:

java helloworld.HelloWorld
只涨不跌 2024-11-22 03:24:58

像这样运行它:

java -jar HelloWorld.jar

Run it like this:

java -jar HelloWorld.jar
芸娘子的小脾气 2024-11-22 03:24:58

请参阅http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/

这是可以从 Windows 命令提示符运行的 Java 命令的长形式:

"C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
"C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
  1. 这些命令可以从任何目录运行,这意味着您不必位于 HelloWorld.java 文件所在的目录中。
  2. 第一行编译 HelloWorld.java 文件,创建 HelloWorld.class 文件。
  3. 第二行运行 HelloWorld.class 文件。
  4. -classpath 告诉 java 在每个命令中查找指定文件的位置
  5. Java 编译器 (javac.exe) 期望 java 文件相对于类路径的位置(即文件位于 C:\Users\Scott\workspace \myproject\com\mycompany\myapp\HelloWorld.java)
  6. Java (java.exe) 需要相对于类路径的包(即 com.mycompany.myapp)和类(HelloWorld)(即文件位于C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.class)

请注意,类路径末尾没有斜杠。 javac.exe 命令期望文件以“.java”结尾。 java.exe 命令需要完整的类名,并且不以“.class”结尾。

有几种方法可以简化这些命令:

  1. 您不必指定 java.exe 的完整路径。将Java添加到Windows路径(运行->sysdm.cpl->高级选项卡->环境变量->选择路径->编辑->附加“;C:\Program Files\Java\jdk1.6.0 _18\bin\")。或者您可以附加 JAVA_HOME 并创建该环境变量。
  2. 您不必输入整个类路径(即,您可以仅使用 -classpath“.”)。输入您将在其中工作的目录:

    cd "C:\Users\Scott\workspace\myproject\"

  3. 您可以使用默认包(将 HelloWorld.java 文件目录放在您的工作目录中,不要使用Java 包指令)

如果您进行这些更改,您将运行类似这样的东西(并且您也许可以省略 -classpath “。”):

cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld

See http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/.

This is the long form of the Java commands that can be run from a Windows command prompt:

"C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
"C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
  1. These commands can be run from any directory, meaning you don't have to be in the directory where your HelloWorld.java file is.
  2. The first line compiles your HelloWorld.java file, creating a HelloWorld.class file.
  3. The second line runs the HelloWorld.class file.
  4. The -classpath tells java where to look for the specified file in each command
  5. The Java compiler (javac.exe) expects the location of the java file, relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.java)
  6. Java (java.exe) expects the package (ie com.mycompany.myapp) and class (HelloWorld), relative to the classpath (ie the file is located at C:\Users\Scott\workspace\myproject\com\mycompany\myapp\HelloWorld.class)

Notice the classpath has no slash at the end. The javac.exe commands expects the file to end with ".java". The java.exe command expects the full class name and does not end with ".class".

There are a few ways to simplify these commands:

  1. You don't have to specify the entire path to java.exe. Add Java to the Windows Path (Run->sysdm.cpl->Advanced Tab->Environment Variables->Select Path->Edit->Append ";C:\Program Files\Java\jdk1.6.0_18\bin\"). Or you can append JAVA_HOME and create that Environment Variable.
  2. You don't have to enter the entire classpath (ie, you can just use -classpath "."). Enter the directory you will be working in:

    cd "C:\Users\Scott\workspace\myproject\"

  3. You can use the default package (put the HelloWorld.java file directory in your working directory and don't use the Java package directive)

If you make these changes you would run something like this (and you might be able to leave out -classpath "."):

cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld
〗斷ホ乔殘χμё〖 2024-11-22 03:24:58

这是最终奏效的。

`@echo off
set path=%path%;C:\Program Files\Java\jdk1.7.0_71\bin;
set classpath=C:\Program Files\Java\jdk1.7.0_71\lib;
cd <packageDirectoryName>
javac .\trainingPackage\HelloWorld.java
cd ..
java trainingPackage.HelloWorld 
REM (Make sure you are on the parent directory of the PackageName and not inside the    Packagedirectory when executing java).`

Here is what finally worked.

`@echo off
set path=%path%;C:\Program Files\Java\jdk1.7.0_71\bin;
set classpath=C:\Program Files\Java\jdk1.7.0_71\lib;
cd <packageDirectoryName>
javac .\trainingPackage\HelloWorld.java
cd ..
java trainingPackage.HelloWorld 
REM (Make sure you are on the parent directory of the PackageName and not inside the    Packagedirectory when executing java).`
夢归不見 2024-11-22 03:24:58

NoClassDefFounError 本身的 javadoc 将是一个好的开始(此处),然后我会建议您清理并重建您的项目。

The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.

江城子 2024-11-22 03:24:58

将 jar 添加到您的类路径中。

例如,如果类 XXXXXX 出现 NoClassDefFoundError,请获取包含该类的 jar 并将其手动添加到类路径中。

这对我有用。希望它也对你有用。

Add the jar in your classpath.

For example, if you have NoClassDefFoundError for class XXXXXX, get the jar containing the class and add it to your classpath manually.

It works for me. Hopefully it will work for you as well.

从来不烧饼 2024-11-22 03:24:58

CLASSPATH 变量需要包含 Java 程序 .class 文件所在的目录。您可以包含“.”在 CLASSPATH 中指示应包含当前目录。

set CLASSPATH=%CLASSPATH%;.

The CLASSPATH variable needs to include the directory where your Java programs .class file is. You can include '.' in CLASSPATH to indicate that the current directory should be included.

set CLASSPATH=%CLASSPATH%;.
海风掠过北极光 2024-11-22 03:24:58

您的 CLASSPATH 还需要知道您的 HelloWorld 类的位置。

简单来说,如果您正在运行 javacjava,您应该在 CLASSPATH 中附加点 .(表示当前目录) > DOS 提示符下的命令。

Your CLASSPATH needs to know of the location of your HelloWorld class also.

In simple terms you should append dot . (means current directory) in the CLASSPATH if you are running javac and java commands from DOS prompt.

梦里寻她 2024-11-22 03:24:58

我遇到了同样的问题,并偶然发现了“Build Main Project F11”的解决方案。
IDE 提出了一个“选项”,我可能想取消选中“保存时编译”
在构建>项目配置对话框的编译部分。
取消选中“保存时编译”,然后执行通常的(对我来说)“清理和构建”对我来说很有效。

I had the same problem, and stumbled onto a solution with 'Build Main Project F11'.
The ide brought up an "option" that I might want to uncheck 'Compile on Save'
in the Build > Compiling portion of the Project configuration dialog.
Unchecking 'Complile on Save' and then doing the usual (for me) 'Clean and Build' did the trick for me.

雪若未夕 2024-11-22 03:24:58

在文件夹中的 cmd 提示符中键入以下内容:

set classpath=%classpath%;.;

type the following in the cmd prompt, within your folder:

set classpath=%classpath%;.;
坐在坟头思考人生 2024-11-22 03:24:58

Java-File:

package com.beans;

public class Flower{
 .......
}

packages :=> com.beans,
java class Name:=> Flower.java,
Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)

那么有两种执行方式:

1. Goto top Folder (here its C:\>),
     then : C:> java com.beans.Flower 
2. Executing from innermost folder "beans" here (C:\com\beans:>),
     then: C:\com\beans:> java -cp ./../.. com.beans.Flower

Java-File:

package com.beans;

public class Flower{
 .......
}

packages :=> com.beans,
java class Name:=> Flower.java,
Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)

then there are two ways of executing it:

1. Goto top Folder (here its C:\>),
     then : C:> java com.beans.Flower 
2. Executing from innermost folder "beans" here (C:\com\beans:>),
     then: C:\com\beans:> java -cp ./../.. com.beans.Flower
南汐寒笙箫 2024-11-22 03:24:58

这里的问题是环境的设置和类文件的运行。
一个。要设置环境路径,请运行以下命令:
设置路径=C:\Program Files (x86)\Java\jdk1.7.0\bin
b.从包中运行程序,如 com.test.TestJavaClass

命令:java com.test.TestJavaClass

这里的一般问题是我们从包内部运行它,如 src/package/name。
我们不应该包含 src,包名就足够了。

The Problem here is the setting the environment and the running of the class file.
a. To set the environment path run the following command:
set path=C:\Program Files (x86)\Java\jdk1.7.0\bin
b. run the program from the package like com.test.TestJavaClass

Command: java com.test.TestJavaClass

The general issue here is we run it either from inside the package like src/package/name.
We should not include src, package name is enough.

握住你手 2024-11-22 03:24:58

尝试

javac Hello.java

这样做,然后,如果它没有出现编译器错误(它不应该这样做,因为我在你的程序中看不到任何错误),然后输入

java Hello

Try doing

javac Hello.java

and then, if it comes up with no compiler errors (which it should not do because I cannot see any bugs in your programme), then type

java Hello

深爱成瘾 2024-11-22 03:24:58

如果您想“编译并执行”使用任何 IDE(如 eclipse)创建的任何 java 文件,只需运行以下命令:

编译:
javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java

执行: java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics

If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:

Compile:
javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java

Execute: java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics

无需解释 2024-11-22 03:24:58

如果您的 Program.java 位于“src/mypkg/subpkg/”目录中:

转到“src”目录

然后编译使用“javac mypkg/subpkg/Program.java”

运行使用“java mypkg.subpkg.Program.class”

if your Program.java is in "src/mypkg/subpkg/" directory:

go to "src" dir

Then to compile use "javac mypkg/subpkg/Program.java"

To run use "java mypkg.subpkg.Program.class"

寄与心 2024-11-22 03:24:58

我终于发现这是 Apache Netbeans 编辑器的一个错误:

以下步骤将消除该错误:

  1. 重命名文件名和文件名。 class 为 Abc
  2. 关闭编辑器
  3. 重新打开编辑器
  4. 重命名文件名 &类,从 Abc,回到以前的名称
  5. 现在调试项目 (Ctrl+F5) 工作正常

希望有帮助,如果您使用新的 Apache Netbeans(而不是旧的 Netbeans)

I finally found this as a bug with Apache Netbeans editor:

Below steps will remove the error:

  1. Rename the filename & class to Abc
  2. Close the editor
  3. Reopen the editor
  4. Rename the filename & class, from Abc, back to the previous name
  5. Now debug project (Ctrl+F5) works fine

Hope that helps, if you are using new Apache Netbeans (not old Netbeans)

又怨 2024-11-22 03:24:58

我有这个问题,我使用maven。我只需单击 Maven 项目 ant,然后命名

project->插件->干净->和按钮运行

输入图片此处描述

I had this proplem I used maven. I just click maven projects ant then name

project-> plugins -> clean -> and button run.

enter image description here

2024-11-22 03:24:58

您可以在pom.xml中找到有关所需库的信息,使用Apache Maven等工具构建java应用程序要容易得多。

    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.20</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-math3</artifactId>
        <version>3.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>24.0-jre</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject.extensions</groupId>
        <artifactId>guice-assistedinject</artifactId>
        <version>4.2.0</version>
    </dependency>

You can find information about required libraries inside pom.xml, it is much easier to use tools like Apache Maven to build java applications.

    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>1.20</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-math3</artifactId>
        <version>3.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>24.0-jre</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.2.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.inject.extensions</groupId>
        <artifactId>guice-assistedinject</artifactId>
        <version>4.2.0</version>
    </dependency>
梦明 2024-11-22 03:24:58

接下来对我有用:

  1. 找到@SpringBootApplication并通过clicikg运行它侧面启动
  2. 单击编辑配置 - 转到步骤1最后创建的并同步构建和运行设置

Next works for me:

  1. Find @SpringBootApplication and run it by clicikg Start on side
  2. Click Edit Configuration - Go to last created by step.1 and synchronize Build and run settings
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文