https://stackoverflow.com/a/5575619/20394 should explain how to add to the library path and still use the same JVM.
To add to the classpath, create your own URLClassLoader
and use that to find the class you want to load, then call its start method reflectively.
URLClassLoader classLoader = new URLClassLoader(
urlsToDirectoriesAndJars, getClass().getClassLoader());
Class<?> myMainClass = classLoader.findClass("pkg.path.for.MainClass");
Method main = myMainClass.getMethod("main", String[].class);
main.invoke(null, new Object[] { new String[] { "args", "for", "main" } });
You'll need to handle some exceptions that are thrown by the reflective API but that's the gist of it.
If that class calls System.exit
, then see How to prevent calls to System.exit() from terminating the JVM?