• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a JAR for an application that uses external libraries

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sridhar Venkataraman
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can perhaps ZIP them, or even create a self-extracting executable. WinZip for instance supports that.
 
Sridhar Venkataraman
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic