简体   繁体   中英

Intellij Executable Jar - no main manifest attribute Vs Could not find or load main class

I am developing a small Java application using intellij idea. There is a package and a class. The class runs as expected when outside of the jar format.

I can build a jar and deploy all the dependencies to the same directory and the jar works as expected (I do this with the copy to the output path and link via manifest option when configuring a jar build process). However when I try to build a fat jar with all dependencies included in the same jar file, I get one of two errors depending on where the META-INF/MANIFEST.MF is positioned: no main manifest attribute and Could not find or load main class .

Given that the I could get a working jar file if all the dependencies are in the same directory as said jar file, I assumed that the issue was with the classpath.

The META-INF/MANIFEST.MF contents is as follows:

Manifest-Version: 1.0
Main-Class: myPackage.myClass
Class-Path: . myClassJarName.jar

When the jar is generated, I see the META-INF/MANIFEST.MF at the root of the structure with the correct information.

Also in the root of the jar is the myPackage/myClass.class , corresponding with the manifest file.

I can see all the dependencies in the jar file too.

My projects structure looks like this - note that I have added two manifest lcoations I've tried and the error which comes with each:

 .idea 
 out   
   artifacts
     myPackage_jar
       myPackage.jar
 src
   main
     java
       myPackage
         myClass.java
     META-INF <-------------------- *If here, Error: Could not find or load main class err*
       MANIFEST.MF
   META-INF < ------------------ *If here no main manifest attribute err*
     MANIFEST.MF

   test
     java
       myPackage
         myClass.java
 target
   classes
     myPackage
       myClass
   generated-sources
   generated-test-sources
   test-classes
 mypackage.iml
 pom.xml

... and its pom file looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>myPackage</groupId>
  <artifactId>myPackage</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>anonymise</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <repositories>
    <repository>
      <id>hortonworks</id>
      <name>hortonworks</name>
      <url>https://repo.hortonworks.com/content/repositories/releases/</url>
    </repository>
  </repositories>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3.2.6.2.0-205</version>
    </dependency>
    <dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>1.2.1000.2.6.2.69-1</version>
    </dependency>
  </dependencies>

</project>

The way I configured the jar artifact is:

  • Project Structure -> Artifacts
  • Click + -> Jar -> From modules with dependencies
  • I select the main class as the interface suggests
  • Option 'Extract to target Jar'
  • I currently have the META-INF/MANIFEST.MF Manifest directory set at src/main (the full path)
  • In the following window, on the left I see all the dependencies of the project. At the bottom of the list, it says myPackage compile output

After applying and oking, I generate the jar file to see, as I would expect, that inside there is both the META-INF/MANIFEST.MF and the myPackage/myClass in the jar root, along with a all the other dependencies. However when I try to run the jar either by:

  • cd to the location of the jar and use java -jar myGeneratedJarFilename.jar
  • Use the intellij idea run/debug configuration with the 'application' or the 'run jar application'

The result is the same:

no main manifest attribute, in myGeneratedJarFilename.jar

I've tried using the invalidate caches and restart several times. I've read several sets of documentation regarding Jars, executable Jars and the Java classpath but I can't find an error.

  • I am using Intellij Idea Community 2020.2 , on Arch Linux.
  • Upon typing java -version on the terminal I see openjdk version "14.0.2" 2020-07-14 .
  • THe Class-Path in the manifest file is a bit of a mystery to me. The docs say that it is a relative path but relative to what I'm not sure. Any clarifications on that from a pro would be nice!
  • The project structure was made using the org.apache.maven quickstark archetype

Can anyone offer any ideas as to what may be the problem?

Thanks a lot in advance!

The problem was that I was using the InteliJ Idea build process to build a fat jar, which is not supported in IntelliJ Idea.

To get it working, I updated my pom file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myPackage</groupId>
    <artifactId>myClass</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>myClass</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>hortonworks</id>
            <name>hortonworks</name>
            <url>https://repo.hortonworks.com/content/repositories/releases/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.3.2.6.2.0-205</version>
        </dependency>
        <dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>1.2.1000.2.6.2.69-1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>myPackage.myClass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Then I had to open the Maven window (View-> Tool Windows -> Maven) and use the Maven tasks clean then compile then package and the jars were created as expected.

Note:

  • In my working structure, the MANIFEST.MF is at src/main/resources/META-INF/MANIFEST.MF .
  • I did not use any of the IntelliJ project structure Artifacts actions to get the working fat jars. These aren't using Maven, even if you created a Maven project.

Thanks to y.bedrov for the definitive answer when lead to me understanding why it wasn't working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM