Created
May 18, 2012 08:06
-
-
Save ryankennedy/2723877 to your computer and use it in GitHub Desktop.
JVM Multi Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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