简体   繁体   中英

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. By "self-contained" I mean that all dependenies of this project (ie 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.

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>
    <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?

Cheers,

Martin

It may help someone

1.Select Export from Project Properties

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

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM