java.io.IOException: Cannot run program error=2, No such file or directory

假设你在shell下要执行abc.sh -c conf download hive --query "select" -f file,这时候如果在java的processbuilder中需要调用该shell命令要如何处理呢?

错误一:

args[0]="abc.sh -c conf download hive --query "select" -f file"

将整个命令拼成一个字符串,这个是错误的做法

错误二:

args[0]=abc.sh

args[1]=-c conf

将部分命令拼成一个字符串

正确的做法是:每个字符串中间都不能有空格,有空格的地方都应该放到数组中,同时要注意“”“”引号不需要带上,因为在字符串数组里已经当成了一个字符串,不需要引用起来,格式如下:
String args[] =new String[9];
                args[0] = CommonUtils.bdpClientPath;
                args[1] = "-c";
                args[2] = CommonUtils.bdpConfPath;
                args[3] = "download";
                args[4] = "hive";
                args[5] = "--query";
                args[6] = sqlcommand;  //注意这里的sqlcommand没有引号
                args[7] = "-f";
                args[8] = CommonUtils.baseDir + jobid;

ProcessBuilder pb = new ProcessBuilder(args);

猜你喜欢

转载自blog.csdn.net/shyrainxy/article/details/107239720