Skip to content

Instantly share code, notes, and snippets.

@ryankennedy
Created May 18, 2012 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryankennedy/2723877 to your computer and use it in GitHub Desktop.
Save ryankennedy/2723877 to your computer and use it in GitHub Desktop.
JVM Multi Runner
package com.yammer.runner;
import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
public class MultiRunner {
public static void main(String[] args) throws Exception {
for (String arg : args) {
String[] subArgs = arg.split(" ");
File jarFile = new File(subArgs[0]);
final String[] jarArgs = Arrays.copyOfRange(subArgs, 1, subArgs.length);
JarFile jar = new JarFile(jarFile);
final String mainClassName = jar.getManifest().getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
ClassLoader loader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()});
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Class mainClass = Thread.currentThread().getContextClassLoader().loadClass(mainClassName);
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.invoke(null, new Object[]{jarArgs});
} catch (Exception e) {
e.printStackTrace();
}
}
}, mainClassName);
thread.setContextClassLoader(loader);
thread.setDaemon(false);
thread.start();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment