This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Creating a JAR for an application that uses external libraries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a JAR for an application that uses external libraries" Watch "Creating a JAR for an application that uses external libraries" New topic
Author

Creating a JAR for an application that uses external libraries

Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Hi,

I am writing a desktop Java application that uses some external libraries (packaged into JARs).

I am able to compile my classes by adding these JARs to the lib directory of my JDK.

My question is, if I pack my application into an executable JAR after compilation and then run the JAR from another workstation, will it work if that workstation doesn't have the external libraries I have used?

If not, then I will need to bring these JARs into the directory of my application. Let's say I want to put all these JARs in a folder called lib within the root directory of the application. How do I modify the CLASSPATH so the compiler recognizes these JAR libraries?

I am using the good ol' command prompt for this.

Thank you.

[ June 02, 2008: Message edited by: Sridhar Venkataraman ]
[ June 02, 2008: Message edited by: Sridhar Venkataraman ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Originally posted by Sridhar Venkataraman:
My question is, if I pack my application into an executable JAR after compilation and then run the JAR from another workstation, will it work if that workstation doesn't have the external libraries I have used?

No.
Originally posted by Sridhar Venkataraman:
If not, then I will need to bring these JARs into the directory of my application. Let's say I want to put all these JARs in a folder called lib within the root directory of the application. How do I modify the CLASSPATH so the compiler recognizes these JAR libraries?
Go to the Java Tutorial. Where it says "updating a JAR" it tells you how to add files to the JAR; I have never tried it, but you may be able to add the external library to your JAR. If you are successful, make sure to tell us whether it worked.
You should also find a link on the same page which tells you how to add the external library to the classpath in the manifest file.

If that doesn't work: Campbell's cheat: unzip the external JARs into the same directory structure as your application, adding an acknowledgement wherever appropriate to your documentation. If you get the folders in the right places, it will work without the -cp classpath setting flag. Then try wrapping it all into a JAR again.
[ June 02, 2008: Message edited by: Campbell Ritchie ]
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Thank you for your reply.

I will try it and let you know ASAP. Meanwhile, I tried running the compiled program after adding a JAR that I require (it is the Apache POI jar) to the lib/ext folder of the jre within the jdk folder.

The compilation happened, but I am unable to run the program.

The program is XMLToExcel.java and it is present inside the package com/apress/excel, which is inside the 'src' directory

I tried running the program by issuing the command -
java com/apress/excel/XMLToExcel

from the src directory after compiling it, but I get the familiar NoClassDef error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/hssf/u
sermodel/HSSFWorkbook

This class is indeed present in exactly the same package inside the JAR that I added.

How do I fix this? Thanks.
[ June 02, 2008: Message edited by: Sridhar Venkataraman ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Create a folder relative to your own JAR file, usually it's called lib. Place all the external library JAR files in there. Then add a line to your manifest file:

Then recreate your JAR file, but do NOT include the lib folder. Then ship the entire lib folder with your JAR file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Thank you. Does this mean that I have to ship a directory with the application's jar as well as the libraries to the end-user?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Last time I tried this, yes, I ended up putting the lib folder and the jar file on the CD to run the application from.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

You can perhaps ZIP them, or even create a self-extracting executable. WinZip for instance supports that.
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Fine. I tried out something similar along the lines of your replies and it worked.

Is there a slicker or more preferred method of deploying such applications?

Thank you once again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Originally posted by Sridhar Venkataraman:
Fine. I tried out something similar along the lines of your replies and it worked.

Is there a slicker or more preferred method of deploying such applications?

Thank you once again.
Only too pleased to be able to help.

What did you get to work? I would find it useful next time I want to export a jar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a JAR for an application that uses external libraries
 
Similar Threads
Including library jars in an application jar
Error with running batch file from Java class
getting weblogic to add jar to classpath (axis.jar)
HTML hyperlink problem
Applet security issues