使用 Eclipse 将具有外部依赖项的 Java 项目导出到 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17729794/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:47:53  来源:igfitidea点击:

Exporting a Java project with external dependencies to a jar using Eclipse

javajar

提问by JME

Is there an easy way to export a Java project including all of its external dependencies to a standard .jar file?

是否有一种简单的方法可以将 Java 项目(包括其所有外部依赖项)导出到标准 .jar 文件?

I have developed an SDK that uses several Apache libraries and I would like to be able to ship this project as a single jar. The answers I have found thus far to this question require the project to be packaged as a Runnable jar, and does not apply to this project which has no main class.

我开发了一个使用多个 Apache 库的 SDK,我希望能够将此项目作为单个 jar 发布。到目前为止,我找到的这个问题的答案要求将项目打包为 Runnable jar,并且不适用于这个没有主类的项目。

The rest of the answers I have found assume familiarity with Ant or Maven to accomplish this task, and I have no background with either. There are several scripts out there but I don't understand how I would use/modify them, or what I would to run the script and get a result.

我找到的其余答案假设熟悉 Ant 或 Maven 来完成此任务,并且我没有任何背景。那里有几个脚本,但我不明白我将如何使用/修改它们,或者我将如何运行脚本并获得结果。

Any answer that can provide more background into how exactly a user with no background with Ant or Maven would do these tasks is greatly appreciated.

任何可以提供更多背景信息的答案都非常感谢没有 Ant 或 Maven 背景的用户将如何完成这些任务。

For more background information I have trying to combine the following libraries into one called JetstreamSDK.jar

有关更多背景信息,我尝试将以下库组合成一个名为 JetstreamSDK.jar 的库

commons-codec-1.6.jar
commons-configuration-1.9.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
httpclient-4.2.5.jar
httpcore-4.2.4.jar
JetstreamSDK-Java.jar
jibx-bind.jar
jibx-extras.jar
jibx-run.jar
xpp3.jar

回答by Bimalesh Jha

jarfile is like zipcompressed file. Usually Eclipse (default) java project will be organized like following directory structure:

jar文件就像zip压缩文件。通常 Eclipse(默认)java 项目将按照以下目录结构进行组织:

some_project
       |____ src/ <--- your java source files
       |____ bin/ <--- compiled class files
       |____ lib/ <--- 3rd party libs e.g. commons*.jar

To package above structure and distribute as a single jar (zip) file- go inside bin/directory and select everything inside and just right click (assuming you are working on windows) and send tocompressed (zipped) folder. Give a suitable name to the generated zip file e.g. your-sdk.jarthen right click on lib/and compress into a another zip together with your-sdk.jarand you are done. This can be accomplished using jarcommand line tool also- but, you have to read the jar options/help on how to do this.

要将上述结构打包并作为单个 jar (zip) 文件分发 - 进入bin/目录并选择其中的所有内容,然后右键单击(假设您在 Windows 上工作)和send to压缩(zipped)文件夹。为生成的 zip 文件指定一个合适的名称,例如,your-sdk.jar然后右键单击并lib/与 一起压缩成另一个 zip 文件your-sdk.jar,您就完成了。这也可以使用jar命令行工具来完成 -但是,您必须阅读有关如何执行此操作的 jar 选项/帮助。

回答by Mukesh S

1.) First compile your project

1.) 首先编译你的项目

2.) Right click on your PROJECT -> Export -> Java -> Runnable jar

2.) 右键单击​​您的项目 -> 导出 -> Java -> Runnable jar

3.) You will get a Dialog box, there select the class name (in which your main method resides)

3.) 你会得到一个对话框,在那里选择类名(你的主要方法所在的)

4.) Give the export destination

4.) 给出出口目的地

5.) In library handling(radio button): select your desired option(all are self descriptive)

5.) 在库处理中(单选按钮):选择您想要的选项(都是自我描述的)