java - exec() 在 Debian 7 上不起作用

标签 java linux debian exec keytool

我使用 Runtime.getRuntime().exec() 在程序中通过 keytool 自动创建了 SSL 证书,并且它在 Windows 上运行良好。

当我尝试在 Debian 7 上运行代码时,它只是不执行该命令。好吧,它确实执行了,但返回错误代码 1,这基本上意味着出了问题。

如果我让我的程序打印要执行的字符串,然后手动执行此命令,那么一切都会正常。

这是带有一些调试输出的代码:

private static void initKeystore()
{
    Scanner scr = new Scanner(System.in);
    System.out.println("**** IMPORTANT ****");
    System.out.println("You don't have a certificate for using SSL yet.");
    System.out.println("We will create it together now!");
    System.out.println();
    System.out.print("Name of your organisation: ");
    String ssl_o = scr.nextLine();
    String ssl_ou = ssl_o;
    System.out.print("Your name: ");
    String ssl_cn = scr.nextLine();
    System.out.print("Country code [de/en/fr]: ");
    String ssl_c = scr.nextLine();
    System.out.print("City: ");
    String ssl_l = scr.nextLine();
    System.out.print("State: ");
    String ssl_st = scr.nextLine();
    System.out.print("Password: ");
    String pass = new String(scr.nextLine());

    String execString = "keytool -genkey -keyalg RSA -alias dreamim -keystore " + KEYSTORE + " -storepass " + pass + " -validity 360 -dname \"cn=" + ssl_cn + ", o=" + ssl_o + ", ou=" + ssl_ou + ", c=" + ssl_c + ", l=" + ssl_l + ", st=" + ssl_st + "\" -keypass " + pass;
    System.out.println(execString);
    try
    {
        Process proc = Runtime.getRuntime().exec(execString);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null)
        {
            System.out.println(line);
        }
        bufferedReader.close();

        proc.waitFor();
        System.out.println("Key generation exited with code: " + proc.exitValue());

        execString = "keytool -export -keystore " + KEYSTORE + " -storepass " + pass + " -alias dreamim -file DreamIM.crt";
        proc = Runtime.getRuntime().exec(execString);
        proc.waitFor();

    }
    catch (Exception e)
    {
            // TODO: Improve exception handling
        e.printStackTrace();
    }

    scr.close();
}

我的代码没有抛出任何异常... exec() 和 Debian 7 (OpenJDK) 是否存在任何已知的兼容性问题?

最佳答案

我终于解决了这个问题。这就像我想的那么简单。我只是使用了 exec(String[]) 方法并手动提供了参数。 我猜想这可能是因为 -dname 选项没有正确转义,Windows 忽略了它或自动更正了它。

关于java - exec() 在 Debian 7 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24251642/

相关文章:

java - 释放 JavaFX 资源

linux - 在 Linux 上安装共享库需要帮助

c - 一次性执行函数

mysql - 安装 MariaDB 归档引擎

debian 5 (lenny) 下带有 socket.io 的 node.js 退出并出错

java - 1.sys_refcursor 2.自定义 JSON 3.自定义对象/记录类型从 Oracle 12c plsql 过程,Java 8 中选择哪个更好?

java - 如何获取集合中的最大字段值?

linux - Nginx - 100% 使用 CPU

java - 类显示在自动完成中但无法导入 -IntelliJ

linux - 如何管理 git merge 基本上有两个主人?