2012-03-29 165 views
0

我想用java swing創建一個GUI。從那裏我必須運行linux系統命令。我嘗試使用exec()。但是,如果exec()函數包含單引號,則無法解析該字符串。我所使用的代碼如下 -從java程序內執行linux命令

Process p = Runtime.getRuntime().exec("cpabe-enc pub_key message.txt '(it_department or (marketing and manager))'") 
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); 

但我得到的錯誤,當我運行程序 - 在"'("語法錯誤。

同樣的命令運行時,我寫

Process p = Runtime.getRuntime().exec("cpabe-enc pub_key message.txt default") 

請幫助。在此先感謝您的幫助。

回答

1

向上分割的參數到一個數組代替,一個串對於每個參數,並使用exec-方法,該方法作爲代替String[],其通常適用於參數更好。 Somethign沿着線:

Runtime.getRuntime().exec(new String[] {"cpabe-enc", "pub_key", "message.txt", "(it_department or (marketing and manager))"}); 

或任何你確切的參數是什麼。

+0

非常感謝。有效。我的程序運行完美。 – 2012-03-29 08:30:00