简体   繁体   中英

How to launch a resource jar from inside another java app - with Solution

EDITED with solution (below...)

I have a Splash screen that is packaged into it's own jar. It works.

I can call the Splash.jar from inside another java app by:

Desktop.getDesktop().open(new File("/Applications/Eclipse/Splash.jar"));

and it works just fine. But, that's pretty limited. So, I created a res folder in the new app and dragged the Splash.jar into it.

Now, how do I call it/run it from the main class of the new app??

I looked at all the related posts and see no clear approach...

Thanks


SOLUTION:

I found the solution - so simple. First, the credit to avjava.com for their clear and excellent tutorial on doing this ( Run Another Jar from a Jar ). So, now I can run the Splash (or other .jar) just as hoped for.

The answer is here - posted in the form of complete code:

package test;

import java.io.IOException;

public class RuntimeExecTest1 {

  public static void main(String[] args) {
    try {
      System.out.println("TextEdit I hope");
      Runtime runTime = Runtime.getRuntime();

      Process process = runTime.exec(
          "java -jar /your directory/your app.jar");

      try {
        Thread.sleep(5000); // keep in open 5000ms
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println("Closing TextEdit, I hope");
      process.destroy();        // kill the process of running the .jar
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

We don't know how your existing Splash Screen works...

Java AWT support for Splash Screen:

If you are using the Java built-in support for splash screens (as specified in SplashScreen Javadoc ) then you need to use a command line switch, or better yet, modify your MANIFEST file in order to reference your Splash Screen:

Manifest-Version: 1.0
Main-Class: Test
SplashScreen-Image: filename.gif

I don't know if, for this particular case, you can reference files in a different JAR. In the worst case, you can unpack the existing JAR (they are just ZIP files) and get the image file in order to include it in your own main jar.

Possibly custom Splash:

If your Splash is created using custom code, then you need the documentation about how to load it. At least, you'd need to add Splash.jar to the classpath of your application and, from your app, call the necessary method or load the appropriate resource.

All the resources and classes contained in .jar files that are added to the classpath are available from your app.

You could create a new URLClassLoader with the Splash.jar and then use reflections to execute the main method.

URL[] urls = new URL[] { new URL("res/Splash.jar") };
ClassLoader cl = new URLClassLoader(urls);
Class<?> clazz = cl.loadClass("splash.Main");
Method method = clazz.getMethod("main", String[].class);
method.invoke(null, new String[0]);

add the resource path to your CLASSPATH envoirment variable and you can use it without modifying your existing code

if your running linux

export CLASSPATH=yourpath;

and if your running windows:

from My Computer right click > properties

OR

if you dont want to add it to CLASSPATH ENV variable,

then

java -classpath="your path to jar" yourclass

Why not define Splash.jar as an external jar and go about using all its routines. Much like JDBC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM