linux使用java本地执行cd命令问题

来源:互联网 发布:仙侠道是哪个网络 编辑:程序博客网 时间:2024/05/21 02:19

使用java在本地执行linux的cd命令,一直报错“java.io.IOException: Cannot run program "cd": java.io.IOException: error=2, No such file or directory”。

代码如下:String cmd = "cd " + path + ";ls -Gt | head -n" + num;

Process ps  = Runtime.getRuntime().exec(cmd);


后来代码修改为:

String[] cmd = {"/bin/sh", "-c", "cd " + path + ";ls -Gt | head -n" + num};

Process ps  = Runtime.getRuntime().exec(cmd);

问题解决。

0 0