简体   繁体   English

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

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

I am trying to create a self-contained .jar file from an Eclipse project. 我正在尝试从Eclipse项目创建一个自包含的.jar文件。 By "self-contained" I mean that all dependenies of this project (ie other jars) should be included in the generated jar. 通过“自包含”,我的意思是该项目的所有依赖项(即其他jar)应该包含在生成的jar中。 I would like to use the exported jar in another project as a library. 我想在另一个项目中使用导出的jar作为库。 The problem is that I get a NoClassDefFoundError for one of the classes from the dependencies. 问题是我从依赖项中获取了一个类的NoClassDefFoundError。 I have tried exporting both a runnable jar with all dependencies packaged into it and a "normal" jar. 我已经尝试导出一个包含所有依赖项的runnable jar和一个“普通”jar。 Neither of these works. 这些都不奏效。

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

Cheers, 干杯,

Martin 马丁

It may help someone 它可能会帮助某人

1.Select Export from Project Properties 1.从项目属性中选择导出

1.选择“从项目属性导出”

2.Select Runnable Jar option 2.选择Runnable Jar选项

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.完成创作。

I would seriously consider using Maven or a similar tool like Gradle to manage the dependencies of the different projects for you; 我会认真考虑使用Maven或像Gradle这样的类似工具来管理不同项目的依赖关系; 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; 将不同的依赖项塞入一个jar中是非常不好的主意,因此您不必再将它们包含在其他项目中; that is a recipe for dependency hell, library clashes and maybe even licensing conflicts. 这是依赖地狱,图书馆冲突甚至许可冲突的秘诀。

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; 在主应用程序jar的清单中存在该类路径行将表明您所期望的事实上并非顺便发生; the dependencies are externally referenced so I'm going to make a guess that the classes are not inside the application jar. 外部引用依赖项,所以我要猜测类不在应用程序jar中。

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. 只是回答这个问题:最后,我们决定将依赖项的jar直接包含在项目中,并避免在jar中使用havign jar。 Using Mavin is also being considered, but would require some substantial chagnes to the way our projects are setup. 正在考虑使用Mavin,但是我们的项目设置方式需要一些实质性的挑战。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Eclipse将Java项目导出为JAR,并带有其他引用的jar - Export Java project from Eclipse as JAR with additional referenced jars 如何从 Eclipse 导出具有所有依赖项的可运行 JAR 文件? - How to export runnable JAR file from Eclipse with all dependencies? 在没有main方法(使用外部jar)的情况下将Java项目导出到Eclipse中的Jar - Export a Java project without main method (with external jars) to Jar in Eclipse 如何导出具有外部Jar依赖项的Eclipse项目? - How to export an Eclipse Project with External Jar dependencies? 在Eclipse中生成具有依赖项(外部jar)的可执行.jar - Generating an executable .jar with dependencies (external jars) in Eclipse 在Maven中创建具有外部依赖项和其他jar的jar - Creating jar with external dependencies and other jars in Maven 如何将eclipse中的java项目导出到带有图像的jar文件中 - How to export java project from eclipse into a jar file with images Java从Eclipse导出为可运行的JAR文件 - Java export as runnable JAR file from Eclipse 如何在Eclipse中使用其他外部jar依赖项创建一个不可运行/不可执行的jar - how to create a not runnable/not executable jar with other external jar dependencies in Eclipse 在OSGi / Eclipse jar中打包具有依赖性的Java库 - packaging Java library with dependencies in OSGi/Eclipse jars
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM