[英]Java ProcessBuilder can't find file in PATH
I try to run curl
from my java application. 我尝试从Java应用程序运行
curl
。 Here is my code: 这是我的代码:
ProcessBuilder pb = new ProcessBuilder("curl",
"-T",
"\"" + tmp_dir + "client-" + node_id + ".war\"",
"\"" + tomcat_url + "/deploy?path=/client" + node_id + "&update=true\"");
Process p = pb.start();
yet, it gives the exception: 但是,它给出了例外:
java.io.IOException: Cannot run program "curl": CreateProcess error=2, The system cannot find the file specified
curl
is in my System PATH and user PATH, and when I run curl
from cmd
it works perfectly: curl
在我的系统路径和用户路径中,当我从cmd
运行curl
,它可以正常运行:
Also, when I give ProcessBuilder absolute path to curl
executable, it works, but I don't want to do that since my code should work with curl
whatver directory it's installed into 另外,当我给ProcessBuilder提供
curl
可执行文件的绝对路径时,它可以工作,但是我不想这样做,因为我的代码应该可以在curl
安装到的目录中工作
What am I doing wrong? 我究竟做错了什么?
It is the shell (or command processor) that locates a file among the directories defined in the PATH variable. 是外壳程序(或命令处理器)在PATH变量中定义的目录中定位文件。
Simply starting a subprocess with a path to a command and a list of arguments does not go through the shell, unless you explicitly code it to do so. 仅通过带有命令路径和参数列表的子过程启动子过程,除非您明确对其进行编码,否则它不会通过外壳。
If you can't offload the exact position of curl into some system property, you may have to interpret PATH and try all directories and then call with the full pathname. 如果无法将curl的确切位置转移到某些系统属性中,则可能必须解释PATH并尝试所有目录,然后使用完整路径名进行调用。
public static Path findAbsolutePath( String curl ){
String path = System.getenv( "PATH" );
String[] dirs = path.split( ";" );
for( String dir: dirs ){
Path toCurl = Paths.get( dir, curl );
File curlFile = new File( toCurl.toString() );
if( curlFile.canExecute() ) return toCurl;
}
return null;
}
Or call the command processor. 或致电命令处理器。
ProcessBuilder#ProcessBuilder(String... command) javadoc: ProcessBuilder#ProcessBuilder(String ... command) javadoc:
It is not checked whether command corresponds to a valid operating system command.
不检查命令是否对应于有效的操作系统命令。
So you need to build the absolute path to curl in the string you pass to the constructor. 因此,您需要构建绝对路径来卷曲传递给构造函数的字符串。
I am posting this answer because i was not able to fix this problem with all the existing answer: 我发布此答案是因为我无法使用所有现有答案解决此问题:
This is one of the answers for any beginner.(mostly combination of all the existing answer). 这是任何初学者的答案之一(主要是所有现有答案的结合)。
curl
is installed. curl
。 (follow the google to install) curl
bin
path to environment system variable curl
bin
路径添加到环境系统变量 processBuilder
was using the PATH
that was not having the CURL in it. Restarting fixed it) This is reason for the original error in the question. processBuilder
使用的是其中没有CURL的PATH
。重新启动已对其进行修复。)这是问题中原始错误的原因。 System.getenv( "PATH" )
to verify on the code. System.getenv( "PATH" )
在代码上进行验证。 Addon : If you are using POST
, then make sure the post payload is escaped when running through WINDOWS. 插件 :如果您使用的是
POST
,那么请确保在通过WINDOWS运行时,转义了有效载荷。 and NOT escaped when running through MAC. 通过MAC运行时不会逃脱。 I really do not know the problem but it took lot time to debug.
我真的不知道这个问题,但是调试花费了很多时间。 Escape in the sense:
"
with \\"
. 从某种意义上逃脱:
"
with \\"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.