wood burning stoves
The moose likes Java in General and the fly likes include jar in an executable jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "include jar in an executable jar" Watch "include jar in an executable jar" New topic

include jar in an executable jar

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
I have a small program that does some FTP work with the help of Apache's commons-net-1.2.2.jar file and so every time I execute the Java program, I have to use the classpath flag to reference that JAR file. I would like to package up my Java program into an executable JAR file, and if possible, I'd like to include the Commons Net JAR file inside of my "main" JAR file so that there would be only one simple thing to call to run it (because I would like to hand this off to less-savvy colleagues who need to use it to automate some tasks). Is there any way to do this? If so, how do you make sure that the classes in the JAR file know how to find the classes in the other JAR file? Thanks...
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Take a look at http://java.sun.com/docs/books/tutorial/jar/basics/manifest.html#download

As far as I can tell, that doesn't work for "nested" jar files, though, sorry...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
You could unpack the contents of the JAR you want to include, and then just add them to your JAR.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

we had the debate some weeks ago:
jar-in-jar talk

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Thanks for all the replies. I'm trying to just JAR up my program and include the classpath to the other JAR file in the manifest so that all someone has to do is drop both of them into the same directory. However, I'm running into this problem:

C:\code\uploadsdownloads>java -jar UploadsDownloads.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/net/ftp/FTPConnectionClosedException

The commons-net-1.2.2.jar containing all that FTP code is in the same directory as the JAR I'm trying to run, and the manifest looks like this:

Manifest-Version: 1.0
Main-Class: UploadsDownloads
Class-Path: ./commons-net-1.2.2.jar

What am I doing wrong? I also tried it with this:

Class-Path: commons-net-1.2.2.jar

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Hey, guess what...I got it!

It was a problem in my manifest.

The Class-Path line needed to have a newline after it--the last line in the manifest has to have a newline marker at the end. :roll:

Oh well, at least it works!

I agree. Here's the link: http://aspose.com/file-tools
subject: include jar in an executable jar
It's not a secret anymore!