我需要将x509证书导入到trustStore中。我有这个命令:
keytool -import -file C:\Users\LU$er\Documents\NetBeansProjects\TryCrypto\firstCA.crt -alias CARoot -keystore myTrustStore .
我想执行此命令不是通过打开cmd,而是通过执行我的Java程序。但它只是打开cmd而没有任何反应。
以下是代码:
String AppPath = System.getProperty ("user.dir") ;
String totalPath = AppPath + "\\firstCA.crt" ;
String command = " keytool " +
" -import "+
" -file "+ totalPath + " " +
" -alias " + "CARoot" + " " +
" - keystore " + "myTrustStore" + " " ;
Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{"cmd.exe","/c","start",command });
答案 0 :(得分:0)
问题是传递给cmdarray
方法的exec()
参数
java.lang.Runtime.exec()
声明为:
public Process exec(String cmdarray[]) throws IOException {
它说:
在单独的进程中执行指定的命令和参数。
cmdarray 包含要调用的命令及其参数的数组。
你不必像对待自己那样处理争论之间的空白
对于此方法,数组的每个String
元素都被视为传递参数
该方法不会检查单个String
元素是否包含空格,并且不会将它们拆分以生成要传递的新参数。
所以整个String
:
String command = " keytool " +
" -import "+
" -file "+ totalPath + " " +
" -alias " + "CARoot" + " " +
" - keystore " + "myTrustStore" + " " ;
作为单个参数传递,而实际上有多个参数要传递。
通过将每个参数作为不同的String
元素传递来替换String
个连接,它应该没问题:
String[] allCommands = new String[] { "cmd.exe", "/c", "start",
"keytool", "-import", "-file", totalPath, "-alias", "CARoot", "-keystore" , "myTrustStore" };
Process process = Runtime.getRuntime().exec(allCommands);