0

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.

Popgalop
  • 737
  • 2
  • 9
  • 26

1 Answers1

0

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.

Frank Ruben
  • 105
  • 7
  • Hmmm, last time I tried this was a decade ago and I still feel the pain. Write yourself a nice batch file / shell script and fiddle with it until the debugger start and then create your Java code to mimic the batch file / shell script, – Frank Ruben Jun 23 '13 at 00:02