2013-06-22 64 views
0

我想每一個應用程序從這個主要方法在調試模式下重新啓動程序

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(); 
} 

的JOptionPane的啓動時使用此代碼

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); 
    } 
} 

但是重新啓動在調試模式下的Java程序總是顯示該消息錯誤爲什麼不會這個工作

isDebug中的問題或與重新啓動應用程序;我認爲問題是重啓。

回答

0

你的isDebug右手邊似乎是非常棘手的,不能確定它是否應該起作用。你爲什麼不把它設置爲true並測試調試器是否啓動。之後,您會知道是否是isDebug評估或調試器重新啓動。

+0

重新啓動似乎沒有工作 – Popgalop

+0

嗯,上次我嘗試這是十年前,我仍然感到痛苦。編寫一個漂亮的批處理文件/ shell腳本,並調試它,直到調試器啓動,然後創建Java代碼以模仿批處理文件/ shell腳本, –

相關問題