java - Eclipse 找不到我的主类

标签 java eclipse

我正在尝试运行我的第一个 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/

相关文章:

java - 选择具有 between where 限定符的命名参数查询

java - 安卓 OpenCV : No resource identifier found for attribute 'camera_id' in package

java - Vaadin 组合框 getValue() 没有得到我想要的

JavaFX LineChart 动态改变图例颜色

java - Scala (java) grpc 异步拦截器状态传播

java - Java 中的导入命令

eclipse - Eclipse 在运行 Web 应用程序时如何工作

java - eclipse 插件中的 SWT 浏览器在 Mac 和 Windows 中的行为不同

java - 编程 "compiler"- 有什么方法可以知道 {(开启者)是否 }(关闭)?

java - 使用java从doc或rtf文件读取表单数据