Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a jar Manifest to use all jars in a folder

I'm trying to set a jar manifest so it loads all the libraries (jars) within a folder next to the jar.

The manifest looks like this:

Manifest-Version: 1.0
Class-Path: libs/
Main-Class: path.to.my.class.Main

The layout is as follows:

- MyJar.jar
- libs/
-----lib1.jar
-----lib2.jar

And I'm trying to run like this:

java -jar MyJar.jar

And I get NoClassDefinition errors about classes in the jar within the folder.

In case someone is curious, this folder might contain optional jars that are processed during class loading. That' swhy I can't use the hardcoded or autogenerated ones.

Any idea?

Update Rephrased the question as this is not currently possible from the manifest itself. The answer was the only really viable without the need of extracting the jars, although it also works.

So as a summary the answer is no, this can't be done from manifest file alone if you have unespecified dependencies.

like image 416
javydreamercsw Avatar asked Oct 03 '16 20:10

javydreamercsw


2 Answers

You should define your Manifest classpath as

Class-Path: libs/lib1.jar libs/lib2.jar

See Oracle documentation for more details https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

like image 181
Issam El-atif Avatar answered Oct 15 '22 11:10

Issam El-atif


Try extracting these jars. It looks like you cannot add all jars from directory but you can add all classes. You lose obviously all configuration in manifest, however, if you are interested in jars' code content only, it might work.

I tested that with these simple classes

import pkg.B;

public class A {
  public static void main(String[] args) {
    System.out.println(B.class.getName());
  }
}

package pkg;

public class B {}

now I try to separate the classes. I have jarred them into

$ jar tf libA.jar 
META-INF/
META-INF/MANIFEST.MF
A.class
$ jar tf libB.jar 
META-INF/
META-INF/MANIFEST.MF
pkg/B.class

no Class-Path in any manifest. I can run A with java -cp libB.jar:libA.jar A. Now I create another jar with Class-Path set to lib/

$ cat manifest 
Class-Path: lib/
$ jar cfm empty.jar manifest

my directory tree look like

$ ls -R
.:
A.java  empty.jar  lib  lib.jar  manifest  pkg

./lib:
libA.jar  libB.jar

./pkg:
B.java

Now I try jar

$ java -jar empty.jar 
Error: Could not find or load main class A

Hopeless, right? Then I extracted libA.jar and libB.jar into lib (same as [this guy][2]). Now all is fine

$ java -jar empty.jar 
pkg.B
like image 31
lord.didger Avatar answered Oct 15 '22 09:10

lord.didger