Run java jar - no main manifest attribute error

2019-04-07 00:49发布

问题:

I’ve created simple java program (maven with pom ) which when I run some command with CMD it should created a file under given path... I do mvn clean install which finish successfully, Now I want to use this created jar from the command line like follwoing:

java -jar   "/Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar" path2genfile2create 

Which should run my program (this the first time that I try something like this…)

But the error which Im getting is:

no main manifest attribute, in /Users/i012/IdeaProjects/myproj/target/test.rts-1.0-SNAPSHOT.jar

What could be missing here ? which manifest attribute ?

The error is not coming from the class i’ve created

i've created some META-INF/MANIFEST.MF not helping but maybe Its wrong

回答1:

If you're using the Maven assembly plug-in, or your IDE tooling is, you need a mainClass element. This is what I use:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <archive>
        <manifest>
          <mainClass>com.foo.MyMainClass</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>


回答2:

A manifest is a file in the path META-INF/MANIFEST.MF within the jar which defines attributes like the classpath and the main class for running the jar file.

The basic structure would be like:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)

You can define your entry point by adding the property Main-Class: classname.

In order to create your jar file with a given manifest you can:

  1. Use your IDE to add a manifest to the jar it generates.
  2. Use a command like jar cfm MyJar.jar Manifest.txt MyPackage/*.class to manually create a jar with the given manifest and classes.
  3. Manually decompress the jar, add the manifest, and compress it again. Compression tools generally could do this with a drag/drop.

You can find out more about the jar manifest file here.



回答3:

in my case, I was using spring-boot but i did not mentioned my builder in my pom so i fixed it by:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>


回答4:

You need a main class to execute you application. Try the following. It worked for me.

Add the following code snippet to your pom.xml file if you are using maven build tool.

 <packaging>jar</packaging>

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

    <build>
        <plugins>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.validator.App</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>