简体   繁体   English

如何使用 Eclipse 中包含的外部库创建 jar?

[英]How to create a jar with external libraries included in Eclipse?

I am done with the project which connects to database (MySQL).我完成了连接到数据库 (MySQL) 的项目。 Now I want to export the project as jar. But I don't know how to include its external dependencies?现在我想将项目导出为 jar。但是我不知道如何包含它的外部依赖项? Is there any way of doing it in Eclipse or should I use any scripts for that?.在 Eclipse 中有什么方法可以做到这一点,或者我应该为此使用任何脚本吗?。

When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar.当您将项目导出为“可运行 jar”(在项目上单击鼠标右键 -> 导出 -> 可运行 jar)时,您可以选择将所有依赖项打包到生成的 jar 中。 It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.它还有两种其他方式(见截图)来导出你的库,在决定你将使用哪种打包方法时要注意许可证。

包库

The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. “启动配置”下拉列表中填充了包含main(String[])方法的类。 The selected class is started when you 'run' the jar.当您“运行”jar 时,将启动选定的类。

Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...).导出为可运行的 jar 使用构建路径上的依赖项(右键单击项目 -> 构建路径 -> 配置构建路径...)。 When you export as a 'regular' (non-runnable) jar you can select any file in your project(s).当您导出为“常规”(不可运行)jar 时,您可以选择项目中的任何文件。 If you have the libraries in your project folder you can include them but external dependencies, for example , cannot be included (for maven projects, search here ).如果您的项目文件夹中有库,则可以包含它们,但不能包含外部依赖项,例如 (对于 maven 项目,请在此处搜索)。

You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies您可以使用 Export->Java->Runnable Jar 创建一个包含其依赖项的 jar

Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together或者,您也可以使用fatjar eclipse 插件将 jar 捆绑在一起

You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'.您可以右键单击项目,单击导出,键入“jar”,选择“Runnable JAR File Export”。 There you have the option 'Extract required libraries into generated JAR'.在那里您可以选择“将所需的库提取到生成的 JAR 中”。

Personally,亲身,

None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies).上面的答案都不适合我,我仍然不断收到 NoClassDefFound 错误(我使用 Maven 作为依赖项)。 My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates.我的解决方案是使用“mvn clean install”构建并使用该命令创建的“[project]-jar-with-dependencies.jar”。 Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.同样,在 Eclipse 中,您可以右键单击项目 -> 运行方式 -> Maven 安装,它将把 jars 放在目标文件夹中。

If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (eg 7-Zip), navigate to the /WEB-INF/lib/ folder.如果要导出 Java 网络项目的所有 JAR 文件,请使用 ZIP 工具(例如 7-Zip)打开最新生成的 WAR 文件,导航到 /WEB-INF/lib/ 文件夹。 Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").在这里,您将找到该项目所需的所有 JAR 文件(如“参考库”中所列)。

While exporting your source into a jar, make sure you select runnable jar option from the options.将源代码导出到 jar 时,请确保从选项中选择可运行的 jar 选项。 Then select if you want to package all the dependency jars or just include them directly in the jar file.然后选择是要打包所有的依赖jar还是直接将它们包含在jar文件中。 It depends on the project that you are working on.这取决于您正在从事的项目。

You then run the jar directly by java -jar example.jar.然后直接通过 java -jar example.jar 运行 jar。

To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,在eclipse中生成jar文件,在要生成的项目上右键,选择Export>Java>Runnable Jar File,

在此处输入图片说明

在此处输入图片说明

Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.它的创建 jar 包含来自 Pom.xml 的所有依赖项,但如果您的应用程序使用第三方依赖项,请确保许可问题。

If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.如果它是一个独立的(Main 方法)java 项目,那么 Not any specific path 把所有的 jars 放在项目中而不是任何特定路径然后右键单击项目 -> export -> Runnable jar --> 选择午餐配置和库处理然后选择单选按钮选项“将所需的库打包到生成的 jar 中”--> 完成。

Or或者

If you have a web project then put all the jars in web-inf/lib folder and do the same step.如果你有一个 web 项目,那么把所有的 jars 放在 web-inf/lib 文件夹中并执行相同的步骤。

Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library.导出前,点击Buildpath,配置java buildpath,在库中添加external jars。 Then try to export as a runnable jar.然后尝试导出为可运行的 jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM