Tag Archives: in XXX.jar

Run jar file under Linux system, prompt: no main manifest attribute, in xxx.jar [How to Solve]

When executing Java – jar xxx.jar com.helloworld under Linux system, you will often be prompted: no main manifest attribute, in xxx.jar

The reasons are as follows:

Normally, when Java is packaged as a jar package, you need to specify the main class item in manifest.mf to find the corresponding main class when running Java – jar xxx.jar. Because – jar means that the following jar package has a main class to run independently, you need to specify this class when you package it into a jar package

If you want to specify the class you want to run at runtime, you should use – CP/– classpath to specify it. The command is as follows:
for example: Java – CP xxx.jar com.helloworld

Packaging can also be specified in the following way to directly run the jar file Java – jar XXX. Jar

<plugins>
    <!-- When packaging the jar file, configure the manifest file and add the jar dependencies of the lib package -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
            <classesDirectory>target/classes/</classesDirectory>
            <archive>
                <manifest>
                    <mainClass>com.alibaba.dubbo.container.Main</mainClass>
                    <!-- Timestamped version not recorded in MANIFEST.MF file when packing -->
                    <useUniqueVersions>false</useUniqueVersions>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>crm-lib/</classpathPrefix>
                </manifest>
                <manifestEntries>
                    <Class-Path>.</Class-Path>
                </manifestEntries>
            </archive>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <type>jar</type>
                    <includeTypes>jar</includeTypes>
                    <outputDirectory>
                        ${project.build.directory}/crm-lib
                    </outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>