【问题标题】:Java ClassLoader VM ArgumentsJava ClassLoader VM 参数
【发布时间】:2015-11-14 11:05:59
【问题描述】:

以下代码尝试启动外部可执行 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

【问题讨论】:

    标签: java jvm arguments executable-jar jvm-arguments


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2013-07-23
      相关资源
      最近更新 更多