想从Java调用linux shell命令 [英] Want to invoke a linux shell command from Java
本文介绍了想从Java调用linux shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用重定向(>&)和管道(|)从Java代码执行一些Linux命令。如何在csh或bash中调用该命令?
I am trying to execute some Linux commands from Java code using redirection (>&) and pipes (|). How can Java invoke that command in csh or bash?
我试图使用这个:
Process p = Runtime.getRuntime().exec("shell command");
但它与重定向或管道不兼容。
But it's not compatible with redirections or pipes.
推荐答案
exec不在您的shell中执行命令
exec does not execute a command in your shell
请尝试
Process p = Runtime.getRuntime().exec(new String[]{"csh","-c","cat /home/narek/pk.txt"});
。
EDIT ::
我的系统上没有csh,所以我改用bash。下面为我工作
: I don't have csh on my system so I used bash instead. The following worked for me
Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","ls /home/XXX"});
这篇关于想从Java调用linux shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文