File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes executing jars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "executing jars" Watch "executing jars" New topic

executing jars

Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
Is there a straight forward way to execute a jar that references other jar libraries? I'm looking for something that works in the general case so I can repeat this when necessary.

I tried to do it from the console line and with ant, unsuccessfully. Here's what I tried:

in ant:

And this didn't help when creating the jar either

Any suggestions?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I can't tell what that <attribute> element that tries to set the Class-Path of your manifest does, but it looks suspiciously as if it tries to put a library name there. If you want the manifest to refer to other jar files, its Class-Path entry has to be a list of those jar files. Just the name of the directory where those jar files are located isn't good enough. And beyond that you need the relative path from the executable jar to the referred-to jars.

And if you do "java -cp something -jar wotsit.jar", the -cp classpath parameter is ignored. The manifest has to be set up correctly.
Ra Carter
Ranch Hand

Joined: Jul 30, 2005
Posts: 96
With that then I guess my next question is an ant question: is there any way to have ant add the individual jars to the Class-Path in the manifest?

Am I overlooking an easier way to do this? It seems like a task that ant should be able to do...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Hi racarter, please change your name, it is not according to the JavaRanch policy.

When you have code in a JAR, you don't have to run it using the "-jar" option. You can also just put your JAR in the classpath and specify the class name of the main class on the command line. If you do it like that, Java doesn't tie you to the Class-Path attribute in the manifest file in your JAR file:

java -cp lib1.jar;otherlib.jar;myjar.jar com.racarter.MyMainClass

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: executing jars
jQuery in Action, 3rd edition