使用外部类路径运行可执行JAR [英] Run an executable JAR with external class path

查看:60
本文介绍了使用外部类路径运行可执行JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Maven,我将项目编译到一个JAR中,该JAR包含除了一个大依赖项以外的所有依赖项.依赖关系的包含是使用以下方式完成的:

Using Maven I compiled my project into a JAR that includes all the dependencies except for one big dependecy. The inclusion of the dependecies is done using:

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.1</version>
<configuration>
    <archive>
      <manifest>
        <mainClass>com.mypackage.Main</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>

排除依赖项是通过<scope>provided</scope>

目标myjar.jarBigExternalJar.jar在同一文件夹中,但是当我尝试运行时:

The target myjar.jar is in the same folder as BigExternalJar.jar, but when I try to run:

java -cp ".:BigExternalJar.jar:myjar.jar" -jar myjar.jar

对于缺少的类,我会得到一个例外(这些类来自BigExternalJar.jar).

I get an exception for missing classes (those classes are from BigExternalJar.jar).

如何仅使用Maven 将一个依赖包打包到一个JAR中,但仍然能够在classpath中添加其他JAR?请注意,BigExternalJar并不总是位于同一文件夹中,因此我无法将其手动添加到MANIFEST文件中.

How can one pack dependencies into a JAR, using Maven only, but still be able to add additional JARs in classpath? Note that the BigExternalJar is not always in the same folder so I cannot add it manually to the MANIFEST file.

有两个类似的问题可能看起来很重复,但是它们并不能解决这种情况. Eclipse:如何使用外部jar构建可执行jar? 并且 运行具有外部依赖关系的可执行JAR

There are two similar questions that might look duplicate but they do not have an answer to this situation. Eclipse: How to build an executable jar with external jar? AND Running a executable JAR with external dependencies

推荐答案

如果使用-jar选项,则将忽略classpath参数.仅使用清单中提供的类路径.

The classpath argument is ignored if you use the -jar option. Only the classpath provided in the manifest is used.

这篇关于使用外部类路径运行可执行JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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