[英]I am having below error while running curl command using java in Mac where it gives IOException and says cannot run the program
Below is my curl for a POST request in java:下面是我在 java 中的 POST 请求的 curl:
String payload1 = "curl --location --request POST 'https://some_url/abc' \\\n" +
"--header 'Api-Key: RIQOWU-9asds943msd9d934nmwdw0d' \\\n" +
"--header 'Content-Type: application/json' \\\n" +
"--header 'BarId: di292sdk/dmasdaskdnasd934n3sda0334n5Yz1QlVnnTmJ39U4aJT8/9sdns/Xwpen/asdksaeibwae+292nsadsaweg3ARyWwAA0LuSDIL3s+A/EVkLoVhc=' \\\n" +
"--header 'Accept: application/json;v=2' \\\n" +
"--header 'Cookie: JSESSIONID=9SDKSDSD9M23B4D46BF663F2B51E75137' \\\n" +
"--data-raw '{\n" +
"\"AU\": \"EC\",\n" +
" \"is\": \"is\",\n" +
" \"ls\": \"js\",\n" +
" \"Timestamp\": \"2021-06-26T18:43:00Z\"\n" +
"}'";
ProcessBuilder process = new ProcessBuilder(payload1);
Process p;
p = process.start();
Below is the relevant data from response以下是来自响应的相关数据
java.io.IOException: Cannot run program "/usr/local/Cellar/curl/7.71.1/bin/curl.exe --location --request POST '..........
: error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at com.aws.s3.service.EntryPoint.POSTAPI(EntryPoint.java:343)
at com.aws.s3.service.EntryPoint.main(EntryPoint.java:72)
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)
... 2 more
Process finished with exit code 0
I've tried mentioning the path of curl too in the String like below but still getting the same error.我也尝试在下面的字符串中提及 curl 的路径,但仍然出现相同的错误。
String payload1 = "/usr/local/Cellar/curl/7.71.1/bin/curl.exe "+"--location --request POST......above payload......"
Please let me know if what could be the cause of it not able to interpret curl command in java.如果无法解释 java 中的 curl 命令,请告诉我。 Greatly appreciated!!
非常感激!!
Are you certain the path to curl is correct - does it end ".exe"?您确定 curl 的路径是否正确 - 它是否以“.exe”结尾? I would have thought Mac version had no ".exe".
我原以为 Mac 版本没有“.exe”。 Validate the executable path used in the call with:
验证调用中使用的可执行路径:
File curl = new File("/usr/local/Cellar/curl/7.71.1/bin/curl");
System.out.println("curl.exists()="+curl.exists());
System.out.println("Files.isExecutable()="+Files.isExecutable(curl.toPath()));
Runtime.exec has Runtime.exec(String)
but there is no ProcessBuilder(String)
- that has ProcessBuilder(String...)
. Runtime.exec 有
Runtime.exec(String)
但没有ProcessBuilder(String)
- 有ProcessBuilder(String...)
。 As your parameter is complex switch to String[], and this rules out issue with punctuation inside your command which will not require the line continuation values \\\n
:由于您的参数是复杂的切换到 String[],这排除了命令中的标点符号问题,不需要行继续值
\\\n
:
String [] payload1 = new String[] { curl.getAbsolutePath(),
"--location", "--request", "POST"
, "https://some_url/abc"
, "--header", "Api-Key: RIQOWU-9asds943msd9d934nmwdw0d"
... etc
, "--data-raw", "{\n" +
"\"AU\": \"EC\",\n" +
" \"is\": \"is\",\n" +
" \"ls\": \"js\",\n" +
" \"Timestamp\": \"2021-06-26T18:43:00Z\"\n" +
"}"
};
Now you have Pipe errors, so if you send data to the process getOutputstream you should close() it, and ideally redirect the process output and error streams to file with:现在你有 Pipe 错误,所以如果你将数据发送到进程 getOutputstream 你应该 close() 它,理想情况下重定向进程 output 和错误流到文件:
process.redirectOutput(file); process.redirectOutput(文件); process.redirectError(file):
process.redirectError(文件):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.