Java.lang.NoClassDefFoundError在命令提示符中运行时。

huangapple go评论75阅读模式
英文:

Java.lang.NoClassDefFoundError in command prompt run

问题

我正在尝试在命令提示符中运行我的代码,但是它给了我一个错误.. 有谁知道是什么问题吗?

错误:找不到或加载主类hello
原因:java.lang.NoClassDefFoundError:FirstQuarter/hello(错误名称:hello)

Java.lang.NoClassDefFoundError在命令提示符中运行时。

英文:

I'm trying to run my code in command prompt and it gives me error .. Can anyone know whats wrong on it?

Error: Could not find or load main class hello
Caused by: java.lang.NoClassDefFoundError: FirstQuarter/hello (wrong name: hello)

Java.lang.NoClassDefFoundError在命令提示符中运行时。

答案1

得分: 0

这种类型的错误是由于在运行时未在Classpath中找到该类,但在编译时找到了。请尝试打印:

System.getProperty("java.classpath")

这将打印出类路径,从而让您了解实际运行时的类路径。

此外,请确保您将包含了main方法用于执行的类的完全限定名称传递给java命令,例如:

directory_that_holds_package> java package_name.Class_name
英文:

This type of error is due to the class not found in the Classpath during runtime but found during compile time.
Look to print 

System.getproperty("java.classpath")

which will print the classpath so you get an idea as to the actual runtime classpath.

Also, make sure you pass the fully qualified name of the class to "java" command which contains the main method for execution.

directory_that_holds_package>java package_name.Class_name

答案2

得分: 0

首先,我猜测您的程序可能在Eclipse和Idea中能够顺利运行,但在命令行中却出现了这个错误。

现在,您应该在命令行中包括您程序的package。如果您的程序像这样:

package firstprogram;

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

那么您应该在FirstQuarter文件夹中运行 java firstprogram.HelloWorld

英文:

First, I take a guess that your program could run smoothly in Eclipse and Idea, but it gives this error in command line.

Now, you should include your program's package in command line. If your program is like:

package firstprogram;

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

Then you should run java firstprogram.HelloWorld in FirstQuarter folder.

答案3

得分: 0

这个错误主要是因为程序无法访问你在程序中定义的类,这可能是因为你没有定义正确的类路径,或者没有包含运行该类所需的必要库。原因可能有很多。

因此,尝试在任何集成开发环境(IDE)中运行你的代码,这样你就可以更容易地识别错误。

英文:

This error is mainly due to when the program is not able to access the class which you have defined in your program and it can be due to reasons like you have not defined the proper classpath or you have not included the required library needed to run that class. The reasons can be many.

So try to run your code on any IDE as you will be able to easily identify the errors.

huangapple
  • 本文由 发表于 2020年6月29日 11:47:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/62630897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定