aspose file tools*
The moose likes Java in General and the fly likes Classpath and Jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Classpath and Jar file" Watch "Classpath and Jar file" New topic
Author

Classpath and Jar file

badari gururaj
Ranch Hand

Joined: Feb 25, 2002
Posts: 63
Hi,
I have a Jar file with some class files - file1.jar . These class files need 2 more jar files(file_a.jar and file_b.jar)in the classpath to run correctly.
I want to bundle the dependent jar files inside the main jar file.

Is there any way to make the dependent jar files a part of the classpath without un jarring them.

|-- class files
file1.jar----------|
|-- file_a.jar, file_2.jar

i am looking at someting line

java -cp file_a.jar;file_b.jar -jar file1.jar

The only other option i am seeing is to un jarr this file and put file_a.jar and file_b.jar in the system classpath

Regards
Badari


SCJP, SCJD, SCBCD, SCEA, IBM 665
John Kelty
Ranch Hand

Joined: Aug 08, 2006
Posts: 33
Why couldn�t you just add file_a.jar and file_b.jar to the classpath like any other JARs?

Your command

java -cp file_a.jar;file_b.jar -jar file1.jar

should work just fine. It puts the two JARs on the class path. You shouldn�t have to unjar them. They could go on the system class path, or on the class path of the Java command you use to launch file1.jar.
badari gururaj
Ranch Hand

Joined: Feb 25, 2002
Posts: 63
But file_a and file_b are inside the main jar file...how do i make a reference to them through the classpath?
John Kelty
Ranch Hand

Joined: Aug 08, 2006
Posts: 33
You could write a custom class loader that will know to look inside the outer jar.

Otherwise, you are stuck with needing to extract the inner JARs out of the main one.

http://forums.java.net/jive/thread.jspa?threadID=342&messageID=9588

has some information on using ant to automate that task.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
In the manifest of the first jar, define a property like this:

Class-Path: file1.jar file2.jar file3.jar

This way all this jars will be automatically included in the first jar as dependencies.

See the Java Deployment Tutorial for more information on this topic and other manifest properties.
[ October 18, 2006: Message edited by: Edwin Dalorzo ]
Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
This way all this jars will be automatically included in the first jar as dependencies.

Jars listed in the MANIFEST.MF file are relative to the location of the jar containing the MAINFEST.MF file.

From the link you provided:

Note : The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath and Jar file
 
Similar Threads
same name java classes in different JAR
Trying to execute java program which is in jar file
File Exists
Can't get a jar to run with dependencies
why is log4j not working in executable jar?