如何以编程方式将输入传递给Java keytool?
我正试图解决开源项目中的一个问题 问题出现在方法如何以编程方式将输入传递给Java keytool?,java,ssl,keytool,jcabi,Java,Ssl,Keytool,Jcabi,我正试图解决开源项目中的一个问题 问题出现在方法com.jcabi.ssl.maven.plugin.Keytool#genkey: @Loggable(Loggable.DEBUG) public void genkey() throws IOException { final Process proc = this.proc( "-genkeypair", "-alias", "localhost", "-keyalg"
com.jcabi.ssl.maven.plugin.Keytool#genkey
:
@Loggable(Loggable.DEBUG)
public void genkey() throws IOException {
final Process proc = this.proc(
"-genkeypair",
"-alias",
"localhost",
"-keyalg",
"RSA",
"-keysize",
"2048",
"-keypass",
this.password
).start(); // Here we launch the keytool
final PrintWriter writer = new PrintWriter(
new OutputStreamWriter(proc.getOutputStream())
);
writer.print("localhost\n"); // These and other writer.print(...) statements
writer.print("ACME Co.\n"); // write answers to questions of keytool
writer.print("software developers\n");
writer.print("San Francisco\n");
writer.print("California\n");
writer.print("US\n");
writer.print("yes\n");
writer.close();
new VerboseProcess(proc).stdout();
Logger.info(
this,
"Keystore created in '%s' (%s)",
this.keystore,
FileUtils.byteCountToDisplaySize(this.keystore.length())
);
}
本守则旨在
“C:\Program Files\Java\jdk1.8.0\jre\bin\keytool”-genkeypair-alias localhost-keyalg RSA-keysize 2048-keypass some password-storetype jks-noprompt-storepass some password-keystore C:\Users\DP118M\AppData\Local\Temp\junit4133481088660376680\keystore.jks
然后,keytool会问您几个问题(见下文,翻译自德语):
上面的代码使用writer.print(…),而不是手动输入答案(A
)代码>语句
但它们显然不起作用,因为当我运行一个测试时,其中包括使用keytool(例如),我会得到以下错误:
java.lang.IllegalArgumentException: Non-zero exit code 1: Keytool Error: java.lang.RuntimeException: Too many repeated attempts. Program will be terminated.
\n
at com.jcabi.log.VerboseProcess.stdout(VerboseProcess.java:220)
at com.jcabi.log.VerboseProcess.stdout(VerboseProcess.java:158)
at com.jcabi.ssl.maven.plugin.Keytool.genkey(Keytool.java:116)
at com.jcabi.ssl.maven.plugin.Keystore.activate(Keystore.java:121)
at com.jcabi.ssl.maven.plugin.CacertsTest.importsCertificatesFromKeystore(CacertsTest.java:64)
我假设这个问题的根本原因是writer.print(…)代码>语句无法将其文本传递到keytool
我如何解决这个问题(确保在com.jcabi.ssl.maven.plugin.Keytool#genkey
方法中,writer.print(…);
编写的消息正确地传递到Keytool
)
更新1(2014年12月28日MSK):
我想我找到了原因。我正在Windows上运行测试,所以调用了writer.print(“localhost\n”)
需要替换为writer.print(“localhost”+System.getProperty(“line.separator”)代码>
这很容易
但还有另一部分。显然,这段代码的开发人员使用的是英语版本的keytool,因此在最后它需要一个yes
(参见上面示例输出keytool中的最后一个问题)
因此,有这样一种说法
writer.print("yes\n");
但我使用的是德文Windows版本,因此在我的情况下,正确的回答不是yes
,而是Ja
如果我像这样更改代码,它会工作(测试不会失败):
我很感激您给我一些提示,告诉我如何让这部分代码在任何语言的操作系统上工作(如何避免硬编码yes
和Ja
)。(1)如果您使用-dname
将主题名称字段作为参数传递,请注意,有几个字段组合成一个参数,如CN=localhost,O=ACME Co。,etc
——它不会问“全部正确”的问题。请参阅keytool
doc。(2) 但是您没有指定-storepass
,因此(无论哪种方式)keytool
都应该提示该提示,而您没有回答该提示,因此它现在不应该工作。请参阅keytool
doc。是否可以改用java的KeyStore
类?像这样的事情,这会简单得多
writer.print("yes\n");
final String newLine = System.getProperty("line.separator");
writer.print("localhost" + newLine);
writer.print("ACME Co." + newLine);
writer.print("software developers" + newLine);
writer.print("San Francisco" + newLine);
writer.print("California" + newLine);
writer.print("US" + newLine);
writer.print("Ja" + newLine);