Building JAR that includes all its dependencies

2019-01-15 08:33发布

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.

I'm attempting to build an Ant library which depends on the TFS SDK. I followed the guide to 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:

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.

This answer seems 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?

标签: java ant jar
5条回答
别忘想泡老子
2楼-- · 2019-01-15 08:46

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

Extract required libraries into generated JAR

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

查看更多
冷血范
3楼-- · 2019-01-15 08:47

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 jarjar to 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).

The other option would be to use a dependency management system such as Maven or 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 Start to distribute to your end users).

查看更多
干净又极端
4楼-- · 2019-01-15 08:47

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

查看更多
闹够了就滚
5楼-- · 2019-01-15 08:51

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

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

查看更多
Luminary・发光体
6楼-- · 2019-01-15 09:02

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

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

查看更多
登录 后发表回答