概述
java 抛IO异常
java.io.IOException: 断开的管道
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:318)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:297)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at com.zehin.util.exec.ExecuteCommand.re_excuteCommand(ExecuteCommand.java:60)
命令比如 command="sudo echo 111 > 123.txt"
re_excuteCommand方法如下
public static void re_excuteCommand(String command) {
try {
Vector commands = new Vector();
commands.add("/bin/bash");
commands.add("-c");
commands.add(command);
ProcessBuilder p = new ProcessBuilder(commands);
Process process = p.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = br.readLine();
OutputStreamWriter out = new OutputStreamWriter(
process.getOutputStream());
out.write("123456");//这个是ubuntu的密码
out.flush();
out.close();
System.out.println("BASHPID=" + line);
br.close();
isr.close();
is.close();
process.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
最后
以上就是幸福可乐为你收集整理的java exec 关闭_java 抛IO异常 用runtime.exec 执行linux命令的全部内容,希望文章能够帮你解决java exec 关闭_java 抛IO异常 用runtime.exec 执行linux命令所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复