How to setup a JDI launching connector?

£可爱£侵袭症+ 提交于 2019-11-30 22:45:30

It's probably because you aren't handling the subprocess stdin/stdout streams. I've got a VMLauncher utility class in my JDI scripting project that handles this; the relevant code is:

public VirtualMachine safeStart()
    throws IOException,
           IllegalConnectorArgumentsException,
           VMStartException
{
    VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
    LaunchingConnector connector = vmm.defaultConnector();
    Map<String, Argument> cArgs = connector.defaultArguments();
    cArgs.get("options").setValue(options);
    cArgs.get("main").setValue(main);
    final VirtualMachine vm = connector.launch(cArgs);

    final Thread outThread = redirect("Subproc stdout",
                                      vm.process().getInputStream(),
                                      out);
    final Thread errThread = redirect("Subproc stderr",
                                      vm.process().getErrorStream(),
                                      err);
    if(killOnShutdown) {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                outThread.interrupt();
                errThread.interrupt();
                vm.process().destroy();
            }
        });
    }

    return vm;
}

private Thread redirect(String name, InputStream in, OutputStream out) {
    Thread t = new StreamRedirectThread(name, in, out);
    t.setDaemon(true);
    t.start();
    return t;
}

Did you set your classpath in the LaunchingConnector?

env.get("options").setValue("-cp " +
  "/my-project/target/classes:" +
  "/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/lib/tools.jar:" +
  "");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!