Java 构建包含所有依赖项的 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9722257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:43:39  来源:igfitidea点击:

Building JAR that includes all its dependencies

javaantjar

提问by Greg Beech

This is probably a really fundamental question, but I'm afraid I don't know much about Java and I couldn't find the answer anywhere.

这可能是一个非常基本的问题,但恐怕我对 Java 了解不多,而且在任何地方都找不到答案。

I'm attempting to build an Ant library which depends on the TFS SDK. I followed the guideto setting up a project, but when I export it as a JAR and try to run a task using ANT I get the following error:

我正在尝试构建一个依赖于 TFS SDK 的 Ant 库。我按照指南设置了一个项目,但是当我将它导出为 JAR 并尝试使用 ANT 运行任务时,我收到以下错误:

java.lang.NoClassDefFoundError: /com/microsoft/tfs/core/util/TFSUser

I realise I could put the TFS SDK JAR in my ANT lib folder, but if possible I'd like my JAR to include it and the library just work without having to do so.

我意识到我可以将 TFS SDK JAR 放在我的 ANT lib 文件夹中,但如果可能的话,我希望我的 JAR 包含它,并且该库无需这样做即可工作。

This answerseems to say it's possible to include all the resources needed to run using Eclipse (I'm using 3.7.2) but it doesn't detail how to actually do it. What is the option in Eclipse to do so?

这个答案似乎说可以包含使用 Eclipse 运行所需的所有资源(我使用的是 3.7.2),但它没有详细说明如何实际执行。Eclipse 中有什么选项可以这样做?

采纳答案by Java42

Select "Extract required libraries into generated JAR" as you do the export.

导出时选择“将所需的库提取到生成的 JAR 中”。

Extract required libraries into generated JAR

将所需的库提取到生成的 JAR 中

Select "Extract required libraries into generated JAR" as you do the export.

导出时选择“将所需的库提取到生成的 JAR 中”。

回答by Thorbj?rn Ravn Andersen

Use File -> Export -> Java -> Runnable JAR file instead from Eclipse.

使用 File -> Export -> Java -> Runnable JAR 文件代替 Eclipse。

The "Extract required libraries into generated JAR" should be what you need.

“将所需的库提取到生成的 JAR 中”应该是您所需要的。

回答by Michael Lloyd Lee mlk

When you build a jar you get a JAR containing just your code, and not any dependencies your Jar requires. You could use something like jarjarto combine all the dependencies into one easy to manage Jar file or copy all the depend JARs into a folder for ease of use. It looks like Eclipse has options to also do this kind of thing (see posts above).

当您构建 jar 时,您会得到一个仅包含您的代码的 JAR,而不包含您的 Jar 所需的任何依赖项。您可以使用jarjar 之类的工具将所有依赖项组合到一个易于管理的 Jar 文件中,或者将所有依赖项 JAR 复制到一个文件夹中以方便使用。看起来 Eclipse 也可以选择做这种事情(见上面的帖子)。

The other option would be to use a dependency management system such as Mavenor Ivy. This has a higher learning curve, but for a library it is worthwhile as it will allow users of your library to easy grab all the dependencies. For an end user application then a single distributable is likely a better option (for which you could use Maven or Ivy to internally manage the dependencies and then something like jarjar or Java Web Startto distribute to your end users).

另一种选择是使用依赖管理系统,例如MavenIvy。这具有更高的学习曲线,但对于库来说这是值得的,因为它可以让您的库的用户轻松获取所有依赖项。对于最终用户应用程序,单个可分发文件可能是更好的选择(为此,您可以使用 Maven 或 Ivy 在内部管理依赖项,然后使用 jarjar 或Java Web Start 之类的东西分发给您的最终用户)。

回答by ch4nd4n

You would need to depend on classpath attribute in manifest file. This explained well at How to package libraries into my jar using Ant

您需要依赖清单文件中的类路径属性。这很好地解释了如何使用 Ant 将库打包到我的 jar 中

回答by Mayur

Just in case if you're doing with maven. You need to include following plugin.

以防万一,如果您正在使用 maven。您需要包含以下插件。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <properties>
            <property>
                <name>listener</name>
                <value>com.example.TestProgressListener</value>
            </property>
        </properties>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Read more @ how do I build JAR file with dependencies?

阅读更多@如何构建具有依赖项的 JAR 文件?