[英]Java.io.IOException: error=2, No such file or directory on executing curl in java
I'm using curl for certificate-based/x509 authentication ` 我正在使用curl进行基于证书的/ x509身份验证
String command = "curl --cert " +certificateFilePath+" --key "+ certificateKeyFilePath+" --insecure "+authUrl;
ProcessBuilder process = new ProcessBuilder(command);
Process p;
try
{
p = process.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.print("result ------------------: "+result);
}catch(Exception e){
e.printStackTrace();
}
` `
but getting exception ` 但得到例外
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:247)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
` `
given that the application and auth service are running inside docker-container on Ubuntu and works fine when I manually go inside the application docker-container and execute the above curl command. 鉴于应用程序和身份验证服务在Ubuntu上的docker-container内部运行,并且当我手动进入应用程序docker-container并执行上述curl命令时,可以正常工作。 Please help
请帮忙
From process builder docs: 从流程构建器文档中:
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
The proper way to do it is to call it with the parameters passed as separate strings otherwise it won't work. 正确的方法是使用作为单独字符串传递的参数来调用它,否则它将不起作用。 Also in order to run system commands like
curl
in linux or dir
in windows you need to call the command interpreter - sh for linux. 同样,为了在linux中运行
curl
或在Windows中运行dir
等系统命令,您需要调用命令解释程序-sh for linux。 So your command should become something like: 因此,您的命令应如下所示:
ProcessBuilder process = new ProcessBuilder("sh", "-c","curl","--cert", certificationPath......);
Or you can try: 或者您可以尝试:
Runtime.getRuntime().exec(command); //Here you pass the whole command as a string
您是否可以尝试在root用户下或使用sudo java运行JVM。...它可能没有足够的JVM权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.