linux command not running in Java application

> **Possible Duplicate:** > [How to use Pipe Symbol in Java](https://stackoverflow.com/questions/7226538/how-to-use-pipe-symbol-in-java) Hi I wanted to run some linux command through my Java application. When I run the below command neither it throws the exception nor gives the output.But that particular command when I run from the linux command prompt, it executed properly.Also when I gave "ls -al" command through java application it worked properly. So how to make it work? Following is the command. String cmd = "dir | grep gpc | grep -v 25"; Following is my java program public class Test { public static void main(String[] args) { //String cmd = "ls -al"; String cmd ="dir | grep gpc | grep -v 25" ; String property = System.getProperty("user.dir"); System.out.println("current directory:::: "+property); String cmd =property+File.separator+"test1.sh" ;*/ //String cmd ="ls -al" ; //String cmd = args[0]; String cmd = dir | grep gpc | grep -v 25; System.out.println("executing command is:: "+cmd); //String cmd ="dir | grep gpc | grep -v 25" ; Runtime run = Runtime.getRuntime(); Process pr = null; try { pr = run.exec(cmd); } catch (IOException e) { e.printStackTrace(); } try { pr.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } BufferedReader buf = new BufferedReader(new InputStreamReader( pr.getInputStream())); String line = ""; try { while ((line = buf.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }

以上就是linux command not running in Java application的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏