简体   繁体   中英

Maven : no main manifest attribute, in target/xx.jar, Unable to execute jar file

Here is the pom.xml:

<?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>dl4j</groupId>
    <artifactId>dl4j</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>example.JavaTestExample</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-core</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>nd4j-native-platform</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-api</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <!--  You need the below dependency to use CodecRecordReader-->
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-data-codec</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
        <!--       <dependency>
                   <groupId>org.bytedeco.javacpp-presets</groupId>
                   <artifactId>${moduleName}-platform</artifactId>
                   <version>${moduleVersion}-1.4.4</version>
               </dependency>-->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.8.0-beta4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-beta4</version>
        </dependency>
        <!--  You need the below dependency to use LocalTransformExecutor-->
        <dependency>
            <groupId>org.datavec</groupId>
            <artifactId>datavec-local</artifactId>
            <version>1.0.0-beta3</version>
        </dependency>
    </dependencies>
    <!-- Uncomment to use snapshot version -->
    <!--<repositories>
        <repository>
            <id>snapshots-repo</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>daily</updatePolicy>  &lt;!&ndash; Optional, update daily &ndash;&gt;
            </snapshots>
        </repository>
    </repositories>-->
</project>

I wanted to run maven-assembly-plugin and create a jar file with all dependencies added to it. When I run java -jar jarfile.jar , I get the below error:

no main manifest attribute, in target/jarfile.jar

I went through several Stack Overflow posts for the same and tried all permutations and combinations. I tried adding both assembly and shade plugins. You can see the settings I have in my pom.xml above. I'm not really sure on how this is happening even though I added proper maven settings.

I verified that Java is running fine:

在此处输入图片说明

I verified whether I specified the main class correctly.

Everything looks fine for me except the results and I'm confused at the moment.

What might be going wrong? I expect some of them still mark the question as "duplicate", but I have been reading other posts and tried changing the settings accordingly. None of them worked.

Good quality question!

I think this will help achieve your goal.

This is what I use to build a clickable JavaFX Jar with all dependencies included. This is using maven-jar-plugin and maven-shade-plugin rather than maven-assembly-plugin ; Here is a good resource on the differences.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>10</release>
            </configuration>
        </plugin>
        <plugin>
            <!-- I think you don't need this one -->
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>${mainClass}</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            ${mainClass}
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>${mainClass}</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

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