Make a class, say Parallel
having as command line arguments the other class names.
Then start for each class a new thread and call its main.
Probably can be done neater.
java Parallel A B
public class Parallel {
public static void main(String[] args) {
for (String arg : args) {
try {
Class<?> klazz = Class.forName(arg);
final Method mainMethod = klazz.getDeclaredMethod("main",
String[].class);
if (mainMethod.getReturnType() != void.class) {
throw new NoSuchMethodException("main not returning void");
}
int modifiers = mainMethod.getModifiers();
if (!Modifier.isStatic(modifiers)
|| !Modifier.isPublic(modifiers)) {
throw new NoSuchMethodException("main not public static");
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
mainMethod.invoke(null, (Object)new String[0]);
} catch (IllegalAccessException
| IllegalArgumentException
| InvocationTargetException ex) {
Logger.getLogger(Parallel.class.getName())
.log(Level.SEVERE, null, ex);
}
}
});
thread.start();
} catch (ClassNotFoundException
| NoSuchMethodException
| SecurityException ex) {
Logger.getLogger(Parallel.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}
}