Let us have a Java application, contained in files A.java and B.java with dependency on somejar.jar file. The questions are:
-
how to create a self-runnable JAR file with all the dependencies and sources compiled within? The main condition is using the standard Java utilities only (given with JDK; e.g.:
java,javacandjar) and NOT any of build tools like Maven, Ant or any other. -
how to use external JAR files within my application? For example, the algorithm is:
if the 'otherjar.jar' is present near the main application JAR, we should call method Moo::method1 from that class, passing the new instance of Foo class to it. Moo and Foo should be present in 'otherjar.jar' file. Still, the 'config.xml' file should be there too.
From the link that @michael667 gave, you may be more interested in this section: Adding Classes to the JAR File’s Classpath. In particular this note:
It’s not a possible thing to do with the standard java tools if you don’t code your own classloader. There are tools out there, like One-jar which can provide you of such classloader.
Of course, you could always manually use the exploding-jar approach, but that doesn’t seem to be what you really want.
You may also find the answers to this question useful: Classpath including JAR within a JAR