Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ build wrong JAR: Could not find or load main class

I have a simple example

public class FileSystemReadFile {
    public static void main(String[] args) throws IOException {
        System.out.println("Reading the file" + args[0]);
    }
}

which is created in IntelliJ where I want to build JAR file; So what I did:

  1. Added Artifact with dependencies (presumably I have some);
  2. Ensure that MANIFEST.MF is located in src\main\resources\META-INF\ as it is already mentioned somewhere here on the site.
  3. Run Artifact build which gave me JAR file in out folder and I run that jar file that said me "Could not find or load main class"
    java <name>.jar

You may see that main class is added into MANIFEST and location of manifest is also fine.

Project structure

When I open that created JAR file, I see the same MANIFEST content, I see lots of dependency modules, but I don't see my class!

enter image description here

I suspect that is a cause. Any ideas?

like image 778
laggerok19 Avatar asked Nov 16 '16 16:11

laggerok19


People also ask

Could not find or load main class when running a jar?

When the JVM is unable to locate the main class, it's often because it's looking for the corresponding . class files in the wrong classpath. Of course, the way to rectify this problem is to manually specify the classpath by either using packages or specifying the classpath.

Why error Could not find or load main class?

The "Could not find or load main class" error occurs when the JVM fails to load the main class. This can happen due to various reasons, such as: The class being declared in the incorrect package. The file path of the class not matching the fully qualified name.


1 Answers

If you include any signed JARs in your app and then use IntelliJ to build artifacts, it will extract the JARS and bundle them with your compiled output.

This then causes a JAVA security exception. I've seen this with Eclipse Paho and Bouncy Castle which are signed.

You can check if any of the library JARs you are using are signed using the jarsigner tool.

jarsigner -verify -verbose  <path to library JAR>

Change your IntelliJ artifact setup so that these get bundled as libraries instead of being extracted. Extraction invalidates the certificate as you'd expect.

See how Paho and BCP are not extracted during artifact creation

Try creating a dummy project with just Main. Add 1 library JAR (that you are trying to build with) at a time. Build an output JAR each time until Main breaks. That's how I found this.

IntelliJ should warn you.....

like image 136
user1325094 Avatar answered Oct 01 '22 03:10

user1325094