Eclipse:如何使用外部jar构建可执行jar?

时间:2023-01-13 13:27:24

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.

我正在尝试构建一个可执行的jar程序,它取决于下载的外部jar。在我的项目中,我将它们包含在构建路径中,并且可以在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.

当我尝试将它导出到jar时,我可以运行程序,但是当我尝试按下包含来自外部jar的函数调用和类的按钮时,我无法运行。我编辑了环境变量(Windows XP)CLASSPATH以包含所有外部jar的路径,但它不起作用。

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.

需要注意的一点是,我在导出可执行jar时遇到了编译警告,但它没有显示有关警告的任何描述。

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

有人会提供一个关于如何使用eclipse包含外部jar程序的详尽指南吗?

6 个解决方案

#1


36  

Eclipse 3.5 has an option to package required libraries into the runnable jar. File -> Export... Choose runnable jar and click next. The runnable jar export window has a radio button where you can choose to package the required libraries into the jar.

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

#2


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.

您可以通过为jar创建清单来完成此操作。看看Class-Path标头。 Eclipse可以选择在导出时选择自己的清单。

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

#3


8  

How to include the jars of your project into your runnable jar:

如何将项目的jar包含到runnable jar中:

I'm using Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.

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

Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.

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

  1. Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.

    启动Eclipse并创建一个新的Java项目,创建一个新的包'mypackage',添加您的主类:Runner将此代码放在那里。

    Eclipse:如何使用外部jar构建可执行jar?

  2. Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.

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

  3. Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.

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

  4. In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:

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

    Eclipse:如何使用外部jar构建可执行jar?

    Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.

    确保设置“另存为ant脚本”复选框。这就是为什么你可以使用命令行稍后做一个ant jar。

  5. Then go to the terminal and look at the ant script:

    然后转到终端并查看ant脚本:

    Eclipse:如何使用外部jar构建可执行jar?

So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.

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

Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class

查看Hello.jar:vi Hello.jar,你会看到许多对com / mysql / jdbc / stuff.class的引用

To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.

要在命令行上执行ant jar以自动完成所有这些操作:将buildant.xml重命名为build.xml,并将目标名称从create_run_jar更改为jar。

Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.

然后,在MyProject中输入ant jar和boom。你在MyProject中有你的jar。你可以使用java -jar Hello.jar来调用它,一切正常。

#4


5  

As a good practice you can use an Ant Script (Eclipse comes with it) to generate your JAR file. Inside this JAR you can have all dependent libs.

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

You can even set the MANIFEST's Class-path header to point to files in your filesystem, it's not a good practice though.

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

Ant build.xml script example:

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>

#5


2  

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

尝试fat-jar扩展。它将包括jar内的所有外部罐子。

#6


0  

look @ java-jar-ignores-classpath-Workaround

看看@ java-jar-ignores-classpath-解决方法

#1


36  

Eclipse 3.5 has an option to package required libraries into the runnable jar. File -> Export... Choose runnable jar and click next. The runnable jar export window has a radio button where you can choose to package the required libraries into the jar.

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

#2


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.

您可以通过为jar创建清单来完成此操作。看看Class-Path标头。 Eclipse可以选择在导出时选择自己的清单。

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

#3


8  

How to include the jars of your project into your runnable jar:

如何将项目的jar包含到runnable jar中:

I'm using Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the build.xml so you can do the ant jar from command line and create your jar with other imported jars extracted into it.

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

Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.

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

  1. Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class: Runner Put this code in there.

    启动Eclipse并创建一个新的Java项目,创建一个新的包'mypackage',添加您的主类:Runner将此代码放在那里。

    Eclipse:如何使用外部jar构建可执行jar?

  2. Now include the mysql-connector-java-5.1.28-bin.jar from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.

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

  3. Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.

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

  4. In Eclipse go to File -> Export -> Java -> Runnable Jar File. You will see this dialog:

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

    Eclipse:如何使用外部jar构建可执行jar?

    Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an ant jar later.

    确保设置“另存为ant脚本”复选框。这就是为什么你可以使用命令行稍后做一个ant jar。

  5. Then go to the terminal and look at the ant script:

    然后转到终端并查看ant脚本:

    Eclipse:如何使用外部jar构建可执行jar?

So you see, I ran the jar and it didn't error out because it found the included mysql-connector-java-5.1.28-bin.jar embedded inside Hello.jar.

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

Look inside Hello.jar: vi Hello.jar and you will see many references to com/mysql/jdbc/stuff.class

查看Hello.jar:vi Hello.jar,你会看到许多对com / mysql / jdbc / stuff.class的引用

To do ant jar on the commandline to do all this automatically: Rename buildant.xml to build.xml, and change the target name from create_run_jar to jar.

要在命令行上执行ant jar以自动完成所有这些操作:将buildant.xml重命名为build.xml,并将目标名称从create_run_jar更改为jar。

Then, from within MyProject you type ant jar and boom. You've got your jar inside MyProject. And you can invoke it using java -jar Hello.jar and it all works.

然后,在MyProject中输入ant jar和boom。你在MyProject中有你的jar。你可以使用java -jar Hello.jar来调用它,一切正常。

#4


5  

As a good practice you can use an Ant Script (Eclipse comes with it) to generate your JAR file. Inside this JAR you can have all dependent libs.

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

You can even set the MANIFEST's Class-path header to point to files in your filesystem, it's not a good practice though.

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

Ant build.xml script example:

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>

#5


2  

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

尝试fat-jar扩展。它将包括jar内的所有外部罐子。

#6


0  

look @ java-jar-ignores-classpath-Workaround

看看@ java-jar-ignores-classpath-解决方法