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?.
问题:
回答1:
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.
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 maven, cannot be included (for maven projects, search here).
回答2:
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
回答3:
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\'.
回答4:
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.
回答5:
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\").
回答6:
To generate jar file in eclipse right click on the project for which you want to generate, Select 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.
回答7:
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
回答8:
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.