我使用 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/