Java 类加载器 VM 参数

Java ClassLoader VM Arguments

下面的代码尝试启动外部可执行 JAR 文件。

final File file = new File("/path/to/executable.jar");
JarFile jarFile = null;
jarFile = new JarFile(file);
final Manifest manifest = jarFile.getManifest();
final URLClassLoader child = new URLClassLoader(new URL[] { file.toURI().toURL() }, Launcher.class.getClassLoader());
final Class<?> classToLoad = Class.forName("com.example.launcher.Launcher", true, child);
final Method method = classToLoad.getDeclaredMethod("main", String[].class);
final Object[] arguments = { new String[0] };
        method.invoke(null, arguments);
jarfile.close();

main 方法接收的参数可以在 Object[] arguments 中设置,但是如何设置 VM 参数,例如 -XstartOnFirstThread

要设置 JVM 参数,您必须启动 JVM。您当前的代码将 运行 JVM 中已经 运行ning 的主要方法。

要启动 JVM,可能最简单的方法是 运行 使用 ProcessBuilderjava 命令。