java - Linux 下 Runtime.getRuntime().exec() 的 Keytool 用法

标签 java command-line keytool runtime.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 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/

相关文章:

java - “circle” 无法解析为变量

java - 使用 keystore 文件为 SOAP WS 运行客户端

java - 我在 Android 环境下工作时找不到 keytool?

java - 使用 jdk 中提供的 keytool 生成 SSL 证书

java - 当我按下按钮时,如何在另一个 Activity 中创建一个新的 TextView?

java - 优雅地处理 EJB/JPA 环境中的约束违规?

java - 扩展抽象类后的方法

C# - 没有窗口的程序

objective-c - 在 Objective-C Foundation 命令行实用程序中处理用户输入

php - PHP与C++之间的通信