gpt4 book ai didi

java - Eclipse 找不到我的主类

转载 作者:行者123 更新时间:2023-12-02 03:30:06 28 4
gpt4 key购买 nike

我正在尝试运行我的第一个 Eclipse Java 项目,但由于某种原因 Eclipse 找不到 main

我创建了一个新的 Java 运行配置,并将项目的源文件目录添加到类路径选项卡下的用户条目中。然后我添加了我的主类 DisplayDeckmain主选项卡下的类字段。当我运行我的项目时,出现以下错误。

Error: could not find or load main class DisplayDeck.

这是我的代码:

package cards;

public class DisplayDeck {
static void main(String[] args) {
Deck cardDeck = new Deck();
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
card tempCard = cardDeck.getCard(i, j);
System.out.format("%s of %s", tempCard.rankToString(tempCard.getRank()), tempCard.suitToString(tempCard.getSuit()));
}
}
}
}

我在这里阅读了一些类似的问题,但这些问题的解决方案似乎都不起作用...我注意到的其他事情是当我选择 main 旁边的搜索按钮时在构建配置中 main 下的类字段中,listView 中没有可供选择的类。 .

最佳答案

在代码片段中,您有 static void main(String[] args) 而不是 public static void main(String[] args)。这就是找不到 main 的原因。

来自http://www.cs.princeton.edu/courses/archive/spr96/cs333/java/tutorial/java/anatomy/main.html :

The method signature for the main() method contains three modifiers: public indicates that the main() method can be called by any object. Missing Page covers the ins and outs of access modifiers supported by the Java language: public, private, protected, and the implicit, friendly. static indicates that the main() method is a class method. Class Members vs. Instance Members later in this lesson talks in more detail about class methods and variables. void indicates that the main() method has no return value. The main() method in the Java language is similar to the main() function in C and C++. When you execute a C or C++ program, the runtime system starts your program by calling its main() function first. The main() function then calls all the other functions required to run your program. Similarly, in the Java language, when you execute a class with the Java interpreter, the runtime system starts by calling the class's main() method. The main() method then calls all the other methods required to run your application. If you try to run a class with the Java interpreter that does not have a main() method, the interpreter prints an error message. For more information see Troubleshooting Interpreter Problems .

Arguments to the main() Method

As you can see from the code snippet above, the main() method accepts a single argument: an array of Strings.

public static void main(String args[])

关于java - Eclipse 找不到我的主类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38228042/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com