Apache Commons Exec으로 시작한 명령의 표준 입력란에 텍스트 인수를 파이프해야합니다. (호기심에 대한 명령은 gpg이고 인수는 키 저장소에 대한 암호이며 gpg에는 암호를 명시 적으로 제공하는 인수가 없습니다 , 단지 표준에서 그것을 받아들이는).Apache Commons Exec에서 실행되는 실행 파일에 문자열 인수를 파이프하는 방법은 무엇입니까?
또한 Linux와 Windows를 모두 지원해야합니다. 쉘 스크립트에서
내가 할 줄cat mypassphrase|gpg --passphrase-fd
또는
type mypassphrase|gpg --passphrase-fd
있지만 (cmd를 해석 실행하지만, 명령에 내장 된 명령이 아니다으로 유형은 Windows에서 작동하지 않습니다 .exe).
코드이 작동하지 않습니다 (위의 이유로). 이것을 위해 전체 셸을 생성하는 것은 너무 추합니다. 좀 더 우아한 해결책을 찾고있었습니다. 불행히도 BouncyCastle 라이브러리와 PGP 간에는 몇 가지 비호 환성 문제가 있으므로 (매우 짧은 시간에) 완전히 프로그래밍 방식의 솔루션을 사용할 수는 없습니다.
미리 감사드립니다. gpg
명령을 받아 들일 수 없기 때문에
CommandLine cmdLine = new CommandLine("type");
cmdLine.addArgument(passphrase);
cmdLine.addArgument("|");
cmdLine.addArgument("gpg");
cmdLine.addArgument("--passphrase-fd");
cmdLine.addArgument("0");
cmdLine.addArgument("--no-default-keyring");
cmdLine.addArgument("--keyring");
cmdLine.addArgument("${publicRingPath}");
cmdLine.addArgument("--secret-keyring");
cmdLine.addArgument("${secretRingPath}");
cmdLine.addArgument("--sign");
cmdLine.addArgument("--encrypt");
cmdLine.addArgument("-r");
cmdLine.addArgument("recipientName");
cmdLine.setSubstitutionMap(map);
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
아주 좋은 대답을 : 미리 떠들썩한 파티를 호출하지 않고 https://github.com/Macilias/Utils/blob/master/ShellUtils.java
는 기본적으로보다 전에 여기에 표시된 같은 파이프 사용을 시뮬레이션 할 수 있습니다 ! 나의 날을 구했다! – BetaRide