Linux 下 Keytool 与 Runtime.getRuntime().exec() 的使用
我想在运行时执行期间调用 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错误:java.io.IOException:无效关键字“”CN”
在 Linux 命令行中运行命令(不是在 java 中启动),代码有效。我做错了什么以及 String[]< /code> 使用时的样子
Runtime.getRuntime().exec(String[])
提前致谢!
I'd like to call the java keytool during runtime execution providing dynamic arguments.
Here's what is working under Windows, but not under Linux (Ubuntu) same 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
}
The Output under Linux is
keytool error: java.io.IOException: Invalid keyword ""CN"
Running the command in the Linux command line (not starting in java), the code works. What am I doing wrong and how would the String[]
look like when using
Runtime.getRuntime().exec(String[])
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[]) 代替
然后你不必担心转义每个参数你不在这里做的
I recommend using http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String[]) instead
Then you dont have to worry about escaping each argument which you are not doing here