Java Eclipse:如何使用外部 jar 构建可执行 jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/502960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Eclipse: How to build an executable jar with external jar?
提问by
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 程序的详尽指南吗?
回答by Marius
Try the fat-jar extension. It will include all external jars inside the jar.
尝试 fat-jar 扩展。它将包括罐子内的所有外部罐子。
- Update url: http://kurucz-grafika.de/fatjar
- Homepage: http://fjep.sourceforge.net/
回答by McDowell
You can do this by writing a manifestfor your jar. Have a look at the Class-Pathheader. 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
回答by Lucas Pottersky
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>
回答by Moro
回答by Bob Tefft
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 可以选择将所需的库打包到可运行的 jar 中。文件 -> 导出...选择可运行的 jar 并单击下一步。runnable jar 导出窗口有一个单选按钮,您可以在其中选择将所需的库打包到 jar 中。
回答by Eric Leschinski
How to include the jars of your project into your runnable jar:
如何将项目的 jars 包含到可运行的 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 中。
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class:
Runner
Put this code in there.Now include the
mysql-connector-java-5.1.28-bin.jar
from Oraclewhich 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.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.
In Eclipse go to
File
->Export
->Java
->Runnable Jar File
. You will see this dialog: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.Then go to the terminal and look at the ant script:
启动 Eclipse 并创建一个新的 Java 项目,创建一个新的包“mypackage”,添加您的主类:
Runner
将这段代码放在那里。现在包括
mysql-connector-java-5.1.28-bin.jar
from Oracle,它使我们能够编写 Java 以连接到 MySQL 数据库。为此,请右键单击项目 -> 属性 -> java 构建路径 -> 添加外部 Jar -> 选择 mysql-connector-java-5.1.28-bin.jar。在 eclipse 中运行该程序,它应该会运行,并告诉您用户名/密码无效,这意味着 Eclipse 已正确配置了 jar。
在 Eclipse 中,转到
File
->Export
->Java
->Runnable Jar File
。您将看到此对话框:确保设置“另存为 ant 脚本”复选框。这就是它的原因,因此您可以使用命令行
ant jar
稍后执行。然后到终端查看ant脚本:
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 并且它没有出错,因为它找到了mysql-connector-java-5.1.28-bin.jar
嵌入的 .jar 文件Hello.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
内心打字和繁荣。你的 jar 已经在 MyProject 中了。您可以使用java -jar Hello.jar
它来调用它,并且一切正常。