如何从 Eclipse 导出 Java jar 并依赖于其他 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19785804/
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-12 20:12:11  来源:igfitidea点击:

How export Java jar from Eclipse with dependencies on other jars?

javaeclipsejarexportnoclassdeffounderror

提问by marw

I am trying to create a self-contained .jar file from an Eclipse project. By "self-contained" I mean that all dependenies of this project (i.e. other jars) should be included in the generated jar. I would like to use the exported jar in another project as a library. The problem is that I get a NoClassDefFoundError for one of the classes from the dependencies. I have tried exporting both a runnable jar with all dependencies packaged into it and a "normal" jar. Neither of these works.

我正在尝试从 Eclipse 项目创建一个独立的 .jar 文件。“自包含”是指该项目的所有依赖项(即其他 jar)都应包含在生成的 jar 中。我想在另一个项目中使用导出的 jar 作为库。问题是我从依赖项中得到一个类的 NoClassDefFoundError 。我已经尝试导出一个可运行的 jar 并将所有依赖项打包到其中和一个“正常”的 jar。这些都不起作用。

The manifest looks like this:

清单如下所示:

Manifest-Version: 1.0
Main-Class: com.path.to.MyMainClass
Class-Path: lib/resolver.jar lib/xercesImpl.jar lib/xml-apis.jar lib/Mac-Cocoa64/swt.jar  

The .classpath look like this:

.classpath 如下所示:

<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="lib" path="lib/resolver.jar"/>
    <classpathentry kind="lib" path="lib/xercesImpl.jar"/>
    <classpathentry kind="lib" path="lib/xml-apis.jar"/>
    <classpathentry kind="lib" path="lib/Mac-Cocoa64/swt.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

The missing class def is from the SWT.jar file. Any idea what might be wrong or what steps I need to take in order to successfully export this jar so that it can be included elsewhere?

缺少的类 def 来自 SWT.jar 文件。知道什么可能是错误的,或者我需要采取哪些步骤才能成功导出这个 jar 以便它可以包含在其他地方?

Cheers,

干杯,

Martin

马丁

回答by Gimby

I would seriously consider using Maven or a similar tool like Gradle to manage the dependencies of the different projects for you; it is very much not a good idea to cram together different dependencies into one jar just so you don't have to include them in other projects again; that is a recipe for dependency hell, library clashes and maybe even licensing conflicts.

我会认真考虑使用 Maven 或类似 Gradle 之类的工具来为您管理不同项目的依赖项;将不同的依赖项塞进一个 jar 中,这样您就不必再次将它们包含在其他项目中,这非常不是一个好主意;这是依赖地狱、库冲突甚至许可冲突的秘诀。

The existence of that class-path line in your main application jar's manifest would be an indication that what you expect has in fact not happened by the way; the dependencies are externally referenced so I'm going to make a guess that the classes are not inside the application jar.

在您的主应用程序 jar 的清单中存在该类路径行将表明您所期望的实际上并没有发生;依赖项是外部引用的,因此我将猜测这些类不在应用程序 jar 中。

回答by marw

Just to answer this question: In the end, we decided to include the jars of the dependencies directly in the project and avoid havign jars in jars. Using Mavin is also being considered, but would require some substantial chagnes to the way our projects are setup.

简单回答一下这个问题:最后,我们决定将依赖的jars直接包含在项目中,避免在jars中使用havign jars。也正在考虑使用 Mavin,但需要对我们的项目设置方式进行一些实质性的修改。

回答by sunil

It may help someone

它可能会帮助某人

1.Select Export from Project Properties

1.从项目属性中选择导出

1. Select Export from Project Properties

1. 从项目属性中选择导出

2.Select Runnable Jar option

2.选择Runnable Jar选项

2. Runnable Jar option

2. Runnable Jar 选项

3.Finish the JAR creation (Here, you have the option to package all dependencies into the generated jar.)

3.完成JAR的创建(这里可以选择将所有依赖打包到生成的jar中)

3. Finish the creation.

3. 完成创建。