2014-03-28 45 views
0

我正試圖在需要遠程調試的安全系統上工作。因此,我正在搜索的是執行字符串中的代碼的方式,如下面的示例但用java。如何從Java中的字符串執行代碼

try { 

    String Code = "rundll32 powrprof.dll, SetSuspendState";// the code we need to excecute 

    Runtime.getRuntime().exec(Code); 


} catch (IOException e) { 
} 
+0

可能的重複[如何在後臺運行Java代碼的命令行?](http://stackoverflow.com/questions/11394394/how-do-i-run-command-line-from-java- code-in-the-background) – logoff

+7

我以爲在字符串中執行任何舊代碼都是安全系統的反面代碼...... – doctorlove

回答

1
String Code = "rundll32 powrprof.dll, SetSuspendState"; 

    StringBuffer output = new StringBuffer(); 

    Process p; 
    try { 
     p = Runtime.getRuntime().exec(Code); 
     p.waitFor(); 
     BufferedReader reader = 
         new BufferedReader(new InputStreamReader(p.getInputStream())); 

        String line = "";   
     while ((line = reader.readLine())!= null) { 
      output.append(line + "\n"); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    System.out.println(output.toString()); 

請參閱以下網址瞭解更多信息http://www.mkyong.com/java/how-to-execute-shell-command-from-java/

0

不,你得到了它wrong.I真的不想執行CMD codes.what我真的希望朋友就是執行java命令.as是一個如下所示傳遞的字符串。

例如:

字符串代碼= 「的System.out.println(」 測試代碼 「)」;

Runtime.getRuntime()。exec(Code);

這樣的事情。

+0

您的問題中的示例另有說明。那麼我會修改你原來的問題來說實話。 – Gimby