我正在尝试使用Java的Runtime.getRuntime()。exec()使用ssh-keygen linux实用程序从私钥中提取公钥。
当我在终端上运行此命令时,它可以完美运行,并且能够从RSA私钥中提取公钥。
ssh-keygen -y -f /home/useraccount/private.txt > /home/useraccount/public.txt
但是,当我使用Java运行相同的命令时,它不会创建public.txt文件。它也不会引发任何错误。
Process p = Runtime.getRuntime().exec("ssh-keygen -y -f /home/useraccount/private.txt > /home/useraccount/public.txt");
p.waitFor();
我想知道为什么呢?
并不是真正的答案,因为我不必花时间测试,而是选择基本选项:
// 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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句