47

I am trying to build an executable jar program which depends on external jar downloaded. In my project, I included them in the build path and can be run and debug within eclipse.

When I tried to export it to a jar, I can run the program but I can't when I try to press a button which includes function calls and classes from the external jar. I have edited the environment variables (Windows XP) CLASSPATH to include paths of all the external jar, but it doesn't work.

A point to note is that I got compile warnings while exporting my executable jar, but it doesn't show up any description about the warnings.

Would someone kindly provide a thorough guide on how to include an external jar program using eclipse?

4

6 回答 6

36

Eclipse 3.5 可以选择将所需的库打包到可运行的 jar 中。文件 -> 导出... 选择可运行的 jar 并单击下一步。可运行的 jar 导出窗口有一个单选按钮,您可以在其中选择将所需的库打包到 jar 中。

于 2010-04-29T17:53:19.633 回答
14

You can do this by writing a manifest for your jar. Have a look at the Class-Path header. Eclipse has an option for choosing your own manifest on export.

The alternative is to add the dependency to the classpath at the time you invoke the application:

win32: java.exe -cp app.jar;dependency.jar foo.MyMainClass
*nix:  java -cp app.jar:dependency.jar foo.MyMainClass
于 2009-02-02T12:08:34.557 回答
8

如何将项目的 jar 包含到可运行的 jar 中:

我正在使用 Eclipse 版本:在 Ubuntu 12.10 上运行的 3.7.2。我还将向您展示如何制作,build.xml以便您可以ant jar从命令行执行并创建您的 jar,并将其他导入的 jar 提取到其中。

基本上,您要求 Eclipse 为您构建将库导入 jar 的 build.xml。

  1. 启动 Eclipse 并创建一个新的 Java 项目,创建一个新包“mypackage”,添加您的主类:Runner 将这段代码放在那里。

    在此处输入图像描述

  2. 现在包括mysql-connector-java-5.1.28-bin.jar 来自 Oracle的,它使我们能够编写 Java 来连接到 MySQL 数据库。通过右键单击项目 -> 属性 -> java 构建路径 -> 添加外部 Jar -> 选择 mysql-connector-java-5.1.28-bin.jar 来执行此操作。

  3. 在 Eclipse 中运行程序,它应该会运行,并告诉您用户名/密码无效,这意味着 Eclipse 已正确配置了 jar。

  4. 在 Eclipse 中转到File-> Export-> Java-> Runnable Jar File。您将看到此对话框:

    在此处输入图像描述

    确保设置“另存为 ant 脚本”复选框。这就是它的原因,因此您可以使用命令行ant jar稍后进行操作。

  5. 然后去终端查看ant脚本:

    在此处输入图像描述

所以你看,我运行了 jar 并没有出错,因为它发现包含的mysql-connector-java-5.1.28-bin.jar嵌入在里面Hello.jar

查看 Hello.jar: 内部 vi Hello.jar,您会看到许多对com/mysql/jdbc/stuff.class

ant jar在命令行上自动执行所有这些操作:重命名buildant.xmlbuild.xml,并将目标名称从 更改create_run_jarjar

然后,从MyProject您的内部输入ant jar并发出声音。你在 MyProject 中有你的 jar。你可以使用它来调用它java -jar Hello.jar,这一切都有效。

于 2013-12-30T00:21:12.987 回答
5

作为一种好的做法,您可以使用Ant 脚本(Eclipse 自带)来生成 JAR 文件。在这个 JAR 中,您可以拥有所有依赖库。

您甚至可以将 MANIFEST 的 Class-path 标头设置为指向文件系统中的文件,但这不是一个好习惯。

Ant build.xml 脚本示例:

<project name="jar with libs" default="compile and build" basedir=".">
<!-- this is used at compile time -->
<path id="example-classpath">
    <pathelement location="${root-dir}" />
    <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</path>

<target name="compile and build">
    <!-- deletes previously created jar -->
    <delete file="test.jar" />

    <!-- compile your code and drop .class into "bin" directory -->
    <javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
        <!-- this is telling the compiler where are the dependencies -->
        <classpath refid="example-classpath" />
    </javac>

    <!-- copy the JARs that you need to "bin" directory  -->
    <copy todir="bin">
        <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
    </copy>

    <!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
    <jar destfile="test.jar" basedir="bin" duplicate="preserve">
        <manifest>
            <!-- Who is building this jar? -->
            <attribute name="Built-By" value="${user.name}" />
            <!-- Information about the program itself -->
            <attribute name="Implementation-Vendor" value="ACME inc." />
            <attribute name="Implementation-Title" value="GreatProduct" />
            <attribute name="Implementation-Version" value="1.0.0beta2" />
            <!-- this tells which class should run when executing your jar -->
            <attribute name="Main-class" value="ApplyXPath" />
        </manifest>
    </jar>
</target>

于 2009-02-04T15:22:34.850 回答
2

Try the fat-jar extension. It will include all external jars inside the jar.

于 2009-02-02T12:04:16.110 回答
0

看@ java-jar-ignores-classpath-Workaround

于 2010-02-17T08:42:51.193 回答