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[])
感謝時一樣!
我試過使用String [] - 符號,但沒有得到它的工作。我該如何安排參數(特別是-dname「....」參數)? –
編輯我的問題以顯示用法 – jontro
感謝@Bengt,它工作的很好!我總是嘗試使用String args [] = {arg1,arg2,「-dname dNameArguments」}; –