Could not find or load main class with IntelliJ Application configuration
The issue comes from the fact that the project has an Android module. The module needs the android gradle plugin which is globally applied to all modules in the project, even those which aren't android modules. When applied to a module, it adds an Android-Gradle facet that changes the module classpath from the default out/classes
to build/classes/java
. For kotlin modules, the main class can't be found at runtime since the classpath is wrong.
There's is currently no way to apply the android plugin to only android modules. Workarounds include patching the android.jar file, automatically removing the Android-Gradle facet on sync, or moving the android module to an external project. There's no clean way to do it.
The issue has already been reported here, but it shows no sign that it will ever be fixed.
Unable to find or load Main Class in Intellij 2019.4
Thanks to CrazyCoder for pointing out the errors in my project (silly mistakes). It seems that after I have resolved those mistakes.
- I had to follow the solutions I have posted then rebuild the
.jar
file. - Afterwards I need to mark the
src
folder as a source root folder (right click folder -> Mark Directory as -> sources root
) Edit Configurations
if pointing to wrong main class.- Set Project compiler output (File -> Project Structure -> Project) to a folder designated to hold the output (any folder basically in or outside the project).
Intellij IDEA: Could not find or load main class if Dependencies Storage Format is changed to Eclipse
Thanks for the report, it appears to be a bug in IntelliJ IDEA project configuration when the dependencies format is set to Eclipse. IDE loads the project incorrectly and the sources root configuration is lost, therefore compiler doesn't get the correct information about the source files and nothing is produced in the output directory. Classpath doesn't contain any classes and the execution of the main class fails since it doesn't exist in the output (classpath).
.jar file error could not find or load main class with IntelliJ
The META-INF
folder in your generated Jar has these 2 files -
SIGNINGGC.RSA
SIGNINGGC.SF
I assume that you are not signing the Jar. Then, these files must be coming from one of your dependancies when you are creating a Fat Jar. If any of your dependancies has a Signed Jar, then it could result into that could not find or load main class:
exception.
You can quickly find if this is the issue by removing those files with this command (referenced from here) and try and run the code -
zip -d yourjar.jar 'META-INF/*.SF' 'META-INF/*.RSA' 'META-INF/*.DSA'
IntelliJ shows 2 Options while generating Jar. The first one will generate a Fat Jar. The other option will keep the other library jars intact and link with them using Manifest file.
Assuming all you need is to run your code, use the second option in the Create Jar from Modules
Dialog box.
Related Topics
Convert Html/Mxml File to Word Doc Programmatically in Java
How to Sort by Two Fields in Java and Specify Sort Direction
Scheduling a Job With Spring Programmatically (With Fixedrate Set Dynamically)
In Junit 5, How to Run Code Before All Tests
Avoid Keycloak Default Login Page and Use Project Login Page
How to Remove the Slashes from Json Key While Converting Object into String
Newline Character in Jlabel.Settext()
How to Specify the Path for Getresourceasstream() Method in Java
How to Accept Only Numeric Values in a Jtextfield
Org.Hibernate.Annotationexception: @Onetoone or @Manytoone on <Entity> References an Unknown Entity
How to Send Parallel Get Requests and Wait for Result Responses
How to Generate a Unique and Short File Name in Java
How to Set a Ttl for @Cacheable
Intellij Compilation Error Zip End Header Not Found
How to Update a Jsf Component from a Jsf Backing Bean Method