public static IDebugSession launch()

in com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/DebugUtility.java [180:256]


    public static IDebugSession launch(VirtualMachineManager vmManager,
            String mainClass,
            String programArguments,
            String vmArguments,
            String modulePaths,
            String classPaths,
            String cwd,
            String[] envVars,
            String javaExec)
            throws IOException, IllegalConnectorArgumentsException, VMStartException {
        List<LaunchingConnector> connectors = vmManager.launchingConnectors();
        LaunchingConnector connector = connectors.get(0);

        /** In the sun JDK 10, the first launching connector is com.sun.tools.jdi.RawCommandLineLauncher, which is not the one we want to use.
         *  Add the logic to filter the right one from LaunchingConnector list.
         *  This fix is only for the JDI implementation by JDK. Other JDI implementations (such as JDT) doesn't have the impact.
         */
        final String SUN_LAUNCHING_CONNECTOR = "com.sun.tools.jdi.SunCommandLineLauncher";
        for (LaunchingConnector con : connectors) {
            if (con.getClass().getName().equals(SUN_LAUNCHING_CONNECTOR)) {
                connector = con;
                break;
            }
        }

        Map<String, Argument> arguments = connector.defaultArguments();
        arguments.get(SUSPEND).setValue("true");

        String options = "";
        if (StringUtils.isNotBlank(vmArguments)) {
            options = vmArguments;
        }
        if (StringUtils.isNotBlank(modulePaths)) {
            options += " --module-path \"" + modulePaths + "\"";
        }
        if (StringUtils.isNotBlank(classPaths)) {
            options += " -cp \"" + classPaths + "\"";
        }
        arguments.get(OPTIONS).setValue(options);

        // For java 9 project, should specify "-m $MainClass".
        String[] mainClasses = mainClass.split("/");
        if (mainClasses.length == 2) {
            mainClass = "-m " + mainClass;
        }
        if (StringUtils.isNotBlank(programArguments)) {
            mainClass += " " + programArguments;
        }
        arguments.get(MAIN).setValue(mainClass);

        if (arguments.get(CWD) != null) {
            arguments.get(CWD).setValue(cwd);
        }

        if (arguments.get(ENV) != null) {
            arguments.get(ENV).setValue(encodeArrayArgument(envVars));
        }

        if (isValidJavaExec(javaExec)) {
            String vmExec = new File(javaExec).getName();
            String javaHome = new File(javaExec).getParentFile().getParentFile().getAbsolutePath();
            arguments.get(HOME).setValue(javaHome);
            arguments.get(EXEC).setValue(vmExec);
        } else if (StringUtils.isNotEmpty(DebugSettings.getCurrent().javaHome)) {
            arguments.get(HOME).setValue(DebugSettings.getCurrent().javaHome);
        }

        VirtualMachine vm = connector.launch(arguments);
        // workaround for JDT bug.
        // vm.version() calls org.eclipse.jdi.internal.MirrorImpl#requestVM
        // It calls vm.getIDSizes() to read related sizes including ReferenceTypeIdSize,
        // which is required to construct requests with null ReferenceType (such as ExceptionRequest)
        // Without this line, it throws ObjectCollectedException in ExceptionRequest.enable().
        // See https://github.com/Microsoft/java-debug/issues/23
        vm.version();
        return new DebugSession(vm);
    }