This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

include jar in an executable jar

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could unpack the contents of the JAR you want to include, and then just add them to your JAR.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we had the debate some weeks ago:
jar-in-jar talk
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic