2011-11-29 43 views
0

我想在運行時執行過程中調用java keytool來提供動態參數。 這裏是工作在Windows下,但不是在Linux(Ubuntu的)相同的Java 1.6.0:Runto.getRuntime()的Keytool用法。Linux下的exec()

File f = new File("mykey.jks"); 
StringBuilder command = new StringBuilder(); 
command.append(System.getProperty("java.home") 
      + System.getProperty("file.separator") + "bin" 
      + System.getProperty("file.separator") + "keytool"); 
command.append(" -genkey"); 
command.append(" -dname \"cn=foo,ou=bar,o=company,c=CH\""); 
command.append(" -alias myProduct"); 
command.append(" -keypass " + "testtest"); 
command.append(" -keystore " + f.getAbsolutePath()); 
command.append(" -storepass " + "testtest"); 
command.append(" -validity " + 3650); 
final Process pr = Runtime.getRuntime().exec(command.toString()); 

BufferedReader input = new BufferedReader(new InputStreamReader(
    pr.getInputStream())); 

String line = null; 
while ((line = input.readLine()) != null) { 
    System.out.println(line); 
} 

int exitVal = -1; 
try { 
    exitVal = pr.waitFor(); 
    System.out.println("Exited with error code " + exitVal); 
} catch (InterruptedException e) { 
    // handle 
} 

Linux下的輸出

keytool error: java.io.IOException: Invalid keyword ""CN"

在Linux命令行運行命令(不從java開始),代碼工作。我在做什麼錯了,會怎麼看String[]提前使用

Runtime.getRuntime().exec(String[]) 

感謝時一樣!

回答

3

我建議使用http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String中[]),而不是

然後你不必擔心逃避你在這裏沒有做的每個參數

String args [] = {arg1, arg2, "-dname", "dNameArguments"}; 
+0

我試過使用String [] - 符號,但沒有得到它的工作。我該如何安排參數(特別是-dname「....」參數)? –

+0

編輯我的問題以顯示用法 – jontro

+0

感謝@Bengt,它工作的很好!我總是嘗試使用String args [] = {arg1,arg2,「-dname dNameArguments」}; –