我正在尝试运行以下命令:
gpg -e -r recipientname file.txt
通过Java使用ProcessBuilder API。该代码将在以后的工作中使用。
如果我从命令行运行命令,则会得到以下输出:
gpg: [undisclosed]: There is no assurance this key belongs to the named user
sub rsa2048/[undisclosed] 2019-03-22 [undisclosed] <[undisclosed]>
Primary key fingerprint: [undisclosed]
Subkey fingerprint: [undisclosed]
It is NOT certain that the key belongs to the person named
in the user ID. If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N)
我输入y,然后生成我的加密文件。
如果我使用Java进行如下操作:
ProcessBuilder builder = new ProcessBuilder("gpg", "-e", "-r", "recipientname", "test.txt")
.directory(new File("."))
.redirectErrorStream(true);
final Process process = builder.start();
OutputStream output = process.getOutputStream();
output.write("y".getBytes());
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
System.out.println("test");
process.waitFor();
我只得到输出的第一行:
gpg: [undisclosed]: There is no assurance this key belongs to the named user
并且jvm永远挂起。我怀疑gpg正在引擎盖下启动另一个子流程,该子流程会生成所有额外的输出并要求用户确认,如果是这种情况,我想知道是否仍然可以拦截该子流程并与之交互在我的Java代码中?
请注意,如果可能的话,我宁愿避免使用第三方库。