Error: Could not find or load main class


#1

I’m getting the following error message:

Error: Could not find or load main class

In fact I get quite a few hits which I search in google for this, but I’m successful in applying any of the proposed solutions.

It seems this is ultimately an error from Java, but I’m not sure what it is trying to tell me.

I do notice that the mainapp selection box next to the play button has a red X, as if to indicate
that something is wrong. When I try to select “Edit Configurations…” under that pulldown
I see the following dialog form, which echoes the message (as a warning)
(!) Warning: Class ‘mainapp’ not found in module ‘jims-super-project’

If someone can help, that would be great.


#2

The Edit Configurations dialog seems to have an option to edit the Main class, i.e., the […] button.
When I click that I get a navigator where I can select the “Main Class” by clickity-click.

09

If I navigate to the mainapp class in that navigator, and click OK there. I get a terse, unhelpful message: “main app is not acceptable”

44

It appears from https://stackoverflow.com/questions/12464637/why-is-my-class-not-acceptable
that the message means that the corresponding Java class needs to be “public”.

Is there something I need to do to ensure that this behind-the-scenes Java class is public?


#3

I’m pretty sure that the problem is that mainapp is a class. The entrypoint to your program has to be a main method inside an object.


#4

Hi Jasper, thanks for the clue. Does that object need to inherit from something in particular? Or does the main method need to have a certain signature? Because even if I change the class to an object, the problem of “Error: Could not find or load main class mainapp” still persists.

Here is the first few lines of the object definition

13

and here is the main method definition

04


#5

So after changing mainapp to an object, I have to again go to the Edit Configurations dialog
and re-trace the hierarchical path down to the file, click on the object name, mainapp,
and intellij changes the Main class name to jimka.mainapp

Thereafter the error vanishes.


#6

Rebuild the project. Add Scala (Add framework support) and also make sure you build via your build system (gradle, sbt etc.)


#7

Mill, CBT, … :wink: