简体   繁体   English

无法使用processBuilder执行linux命令

[英]Not able to execute linux command using processBuilder

I using Processbuilder as follows: 我使用Processbuilder如下:

            List<String> args = initArgs();
            ProcessBuilder pb = new ProcessBuilder(args);
            tdmServerProcess = pb.start();

            handleInfoLog(tdmServerProcess.getInputStream());
            handleErrorLog(tdmServerProcess.getErrorStream());

In the initArgs method I am setting the linux commands I want to execute, as follows: initArgs方法中,我正在设置要执行的linux命令,如下所示:

args.add("java -version");
args.add(javaHome + File.separator + "bin" + File.separator + "java");

first command is getting executed successfully. 第一条命令成功执行。 But second is failing with following error: 但是第二个失败,并显示以下错误:

/opt/java7/bin/java: cannot execute binary file / opt / java7 / bin / java:无法执行二进制文件

though i have execute permission, following are permission bits: 虽然我具有执行权限,但以下是权限位:

-r-xr-xr-x 

I am able to execute the same command from terminal. 我可以从终端执行相同的命令。

The error "cannot execute binary file" means that the java file is not valid. 错误“无法执行二进制文件”表示Java文件无效。 Maybe the 'java -version' is actually running java from another location in your path, and the /opt/java7/bin/java is not valid. 也许'java -version'实际上是从路径中的另一个位置运行Java,而/ opt / java7 / bin / java无效。 Try running the '/opt/java7/bin/java' manually, and check the result. 尝试手动运行“ / opt / java7 / bin / java”,然后检查结果。 Check also the location of java in your path by running which java using the same user that your are running your java code with. 通过使用与您运行Java代码的用户相同的用户运行which java还可以检查Java在路径中的位置。

You apparently are trying to execute two separate commands, first "java -version" and then "/opt/java7/bin/java" ? 您显然正在尝试执行两个单独的命令,首先是“ java -version”,然后是“ / opt / java7 / bin / java”?

That's not how it works. 那不是它的工作原理。 Rather, all of the arguments are appended and one process is started. 而是,将附加所有参数并启动一个过程。 In your case, the command is "java -version /opt/java7/bin/java" 在您的情况下,命令是“ java -version / opt / java7 / bin / java”

Try running that at the console - I'm betting that you get the same error. 尝试在控制台上运行它-我敢打赌,您会遇到相同的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM