include external jar when running java -jar
Is there a reason why you are avoiding invoking the main class like
java -cp /usr/local/jar/foobar.jar:/some/other/path.jar com.your.main.classname
?
This type of invocation allows you to mix absolute paths with relative paths. Put this into a shell script or batch file to avoid having to actually type or remember the full classpath to simplify things.
including external jar while bulding my application jar
I use an Ant script (Ant is a build tool included with Eclipse) to package the application and all external JAR files into one executable JAR. There may be ways to make this easier (Maven?) but this has worked well for me. This is set up to work with the following project structure (you should adjust the build script as necessary if different):
build.xml
/src
/com
/mycompany
/myproject
[source files]
/META-INF
MANIFEST.MF
/lib
jsoup-1.7.2.jar
jxl-2.6.10.jar
In Eclipse:
Add a build.xml file to your project root (this is recognized by
Eclipse as an Ant buildfile). Here's one I use - change the
"location" values near the top as necessary to point to your source root, etc. Note that the /build and /dist folders are created when the script runs (your compiled JAR will be in the /dist folder).<?xml version="1.0" encoding="UTF-8"?> <!-- XML file header -->
<!-- Define the Ant project name and default task. The project name goes into a
variable named "ant.project.name". The default task is the one that will be
run automatically by choosing Run As -> Ant Build from the context menu in
Eclipse -->
<project name="MyProject" default="package">
<!-- Set variables for folder paths that will be used later in the script.
The "name" attribute defines the name of the variable (e.g source.dir).
The "location" attribute is the relative path of the folder (from the
project root). -->
<property name="source.dir" location="src"/>
<property name="bin.dir" location="bin"/>
<property name="lib.dir" location="lib"/>
<property name="build.dir" location="build"/>
<property name="dist.dir" location="dist"/>
<!-- Define the "package" task, which depends on the "clean" task (meaning
"clean" will be run automatically when "package" is invoked). -->
<target name="package" depends="clean" description="Remake the jarfile from scratch">
<!-- Make a folder with the name in the build.dir variable ("/build") -->
<mkdir dir="${build.dir}" />
<!-- Make the "/dist" folder, into which the compiled JAR will go -->
<mkdir dir="${dist.dir}" />
<!-- Unzip any JAR files from the "/lib" folder into "/build" -->
<unzip dest="${build.dir}">
<fileset dir="${lib.dir}" includes="*.jar" />
</unzip>
<!-- Copy everything from "/bin" to "/build" -->
<copy todir="build">
<fileset dir="${bin.dir}" includes="**/*.*" />
</copy>
<!-- Set the location of the JAR manifest in the "manifest.mf"
variable. -->
<property name="manifest.mf" location="${build.dir}/META-INF/MANIFEST.MF"/>
<!-- Create a JAR file from everything in "/build". Set the JAR
manifest to the file at the location contained in the
"manifest.mf" variable. The completed JAR will be located in
the "/dist" folder and will be named "MyProject.jar". -->
<jar destfile="${dist.dir}/${ant.project.name}.jar" duplicate="preserve" manifest="${manifest.mf}">
<fileset dir="${build.dir}"/>
</jar>
<!-- Delete the "/build" folder -->
<delete dir="${build.dir}"/>
</target>
<!-- Define the "clean" task, which deletes any old "/build"
and "/dist" folders -->
<target name="clean" description="Delete the working build and distribution folders">
<!-- Delete the "/build" folder -->
<delete dir="${build.dir}"/>
<!-- Delete the "/dist" folder -->
<delete dir="${dist.dir}"/>
</target>
</project>If they aren't already, copy the external JAR files into the /lib folder off your project root.
In your /src folder, add a folder titled META-INF. Into this folder, place a file named MANIFEST.MF that contains the following:
Manifest-Version: 1.0
Main-Class: [the canonical name of the class you want to launch, e.g. com.mycompany.myproject.MyApp]In Eclipse, right-click the build.xml file and select Run As -> Ant Build.
This will package everything up into one executable JAR file without the hassle of external classpath dependencies.
You still may want to use a batch file (or file system shortcut) to launch the JAR if you need to adjust JVM values such as min/max heap size.
cannot include external jar in my executable jar
The jar files referred to by the Class-Path entry in the manifest must not be inside the jar, but outside of it, as explained in the jar tutorial.
how to run external jar library in eclipse
The error occurs because of me adding jar library files to MODULEPATH instead of CLASSPATH. You have to add jar files to your CLASSPATH. If you already add jar files to MODLEPATH you have to remove from there and add jar files to CLASSPATH
as on : Error occurred during initialization of boot layer
Related Topics
Apache Httpclient Making Multipart Form Post
Scanner Only Reads File Name and Nothing Else
How to Stop Parsing Xml Document with Sax at Any Time
Zoom Box for Area Around Mouse Location on Screen
Populate Jfreechart Timeseriescollection from MySQL Db
Java Method Dispatch with Null Argument
Java Array with More Than 4Gb Elements
Pdfbox Encode Symbol Currency Euro
Drag and Drop Custom Object from Jlist into Jlabel
Start Mail-Client with Attachment
How to Set Color to a Certain Row If Certain Conditions Are Met Using Java
Netbeans - Error: Could Not Find or Load Main Class
How to Include External Jar on My Netbeans Project
Accessing Resource Files from External Modules