简体   繁体   中英

Restarting program in debug mode

I am trying to restart a java program in debug mode using this code

public static final String SUN_JAVA_COMMAND = "sun.java.command";

public static void restartApplication() throws IOException 
{
    try 
    {
        String java = System.getProperty("java.home") + "/bin/java";
        final StringBuffer cmd = new StringBuffer("\"" + java + "\" ");
        String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(" ");

        if (mainCommand[0].endsWith(".jar")) 
        {
            cmd.append("-jar " + new File(mainCommand[0]).getPath());
        } 
        else 
        {
            cmd.append("-cp \"" + System.getProperty("java.class.path") + "\" " + mainCommand[0]);
        }

        cmd.append("");

        for (int i = 1; i < mainCommand.length; i++) 
        {
            cmd.append(" ");
            cmd.append(mainCommand[i]);
        }

        cmd.append(" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000");
        Runtime.getRuntime().exec(cmd.toString());
        System.exit(0);
    } 
    catch (Exception e) 
    {
        throw new IOException("Error while trying to restart the application", e);
    }
}

However each time the application starts from this main method

public static void main(String[] args) throws Exception
{
    boolean isDebug = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
    JOptionPane.showMessageDialog(null, isDebug);
    Utility.restartApplication();
}

The joptionpane always displays the message false why wont this work

Is the problem in the isDebug or with the restart application; I think the problem is the restart.

Your isDebug right-hand-side seems to be sufficiently tricky to not be sure whether it should work or not. Why don't you just set it to true and test whether the debugger starts. Afterwards you known whether it's the isDebug evaluation or the debugger restart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM