2016-07-06 2 views
2

Java Runtime.getRuntime(). exec()를 에 사용하려고합니다. ssh-keygen linux 유틸리티를 사용하여 개인 키에서 공개 키를 추출하십시오. 내가 터미널에서이 명령을 실행하면비밀 키에서 공개 키를 추출하는 자바의 ssh-keygen 명령

, 그것은 완벽한 작품과 내가 자바를 사용하여 동일한 명령을 실행할 때 RSA 개인 키

ssh-keygen -y -f /home/useraccount/private.txt > /home/useraccount/public.txt 

그러나이 작성하지 않습니다에서 공개 키를 추출 할 수있어 public.txt 파일. 어떤 오류도 발생시키지 않습니다.

왜 그런지 궁금한가요?

+0

는, 쉘은 실행하기 전에 리디렉션을 수행 프로그램. Java'Runtime.exec()'는 리디렉션을하지 않습니다. (1)'Process.getInputStream()'에서 읽은 다음 파일에 직접 쓰십시오; (2)'ProcessBuilder'를'.redirectOutput()'과 함께 사용하여 리디렉션을 수행하십시오; 또는 (3)'.exec (String ...) '오버로드를 사용하여 예를 들어 실행하십시오. 'sh '를'-c' 그리고 (단 하나의 인자로!) 쉘이 파싱하고 처리하는 전체 커맨드 라인을 사용한다. –

+0

샘플을 공유해주세요. – sunny

답변

0

별로 대답 내가 테스트 할 시간이 있지만, 기본 옵션이 없기 때문에하십시오 shell_를 _to 당신은`> file` 등으로 명령을 입력 할 때

// example code with no exception handling; add as needed for your program 

String cmd = "ssh-keygen -y -f privatefile"; 
File out = new File ("publicfile"); // only for first two methods 

//// use the stream //// 
Process p = Runtime.exec (cmd); 
Files.copy (p.getInputStream(), out.toPath()); 
p.waitFor(); // just cleanup, since EOF on the stream means the subprocess is done 

//// use redirection //// 
ProcessBuilder b = new ProcessBuilder (cmd.split(" ")); 
b.redirectOutput (out); 
Process p = b.start(); p.waitFor(); 

//// use shell //// 
Process p = Runtime.exec ("sh", "-c", cmd + " > publicfile"); 
// all POSIX systems should have an available shell named sh but 
// if not specify an exact name or path and change the -c if needed 
p.waitFor(); 
관련 문제