Eclipse: How to Build an Executable Jar with External Jar

Generating a Jar in Eclipse including external library

Eclipse has its own Jar export wizard for generate a runnable jar packed with required library or with the required library in a folder aside the jar.

Going in File ---> Export then choose Java - Runnable Jar

export

You can then choose how pack the jar and how handling libraries :

Jar packing

You can also save the ant script for later modification or use ...

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

Create executable Jar file under Eclipse

To create a new runnable JAR file in the workbench:

  1. From the menu bar's File menu, select Export.
  2. Expand the Java node and select Runnable JAR file. Click Next.
  3. In the Runnable JAR File
    Specification page, select a 'Java Application' launch configuration
    to use to create a runnable JAR.
  4. In the Export destination field, either type or click Browse to
    select a location for the JAR file.
  5. Select an appropriate library handling strategy.
  6. Optionally, you can also create an ANT script to quickly regenerate
    a previously created runnable JAR file.

Eclipse: How to build an executable jar using ant with external project dependencies?

The javac directive can have subelements that include a classpath. You can put your dependent project's JAR files on the classpath. Consider this example which demonstrates putting JAR files on the classpath:

//This goes inside the javac element     
<classpath>
<fileset dir="${dependentProject}/lib">
<include name="**/*.jar" />
</fileset>
</classpath>

As a second example, if you want to include class files from your dependent project, and your class files are located in the build directory:

 <classpath>
<pathelement location="${dependentProject}/build" />
<fileset dir="${dependentProject}/lib">
<include name="**/*.jar" />
</fileset>
</classpath>

Creating jar executable w/ external jar (JXL) in ECLIPSE java

1.) Right-click on project, select pop-up menu entry "Export...".

Pop-up menu -> Export

2.) Select "Java -> Runnable JAR file".

Sample Image

3.) Choose a working launch configuration for running your program via java -jar MyExecutable.jar from the command line later. Select an output folder and file name. Select "Package required libraries into generated JAR".

Sample Image

Using external Jar dependency in Eclipse with Non-Runnable Jar

There are two ways to do this:

  • Specify the dependent JAR in the "archive" parameter; e.g.

    ARCHIVE="myapplet.jar,jxl.jar"
  • Specify the dependent JAR in the manifest of your applet JAR; e.g. in the manifest.mf file add this line:

    Class-Path: jxl.jar

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.



Related Topics



Leave a reply



Submit