Search code examples
javaentry-point

Set java entrypoint to a class that's in a JAR inside another JAR


I have a JAR that's packaged with One Jar, so it contains multiple dependencies inside itself(using the JAR URL notation):

<one.jar>!/lib/<deps1.jar>
<one.jar>!/lib/<deps2.jar>
...

Is it possible to run main() from a class com.example.A that lives in deps2.jar?

I tried java -cp one.jar!/lib/deps2.jar com.example.A, but that doesn't work.


Solution

  • You can include a main() somewhere in your primary .jar that delegates to com.example.A.main(). This will give you the behavior you seek with just a few additional lines of code...a small additional class definition file. For example:

    package foo.bar;
    
    import com.example.A;
    
    class MainEntryDelegate {
        public static void main(String... args) {
            com.example.A.main(args);
        }
    }
    

    So your app will then be runnable via the obvious:

    java -cp one.jar foo.bar.MainEntryDelegate
    

    or if you've set up your manifest correctly:

    java -jar one.jar