Thomas Divelbiss
Thomas Divelbiss

Reputation: 91

Main class not found in project IntelliJ IDEA: Java Application

IntelliJ does not find a main class in my Java application project. The project was cloned from a git repository so had no run configuration. 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. 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?

Upvotes: 2

Views: 7992

Answers (1)

Thomas Divelbiss
Thomas Divelbiss

Reputation: 91

Okay, hopefully this answer will help others who are unfamiliar with IntelliJ IDEA.

The solution came in two parts

Part 1: Missing compilation directory.

Since I did not create the project from new and instead I cloned a Git repository, there was no default compilation directory set up.

To access this in IntelliJ IDEA go to File -> Project Structure -> Project and set the "Project compiler output" so the project can actually compile.

Part 2: Setting up the modules

The original project was created in Eclipse which has packages. 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. This allowed IntelliJ to find the main() function in my class and the program ran as expected.

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.

Upvotes: 3

Related Questions