exec()没有在Debian 7上工作

时间:2014-06-16 20:26:02

标签: java linux debian exec keytool

我使用keytool在程序中自动通过Runtime.getRuntime().exec()创建了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)有任何已知的兼容性问题吗?

1 个答案:

答案 0 :(得分:0)

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