简体   繁体   English

在 IntelliJ IDEA 项目中找不到主类:Java 应用程序

[英]Main class not found in project IntelliJ IDEA: Java Application

IntelliJ does not find a main class in my Java application project. IntelliJ 在我的 Java 应用程序项目中找不到主类。 The project was cloned from a git repository so had no run configuration.该项目是从 git 存储库克隆的,因此没有运行配置。 I go to Edit Configurations, add a new Application template, go to Main class: and it says "No matches found in project".我转到编辑配置,添加一个新的应用程序模板,转到主类:它说“在项目中找不到匹配项”。

So, manually searching through the hierarchy I find the .java file that contains the main function but it will not accept it as the main class.因此,手动搜索层次结构,我找到了包含 main 函数的 .java 文件,但它不会将其作为主类接受。 I've pasted the file below to prove that it has the correct main function.我已经粘贴了下面的文件以证明它具有正确的主要功能。

public class AdvanceWarsGameHandler implements IGame
{

    private Image mImage;
    private String mTitle;

    public AdvanceWarsGameHandler()
    {
        mTitle = "Advance Wars Game";
        mImage = new Image("/OffBrandCerealOopsAllCarries2-01.png");
    }

    //Game logic unrelated to graphics goes here
    @Override
    public void update(Game game, float deltaTime) 
    {

    }

    //Update, but for graphics
    @Override
    public void render(Game game, Renderer renderer) 
    {
        renderer.drawImage(mImage, game.getInput().getMouseX(), game.getInput().getMouseY());
    }

     public static void main(final String args[])
    {
        //Creating and starting an instance of AdvanceWarsGameHandler
        AdvanceWarsGameHandler advancewars = new AdvanceWarsGameHandler();
        Game myGame = new Game(advancewars);
        myGame.start();
    }

    public String getTitle()
    {
        return mTitle;
    }

}

So the question is, why is the IntelliJ project not recognizing the main function in this file, or what is IntelliJ looking for as the "Main class" of an application?所以问题是,为什么 IntelliJ 项目无法识别此文件中的 main 函数,或者 IntelliJ 寻找什么作为应用程序的“主类”?

Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.好的,希望这个答案能帮助不熟悉 IntelliJ IDEA 的其他人。

The solution came in two parts解决方案分为两部分

Part 1: Missing compilation directory.第 1 部分:缺少编译目录。

Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.由于我没有从 new 创建项目,而是克隆了一个 Git 存储库,因此没有设置默认的编译目录。

To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.要在 IntelliJ IDEA 中访问它,请转到文件->项目结构->项目并设置“项目编译器输出”,以便项目可以实际编译。

Part 2: Setting up the modules第 2 部分:设置模块

The original project was created in Eclipse which has packages.原始项目是在带有包的 Eclipse 中创建的。 In order to get those packages to work in IntelliJ, I had to go to the Modules tab of the Project Structure menu and set my src and res folders as Source and Resource Folders.为了让这些包在 IntelliJ 中工作,我必须转到“项目结构”菜单的“模块”选项卡,并将我的 src 和 res 文件夹设置为源和资源文件夹。 This allowed IntelliJ to find the main() function in my class and the program ran as expected.这允许 IntelliJ 在我的类中找到 main() 函数并且程序按预期运行。

This solved my problem though if any of you IntelliJ users out there can see anything bad about what I did to get it working, please comment.这解决了我的问题,但如果你们中的任何一个 IntelliJ 用户可以看到我为使其正常工作所做的任何事情,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM