我对这种 java 与 Unix 的集成很陌生。
我想做的是
String command="passwd";
Runtime rt=Runtime.getRuntime();
try {
Process pc=rt.exec(command);
try {
pc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedReader buf = new BufferedReader(new InputStreamReader(pc.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
BufferedReader buf1 = new BufferedReader(new InputStreamReader(pc.getErrorStream()));
String line1 = "";
while ((line1=buf1.readLine())!=null) {
System.out.println("Error--"+line1);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("IOException---"+e1.getMessage());
}
现在,当我尝试传递“passwd”命令时,Unix 环境进入挂起模式。
我想知道如何使用 java 代码将旧密码、新密码和确认新密码传递给 shell。
最佳答案
您需要使用混淆名称 Process.getOutputStream() 来传递它.来自文档:
Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object
请注意,您需要同时 捕获进程 stdout/err 以避免阻塞。参见 this answer了解更多详情。
关于java - 如何通过java将参数传递给shell命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12836166/