[英]Why do I get a NoClassDefFoundError Exception in Java?
I'm trying to run a Java app from the cmd, and I'm getting the following Errors: 我正在尝试从cmd运行Java应用程序,并且出现以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: Main
Caused by: java.lang.ClassNotFoundException: Main
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Main. Program will exit.
In the dir you can find: 在目录中,您可以找到:
Directory of C:\Java
AVLNode.java
AVLTree.java
Comparator.java
HashTable.java
input1.dat
input2.dat
Main.java
StringComparator.java
and I'm Running: 而我正在跑步:
java Main input1.dat input2.dat output1.dat
I have Main.Java in the folder and I have: 我的文件夹中有Main.Java,我有:
public static void main(String[] args)
method on the Main.Java (and some more functions) Main.Java上的
public static void main(String[] args)
方法(以及更多功能)
I already read the answers about this problem here but I think I did everything alright :( so what can be the problem? 我已经在这里阅读了有关此问题的答案,但是我想我做的一切都很好:(那么可能是什么问题?
Check whether you have Main.class
file in your current directory or not. 检查当前目录中是否有
Main.class
文件。 If it is already there, check your path
variable in you system environment variables. 如果已经存在,请检查系统环境变量中的
path
变量。 It should point to JAVA_HOME\\bin
. 它应该指向
JAVA_HOME\\bin
。
as stated in another answer check if Main.class
is present in current directory 如另一个答案中所述,检查
Main.class
是否存在于当前目录中
also try java -cp . Main arg1 arg2
也尝试
java -cp . Main arg1 arg2
java -cp . Main arg1 arg2
above line sets classpath to current directory 上一行将classpath设置为当前目录
First make sure that you have compiled your code. 首先,请确保您已编译代码。
You should be in the directory that contains the Main.class (If you are using eclipse IDE it will be bin/). 您应该位于包含Main.class的目录中(如果您使用的是Eclipse IDE,它将为bin /)。
If your Main class is in a package you should run your command from the directory that contains the package and your command will be java [package name].Main [arguments] 如果Main类位于软件包中,则应从包含该软件包的目录中运行命令,并且该命令将是java [package name]。Main[arguments]
If you want to run your command from anywhere you can use the -cp option like the following : 如果要在任何地方运行命令,可以使用-cp选项,如下所示:
java -cp [classpath] [package name].Main [arguments]
with classpath : path to the directory containing the .class until right before the package 使用classpath:包含.class的目录的路径,直到紧接在包之前
In addition to what the others suggested, check to see if you have any package
statements in Main
. 除了其他建议之外,请检查
Main
是否有任何package
语句。 If so, you must include it when starting the program. 如果是这样,则在启动程序时必须包括它。
For example, if you have: 例如,如果您有:
package mypackage;
public class Main
{
public static void main (String[] affhf)
{
}
}
then you must start your program by calling: 那么您必须通过以下方式启动程序:
java mypackage.Main input1.dat input2.dat output1.dat
This might slip by the compilation process in some situations... 在某些情况下,这可能会因编译过程而漏掉。
Looks like you haven't compiled your code. 看起来您尚未编译代码。 Try the following:
请尝试以下操作:
cd Java
javac Main.java
This should create the .class files in the Java directory. 这应该在Java目录中创建.class文件。 Then, without changing the directory, try:
然后,不更改目录,请尝试:
java Main input1.dat input2.dat output1.dat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.