How to Create a Jar with External Libraries Included in Eclipse

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

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

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.

How can i export a jar file with libraries in eclipse? (Not executable jar)

If the jar is not running try to run it using a CLI for example command prompt java -jar <name>.jar and see the errors.

When you export the .jar there are 3 options .

  • One and two produces the .jar with the libraries inside.
  • Third option is producing a .jar (executable) with the libraries added
    a different folder[ which has the same name as the jar file and it
    must be named like it].

Mention that:

You may have serious problems and your executable will not run if your application has duplicate entries. So most of the time the third bullet is the best solution.

Sample image:

Sample Image


If you are trying to create a .jar to add it as a library to other projects then you should follow the below:

Sample Image

Sample Image

How can I export jar file from Eclipse with the referenced libraries?

I would post this as a comment, but my reputation does not allow.

Can you not just add a blank main method?

How to import a jar in Eclipse?

You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar. A quick demo here.

Sample Image

The above solution is obviously a "Quick" one. However, if you are working on a project where you need to commit files to the source control repository, I would recommend adding Jar files to a dedicated library folder within your source control repository and referencing few or all of them as mentioned above.

How to create a jar file from an eclipse java project that uses *.jar files added in Classpath

Export your project as a "Runnable" Jar file (right-click project folder from within Eclipse, select "Export..."). When you configure the export settings, be sure to select "Extract required libraries into generated Jar."



Related Topics



Leave a reply



Submit