4

I have created an executable JAR with the manifest:

Manifest-Version: 1.0
Main-Class: MyClass
Class-Path: lib/ext.jar

I can successfully run this JAR if I have folder lib with dependent ext.jar in the folder of .jar created.

I would like to embed ext.jar in my executable JAR to launch it anywhere I want, without creating lib folder. This would allow the JAR file to be self-contained, including all of its dependencies.

Is there any way to do this?

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
TheFrancisOne
  • 2,667
  • 9
  • 38
  • 58
  • I would suggest building with Maven. See link: http://stackoverflow.com/questions/1729054/include-dependencies-into-jar – hgus1294 Nov 29 '11 at 15:22
  • Does the app. have a GUI? If so, a better deployment option might be [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). JWS makes it simple to add extra Jars to the run-time class-path of an app. Even better, JWS can provide a neat desktop shortcut or menu item (with custom icon) to launch the app. – Andrew Thompson Nov 30 '11 at 00:44
  • @AndrewThompson could you use JWS when there is no GUI? And I guess you can also create a .jnlp and distribute it with your jars (no "Web" then)? – Matthieu Dec 13 '13 at 12:00
  • *"use JWS when there is no GUI?"* No. It is for apps. with a GUI. As (I *thought* was) clearly explained in the link. *"I guess you can also create a .jnlp and distribute it with your jars (no "Web" then)?"* No, it is intended for launch from the internet or a network of some sort. They don't call it 'Java **Web** Start' and name the 'Java **Network** Launch Files' that extension for nothing. ;) – Andrew Thompson Dec 13 '13 at 12:45

2 Answers2

3

The default Java class loader cannot find an embedded jar inside a jar file. You will need to set your Java program to use a class loader which can deal with embedded jar files. As fas as I know, I have not seen one in any opensource libraries so you may need to write one yourself.

Alternatively, you can extract all the jar files and combine the the content into a single jar file.

Clean way to combine multiple jars? Preferably using Ant

Community
  • 1
  • 1
gigadot
  • 8,879
  • 7
  • 35
  • 51
2

Java doesn't have out-of-the-box support for embedding a jar within a jar. It can be achieved by writing a custom class loader, or using a tool that's already been written for this purpose: Fat Jar.

Eli Acherkan
  • 6,401
  • 2
  • 27
  • 34