我正在尝试从JAVA的ProcesBuilder运行keytool命令,但它失败并出现以下错误:
"非法选项:-genkeypairKey和证书管理工具命令:-certreq生成证书请求 - changealias
更改条目的别名-delete删除条目-exportcert导出证书-genkeypair
生成密钥对-genseckey生成密钥-gencert从证书请求生成证书-importcert
导入证书或证书链-importpass导入密码-importkeystore从另一个密钥库导入一个或所有条目
-keypasswd更改条目列表的密钥密码列出密钥库中的条目-printcert
打印证书的内容-printcertreq打印证书请求的内容-printcrl
打印CRL文件的内容-storepasswd更改keystoreUse的商店密码
" keytool -command_name -help"使用command_name"
以下是相同的代码段:
public void testName() throws Exception {
{
String[] commands = { "keytool ", " -genkeypair ", " -keyalg ", " RSA ", " -keysize ", " 2048 ",
" -alias ", " rootkey ", " -dname ", "DNAME_INFORMATION", " -keystore ", " .keystore ",
" -storetype ", " PKCS12 ", " -storepass ", " pass ", " -keypass ", " pass" };
ProcessBuilder probuilder = new ProcessBuilder(commands);
// You can set up your work directory
probuilder.directory(new File(System.getenv("JAVA_HOME") + "\\bin"));
Process process = probuilder.start();
// Read out dir output
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
final StringBuilder commandStr = new StringBuilder();
for (final String command : commands) {
commandStr.append(command);
commandStr.append(" ");
}
System.out.printf("Output of running %s is:\n", commandStr.toString());
is = process.getErrorStream();
if (null != is) {
isr = new InputStreamReader(is, Charsets.UTF_8);
br = new BufferedReader(isr);
}
StringBuffer sbuffer = new StringBuffer();
if (null != br) {
while ((line = br.readLine()) != null) {
sbuffer.append(line);
}
}
// If sbuffer contains some value then error has occured.
// Wait to get exit value
try {
int exitValue = process.waitFor();
System.out.println("\n\nExit Value is " + exitValue);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果我们从%JAVA_HOME%/ bin目录(即cmd)运行相同的命令,那么它会创建.keystore文件而不会出现任何错误。
命令: keytool -genkeypair -keyalg RSA -keysize 2048 -alias rootkey -dname" DNAME_INFORMATION" -keystore .keystore -storetype PKCS12 -storepass pass -keypass pass
如果有人遇到任何此类问题,请告知。
感谢。
答案 0 :(得分:0)
您必须删除参数周围的空格。
String[] commands = { "keytool ", "-genkeypair", "-keyalg", "RSA", "-keysize", "2048",
"-alias", "rootkey", "-dname", "DNAME_INFORMATION", "-keystore", ".keystore",
"-storetype", "PKCS12", "-storepass", "pass", "-keypass", "pass" };
顺便说一下,您可以直接访问密钥存储区,请参阅KeyStore类。