laggerok19
laggerok19

Reputation: 452

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?

Upvotes: 5

Views: 9145

Answers (2)

user1325094
user1325094

Reputation: 339

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.....

Upvotes: 5

laggerok19
laggerok19

Reputation: 452

Not sure what was with IntellJ, but I rebuilded artifacts again and it was ok.

hadoop jar <Jar-name>
java -jar <Jar-name>

Everything is working fine.

Upvotes: 1

Related Questions