0

There are some ways to load external jar libaries such as using loader.path for spring boot application and -classpath for many other java applications including tomcat.

The question is whether it is possible to load an external libaries with some dependencies in it together for a java application (spring boot, tomcat and so on). The external libary may have the following structure:

xxx.jar  
│
└───<compiled class file folder>
│   
└───libs
    │   d1.jar
    │   d2.jar
    |   ...
Tonny Tc
  • 852
  • 1
  • 12
  • 37

1 Answers1

0

It looks like you are trying get external dependencies from a "fat jar". A fat jar is a single, executable jar file that contains all the necessary dependencies and resources needed to run the application. In other words it doesn't indented to be loaded as a library. It is ready-to-run application (you even don't need to add other classes using -classpath).

Technically, of course, you can extract all jars from xxx.jar and then load them using the same -classpath and loader.path options, but I believe it's a wrong way at all (and if you still want to do so, you can read how to unarchive jar here).


But I highly recommend you to make xxx.jar as a simple dependency (without /lib folder) and then download d1 and d2 dependencies directly from some central repository or add them as a system library using system path. Example for Maven:

<dependency>
    <groupId>com.tonny.xxx</groupId>
    <artifactId>d1</artifactId>
    <version>4.1</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/d1.jar</systemPath>
</dependency>

You can read how to add dependencies from filesystem here.

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
  • That is to say if the jar library depends some other libs, all the dependencies together with the jar lib itself should be separately copied to the lib folder of application servers and be loaded. It should not package all of them into one jar lib (fat jar). Is it correct? – Tonny Tc Feb 07 '23 at 02:48
  • @TonnyTc the usefulness of having a single fat jar depends on how you plan to use it. For microservices, it's convenient to have a self-contained jar that includes all dependencies. This makes deployment and distribution easier, as you can simply run the command `java -jar app.jar` without having to specify any additional files. – Volodya Lombrozo Feb 07 '23 at 07:30
  • However, if you intend to use the jar as a library, it's best to have a "slim" jar without dependencies. I suspect that this is your case, with d1 and d2 being separate libraries that need to be distributed independently. – Volodya Lombrozo Feb 07 '23 at 07:33