【问题标题】:exec() not working on Debian 7exec() 无法在 Debian 7 上运行
【发布时间】:2014-06-16 20:26:02
【问题描述】:

我使用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() 和 Debian 7 (OpenJDK) 是否存在已知的兼容性问题?" => 没有。显示您的代码。
  • keytool 可以特别关注参数的顺序。您需要显示一些代码。
  • 我编辑了问题并添加了一些代码。我很确定这不是参数的顺序,因为正如我在问题中所说,当我手动执行 keytool 命令时,一切正常,并且在 Windows 中它也可以使用此代码自动运行。只有 Linux 不工作。

标签: java linux debian exec keytool


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2011-11-17
    相关资源
    最近更新 更多