我尝试启动的每个 Java 程序都显示错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33734791/
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
Every java program I try to start shows error
提问by atilas1
SOLVED, Program was in location with national symbol in it's path.
已解决,程序所在的位置带有国家符号。
I just started studying java, but every program i try to start (even example ones from my course) shows an error.
我刚开始学习 Java,但我尝试启动的每个程序(甚至是我课程中的示例程序)都显示错误。
Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
edit:
编辑:
example of code, but happens to any code.
代码示例,但发生在任何代码中。
public class Hello {
static void hello(){
System.out.println("Hello, World!");
}
public static void main(String[] args) {
hello();
}
}
回答by Michael Lloyd Lee mlk
Error: Could not find or load main class "Any class name of program I try start"
C:\Users\Mine\AppData\Local\NetBeans\Cache.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
You are attempting to run a class called Any class name of program I try start
, however the name of your class is Hello
.
您正在尝试运行一个名为 的类Any class name of program I try start
,但是您的类的名称是Hello
。
I don't know how Netbeans does things, but I would first try compiling and running the program withoutnetbeans.
我不知道 Netbeans 是如何做的,但我会首先尝试在没有netbeans 的情况下编译和运行程序。
javac Hello.java
java Hello
If that works then open up the run settings in netbeans and make sure that it is doing the same thing.
如果可行,则在 netbeans 中打开运行设置并确保它在做同样的事情。
回答by daiscog
This error means that when Netbeans is invoking the JVM, the JVM cannot find the class file for the class Netbeans is telling it to run. When you create a project in Netbeans, the classpathwill be configured for you by the IDE, so you shouldn't normally see this error unless you have deleted the auto-generated main class and made a new one from scratch in the wrong place.
此错误意味着当 Netbeans 调用 JVM 时,JVM 找不到 Netbeans 告诉它运行的类的类文件。当您在 Netbeans 中创建项目时,IDE 将为您配置类路径,因此您通常不会看到此错误,除非您删除了自动生成的主类并在错误的位置从头开始创建了一个新的主类。
So the first thing to do is check what class Netbeans is using as the main class:
所以首先要做的是检查 Netbeans 使用哪个类作为主类:
Right-click on the project name in the Projects tab and click on "Properties"
右键单击“项目”选项卡中的项目名称,然后单击“属性”
Then click on "Run" and check the name of the class in "Main Class":
然后单击“运行”并检查“主类”中的类名称:
Note in my example the class is called "tests.Test". This means the class Test in the package"tests". In your question, your class "Hello" doesn't have a package declaration at the top (although you may have chosen not to copy this). If you have no package (and you really shouldbe using packages, even for trivial programs like "Hello, World!", just to get used to doing so, if nothing else), the "Main Class" entry should just be the class name.
请注意,在我的示例中,该类称为“tests.Test”。这意味着“tests”包中的类Test 。在您的问题中,您的类“Hello”在顶部没有包声明(尽管您可能选择不复制此声明)。如果你没有包(你真的应该使用包,即使是像“Hello, World!”这样的小程序,只是为了习惯这样做,如果没有别的),“主类”条目应该只是类姓名。
So you need to either move your class into the package specified in this parameter, or change this parameter to match the fully qualified nameof your main class
因此,您需要将您的类移动到此参数中指定的包中,或者更改此参数以匹配您的主类的完全限定名称
回答by Gray
Just make a new main class or just re-type public static void main(String[] args) { }
and that's it.
只需创建一个新的主类或重新输入即可public static void main(String[] args) { }
。