views:

11195

answers:

6

Hi all,

I am trying to build an executable jar program which depends on external jar downloaded. In my project, I included them in the build path and can be run and debug within eclipse.

When I tried to export it to a jar, I can run the program but I can't when I try to press a button which includes function calls and classes from the external jar. I have edited the environment variables (Windows XP) CLASSPATH to include paths of all the external jar, but it doesn't work.

A point to note is that I got compile warnings while exporting my executable jar, but it doesn't show up any description about the warnings.

Would someone kindly provide a thorough guide on how to include an external jar program using eclipse?

Best regards, KWAN Chiu Yin, Ben

+1  A: 

Don't do that.

Seriously, never depend on your IDE to create build artifact, use scripts (maven, ant) for that purpose.

Dev er dev
A: 

try the fat-jar extension. It will include all external jars inside the jar. Update url: http://kurucz-grafika.de/fatjar Homepage: http://fjep.sourceforge.net/

Marius
+2  A: 

You can do this by writing a manifest for your jar. Have a look at the Class-Path header. Eclipse has an option for choosing your own manifest on export.

The alternative is to add the dependency to the classpath at the time you invoke the application:

win32: java.exe -cp app.jar;dependency.jar foo.MyMainClass
*nix:  java -cp app.jar:dependency.jar foo.MyMainClass

or

java -cp dependency.jar -jar app.jar
McDowell
A: 

Option 1:

"You can do this by writing a manifest for your jar. Have a look at the Class-Path header. Eclipse has an option for choosing your own manifest on export." by McDowell

Option 2:

As a good practice you can use an Ant Script (Eclipse comes with it) to generate your JAR file. Inside this JAR you can have all dependent libs.

You can even set the MANIFEST's Class-path header to point to files in your filesystem, it's not a good practice though.

Ant build.xml script example:

<project name="jar with libs" default="compile and build" basedir=".">
<!-- this is used at compile time -->
<path id="example-classpath">
 <pathelement location="${root-dir}" />
 <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
</path>

<target name="compile and build">
 <!-- deletes previously created jar -->
 <delete file="test.jar" />

 <!-- compile your code and drop .class into "bin" directory -->
 <javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
  <!-- this is telling the compiler where are the dependencies -->
  <classpath refid="example-classpath" />
 </javac>

 <!-- copy the JARs that you need to "bin" directory  -->
 <copy todir="bin">
  <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
 </copy>

 <!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
 <jar destfile="test.jar" basedir="bin" duplicate="preserve">
  <manifest>
   <!-- Who is building this jar? -->
   <attribute name="Built-By" value="${user.name}" />
   <!-- Information about the program itself -->
   <attribute name="Implementation-Vendor" value="ACME inc." />
   <attribute name="Implementation-Title" value="GreatProduct" />
   <attribute name="Implementation-Version" value="1.0.0beta2" />
   <!-- this tells which class should run when executing your jar -->
   <attribute name="Main-class" value="ApplyXPath" />
  </manifest>
 </jar>
</target>

Lucas -luky- N.
A: 

look @ java-jar-ignores-classpath-Workaround

Moro
+2  A: 

Eclipse 3.5 has an option to package required libraries into the runnable jar. File -> Export... Choose runnable jar and click next. The runnable jar export window has a radio button where you can choose to package the required libraries into the jar.

Bob Tefft