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
You can then choose how pack the jar and how handling libraries :
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.
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:
- From the menu bar's File menu, select Export.
- Expand the Java node and select Runnable JAR file. Click Next.
- In the Runnable JAR File
Specification page, select a 'Java Application' launch configuration
to use to create a runnable JAR. - In the Export destination field, either type or click Browse to
select a location for the JAR file. - Select an appropriate library handling strategy.
- 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...".
2.) Select "Java -> Runnable JAR file".
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".
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
Java: Syntax and Meaning Behind "[B@1Ef9157"? Binary/Address
Most Efficient Conversion of Resultset to JSON
How to Get the Intersection Between Two Arrays as a New Array
What's the C++ Idiom Equivalent to the Java Static Block
What Is Ld_Library_Path and How to Use It
Very Confused by Java 8 Comparator Type Inference
Copy a Stream to Avoid "Stream Has Already Been Operated Upon or Closed"
What HTML Parsing Libraries Do You Recommend in Java
Jsp - What Is Wrong with Scriptlets, and What to Use Instead
Verification of Element in Viewport in Selenium
Spring 4 Loading Static Resources
How to Load a Java Class Dynamically on Android/Dalvik
Which Cipher Suites to Enable for Ssl Socket