失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java Runtime.exec() hangs

Java Runtime.exec() hangs

时间:2018-12-07 16:37:43

相关推荐

Java Runtime.exec() hangs

有一篇总结的很不错的文章:

/javaworld/jw-12-2000/jw-1229-traps.html?page=3

有两种方法可以解决这个问题:

1.采用apache common exec

2.采用多线程

public static void main(String[] args) {try {Process process = Runtime.getRuntime().exec("E:\\test\\dbbackup.cmd");new MyThread(process.getErrorStream()).start();new MyThread(process.getInputStream()).start();int status = process.waitFor();if (status == 0) {System.out.println("exit success");} else {System.out.println("exit fail");}} catch (Exception e) {System.out.println("exception occurs......");e.printStackTrace();}}public class MyThread extends Thread{BufferedReader bf;public MyThread(InputStream input){bf=new BufferedReader(new InputStreamReader(input));}public void run(){String line;try {line = bf.readLine();while(line!=null){System.out.println(line);line=bf.readLine();}} catch (IOException e) {e.printStackTrace();}}}

如果觉得《Java Runtime.exec() hangs》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。