Java: Export to an .Jar File in Eclipse

Java: export to an .jar file in eclipse

No need for external plugins. In the Export JAR dialog, make sure you select all the necessary resources you want to export. By default, there should be no problem exporting other resource files as well (pictures, configuration files, etc...), see screenshot below.
JAR Export Dialog

Export jar (eclipse) while keeping external lib and source files

Create a folder inside your project (parallel to src) and add all external libraries to it. Reference these libraries in your project (by adding them to build path).

Include them in your jar while exporting it with runnable jar. It should work.

Note: Never use external jar files using absolute path in your project. Always use relative path reference, so that the project works fine when shipped somewhere else.

Exporting jar from eclipse with classpaths...How do you do it?

If you want a self-contained JAR file, do

File->Export->Runnable JAR file (from Java section)

It will prompt you for existing launch configuration, and will package all necessary dependencies into the uber-jar.

I would only use this option as a one-off quick-and-dirty solution, and for production would use a build management system, such as Maven or Gradle.

How to create a jar with external libraries included in 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. 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 maven, cannot be included (for maven projects, search here).

Eclipse: Is there a way to quickly export JARs

How to add custom shortcuts

To add or change shortcuts open: Windows->Preferences->General->Keys.
Here you can find all the Eclipse commands and their associated
shortcut keys.

Reference

You can add a custom binding in eclipse to export your jar using a keyboard shortcut.

Example

I added bindings both for Runnable and non-runnable jar:

  • Export Jar: CTRL+SHIFT+J
  • Export Runnable Jar: CTRL+SHIFT+R

Sample Image

How to do with Ant File

If you want the JAR to be generated automatically you can look at the following question:

  • Build project into a JAR automatically in Eclipse

How to do with Batch file run from Button

You can use the combination of the following two answers:

  • Create jar file from command line
  • In Eclipse, how can I add a toolbar button to run a batch file?

Basically create a batch file that makes the JAR... add a button that executes it.



Related Topics



Leave a reply



Submit