使用命令行运行具有多个主类的jar文件 [英] Running a jar file having multiple main classes using command line

查看:107
本文介绍了使用命令行运行具有多个主类的jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下pom.xml中的代码在jar中添加了多个主类

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>

但是无法从命令行成功运行jar文件.使用命令行尝试为
java -jar indexer-1.0.0-SNAPSHOT.jar com.test1 但是在indexer-1.0.0-SNAPSHOT.jar 中没有没有主清单属性"

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

任何人都可以指导如何使用此jar从命令行运行主类.

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

谢谢

推荐答案

您应该运行生成的工件(first-runnable,second-runnable),而不是原始的工件. 如果您使用'finalName'标记,则原始工件将不会被修改,并且将按照

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

运行

java -jar first-runnable.jar com.test1

java -jar second-runnable.jar com.test1

这篇关于使用命令行运行具有多个主类的jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆