Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Java';s runtime().exec()方法_Java_Bash_Shell_Command - Fatal编程技术网

如何通过Java';s runtime().exec()方法

如何通过Java';s runtime().exec()方法,java,bash,shell,command,Java,Bash,Shell,Command,基本上,当通过Java传递for循环时,我在执行它时遇到了问题。当我直接进入控制台时,它工作,就像“mkdir”、“mv”、“cp”等一样。当我尝试执行for循环时,它说它找不到程序“for” 错误: java.io.IOException: Cannot run program "for": java.io.IOException: error=2, No such file or directory 循环示例: for f in folder/*.filetype; do folder/s

基本上,当通过Java传递for循环时,我在执行它时遇到了问题。当我直接进入控制台时,它工作,就像“mkdir”、“mv”、“cp”等一样。当我尝试执行for循环时,它说它找不到程序“for”

错误:

java.io.IOException: Cannot run program "for": java.io.IOException: error=2, No such file or directory
循环示例:

for f in folder/*.filetype; do folder/scriptname $f > ${f}s; done
在java ex中:

String s = "for f in folder/*.filetype; do folder/scriptname $f > ${f}s; done";
Process process = Runtime.getRuntime().exec(s);
在研究了一下之后,我还尝试:

String s = "for f in folder/*.filetype; do folder/scriptname $f > ${f}s; done";
String[] command = {s};
Process process = new ProcessBuilder(command).start();

BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
 }

process.waitFor();
int exitValue = process.exitValue();
process.destroy();  
按照我在这里找到的方式尝试后,错误变为显示整个字符串,但仍然没有运行

java.io.IOException: Cannot run program "for f in folder/filetype; do folder/scriptname $f > ${f}s; done": java.io.IOException: error=2, No such file or directory

但是我承认我是个新手,实际上不知道这到底是在做什么。

for
mkdir
mv
这样的命令都是shell命令,执行时需要在shell中

如果您将命令放在一个文件中,文件顶部有适当的命令,然后从java调用该命令,那么它应该可以工作


但是,您可以在纯java中执行“for f in folder”部分,然后使用ProcessBuilder执行脚本,例如
for
mkdir
mv
等命令都是shell命令,执行时需要在shell中

如果您将命令放在一个文件中,文件顶部有适当的命令,然后从java调用该命令,那么它应该可以工作


但是,您可以在纯java中执行“for f in folder”部分,然后使用ProcessBuilder执行脚本

您需要手动通过shell而不是直接作为可执行文件运行脚本。您需要手动通过shell而不是直接作为可执行文件运行脚本。