如何从Java调用Linux Shell命令 [英] How to invoke a Linux shell command from Java

查看:439
本文介绍了如何从Java调用Linux Shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用重定向(>&)和管道(|)从Java执行一些Linux命令. Java如何调用cshbash命令?

I am trying to execute some Linux commands from Java using redirection (>&) and pipes (|). How can Java invoke csh or bash commands?

我试图用这个:

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"});

相反.

: 我的系统上没有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆