我想在运行时执行期间调用 java keytool 以提供动态参数。 以下是在 Windows 下工作的内容,但在 Linux (Ubuntu) 相同的 Java 1.6.0 下不工作:
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[])
提前致谢!
最佳答案
我建议改用 http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[])
那么你不必担心转义你在这里没有做的每个参数
String args [] = {arg1, arg2, "-dname", "dNameArguments"};
关于java - Linux 下 Runtime.getRuntime().exec() 的 Keytool 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308148/