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

查看:30
本文介绍了如何从 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天全站免登陆