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?
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:
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.
look @ java-jar-ignores-classpath-Workaround
Try the fat-jar extension. It will include all external jars inside the jar.
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:
How to include the jars of your project into your runnable jar:
I'm using Eclipse Version: 3.7.2 running on Ubuntu 12.10. I'll also show you how to make the
build.xml
so you can do theant jar
from command line and create your jar with other imported jars extracted into it.Basically you ask Eclipse to construct the build.xml that imports your libraries into your jar for you.
Fire up Eclipse and make a new Java project, make a new package 'mypackage', add your main class:
Runner
Put this code in there.Now include the
mysql-connector-java-5.1.28-bin.jar
from Oracle which enables us to write Java to connect to the MySQL database. Do this by right clicking the project -> properties -> java build path -> Add External Jar -> pick mysql-connector-java-5.1.28-bin.jar.Run the program within eclipse, it should run, and tell you that the username/password is invalid which means Eclipse is properly configured with the jar.
In Eclipse go to
File
->Export
->Java
->Runnable Jar File
. You will see this dialog:Make sure to set up the 'save as ant script' checkbox. That is what makes it so you can use the commandline to do an
ant jar
later.Then go to the terminal and look at the ant script:
So you see, I ran the jar and it didn't error out because it found the included
mysql-connector-java-5.1.28-bin.jar
embedded insideHello.jar
.Look inside Hello.jar:
vi Hello.jar
and you will see many references tocom/mysql/jdbc/stuff.class
To do
ant jar
on the commandline to do all this automatically: Renamebuildant.xml
tobuild.xml
, and change the target name fromcreate_run_jar
tojar
.Then, from within
MyProject
you typeant jar
and boom. You've got your jar inside MyProject. And you can invoke it usingjava -jar Hello.jar
and it all works.