线程“main”中出现异常java.lang.NoClassDefFoundError:
以下程序抛出错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
我发现了另一个常见原因。如果您使用 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.java 尝试查找您的
.class
文件的地方之一是您的当前目录。因此,如果您的.class
文件位于C:\java
中,您应该将当前目录更改为该目录。要更改目录,请在提示符处键入以下命令,然后按 Enter:
此
.
告诉 java 您的类路径是本地目录。One of the places java tries to find your
.class
file is your current directory. So if your.class
file is inC:\java
, you should change your current directory to that.To change your directory, type the following command at the prompt and press Enter:
This
.
tells java that your classpath is your local directory.如果您的包是
helloworld
您将转到包的父目录然后运行:If your package is
helloworld
you would go to parent dir of your package then run:像这样运行它:
Run it like this:
请参阅http://scottizu.wordpress.com/2013/08/28/fixing-the-exception-in-thread-main-java-lang-noclassdeffounderror-in-eclipse/。
这是可以从 Windows 命令提示符运行的 Java 命令的长形式:
请注意,类路径末尾没有斜杠。 javac.exe 命令期望文件以“.java”结尾。 java.exe 命令需要完整的类名,并且不以“.class”结尾。
有几种方法可以简化这些命令:
您不必输入整个类路径(即,您可以仅使用 -classpath“.”)。输入您将在其中工作的目录:
cd "C:\Users\Scott\workspace\myproject\"
您可以使用默认包(将 HelloWorld.java 文件目录放在您的工作目录中,不要使用Java 包指令)
如果您进行这些更改,您将运行类似这样的东西(并且您也许可以省略 -classpath “。”):
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:
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:
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\"
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 "."):
这是最终奏效的。
Here is what finally worked.
NoClassDefFounError 本身的 javadoc 将是一个好的开始(此处),然后我会建议您清理并重建您的项目。
The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.
将 jar 添加到您的类路径中。
例如,如果类
XXXXXX
出现NoClassDefFoundError
,请获取包含该类的 jar 并将其手动添加到类路径中。这对我有用。希望它也对你有用。
Add the jar in your classpath.
For example, if you have
NoClassDefFoundError
for classXXXXXX
, 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.
CLASSPATH
变量需要包含 Java 程序 .class 文件所在的目录。您可以包含“.”在 CLASSPATH 中指示应包含当前目录。The
CLASSPATH
variable needs to include the directory where your Java programs .class file is. You can include '.' inCLASSPATH
to indicate that the current directory should be included.您的
CLASSPATH
还需要知道您的HelloWorld
类的位置。简单来说,如果您正在运行
javac
和java
,您应该在CLASSPATH
中附加点.
(表示当前目录) > DOS 提示符下的命令。Your
CLASSPATH
needs to know of the location of yourHelloWorld
class also.In simple terms you should append dot
.
(means current directory) in theCLASSPATH
if you are runningjavac
andjava
commands from DOS prompt.我遇到了同样的问题,并偶然发现了“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.
在文件夹中的 cmd 提示符中键入以下内容:
type the following in the cmd prompt, within your folder:
Java-File:
那么有两种执行方式:
Java-File:
then there are two ways of executing it:
这里的问题是环境的设置和类文件的运行。
一个。要设置环境路径,请运行以下命令:
设置路径=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.
尝试
这样做,然后,如果它没有出现编译器错误(它不应该这样做,因为我在你的程序中看不到任何错误),然后输入
java Hello
Try doing
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
如果您想“编译并执行”使用任何 IDE(如 eclipse)创建的任何 java 文件,只需运行以下命令:
If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:
如果您的 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"
我终于发现这是 Apache Netbeans 编辑器的一个错误:
以下步骤将消除该错误:
希望有帮助,如果您使用新的 Apache Netbeans(而不是旧的 Netbeans)
I finally found this as a bug with Apache Netbeans editor:
Below steps will remove the error:
Hope that helps, if you are using new Apache Netbeans (not old Netbeans)
我有这个问题,我使用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
.您可以在
pom.xml
中找到有关所需库的信息,使用Apache Maven等工具构建java应用程序要容易得多。You can find information about required libraries inside
pom.xml
, it is much easier to use tools like Apache Maven to build java applications.接下来对我有用:
Next works for me: