简体   繁体   中英

Running a jar file having multiple main classes using command line

Have added multiple main classes in jar using following code in pom.xml as follows

<groupId>com.test</groupId>
        <artifactId>indexer</artifactId>
        <version>1.0.0-SNAPSHOT</version>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <id>build-first</id>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.test1</mainClass>
                    </transformer>
                </transformers>
                <finalName>first-runnable</finalName>
            </configuration>
        </execution>
        <execution>
            <phase>package</phase>
            <id>build-second</id>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.test2</mainClass>
                    </transformer>
                </transformers>
                <finalName>second-runnable</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

But unable to run the jar file successfully from command line. Tried using the command line as
java -jar indexer-1.0.0-SNAPSHOT.jar com.test1 but get no main manifest attribute, in indexer-1.0.0-SNAPSHOT.jar

Could anyone please guide how to run the main classes from commandline using this jar.

Thanks

You should run produced artifact (first-runnable, second-runnable) instead of original one. If you use 'finalName' tag then original artifact will not be modified and new jar will be created as described in plugin documentation

Run

java -jar first-runnable.jar com.test1

or

java -jar second-runnable.jar com.test1

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