How to create a jar with external libraries includ

2018-12-31 16:02发布

I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.

标签: java eclipse jar
8条回答
十年一品温如言
2楼-- · 2018-12-31 16:09

If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").

查看更多
孤独寂梦人
3楼-- · 2018-12-31 16:12

Personally,

None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.

查看更多
妖精总统
4楼-- · 2018-12-31 16:13

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.

Or

If you have a web project then put all the jars in web-inf/lib folder and do the same step.

查看更多
梦醉为红颜
5楼-- · 2018-12-31 16:21

You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.

查看更多
深知你不懂我心
6楼-- · 2018-12-31 16:22

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. 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.

Package libraries

The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.

Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). 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).

查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 16:23

To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,

enter image description here

enter image description here

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.

查看更多
登录 后发表回答