英文:
I am having below error while running curl command using java in Mac where it gives IOException and says cannot run the program
问题
以下是我的Java中用于POST请求的curl代码:
String payload1 = "curl --location --request POST 'https://some_url/abc' " +
"--header 'Api-Key: RIQOWU-9asds943msd9d934nmwdw0d' " +
"--header 'Content-Type: application/json' " +
"--header 'BarId: di292sdk/dmasdaskdnasd934n3sda0334n5Yz1QlVnnTmJ39U4aJT8/9sdns/Xwpen/asdksaeibwae+292nsadsaweg3ARyWwAA0LuSDIL3s+A/EVkLoVhc=' " +
"--header 'Accept: application/json;v=2' " +
"--header 'Cookie: JSESSIONID=9SDKSDSD9M23B4D46BF663F2B51E75137' " +
"--data-raw '{ " +
"\"AU\": \"EC\", " +
"\"is\": \"is\", " +
"\"ls\": \"js\", " +
"\"Timestamp\": \"2021-06-26T18:43:00Z\" " +
"}'";
ProcessBuilder process = new ProcessBuilder(payload1);
Process p;
p = process.start();
以下是响应中的相关数据:
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
我已经尝试在字符串中提到了curl的路径,如下所示,但仍然遇到相同的错误。
String payload1 = "/usr/local/Cellar/curl/7.71.1/bin/curl.exe " + "--location --request POST......上面的payload......";
请让我知道为什么无法在Java中解释curl命令的原因。非常感谢!
如果您需要进一步的帮助,请告诉我。
英文:
Below is my curl for a POST request in java:
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.
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. Greatly appreciated!!
答案1
得分: 2
你确定curl的路径正确吗 - 是否以“.exe”结尾?我本以为Mac版本没有“.exe”。请验证调用中使用的可执行文件路径:
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有Runtime.exec(String)
,但没有ProcessBuilder(String)
- 它有ProcessBuilder(String ...)
。由于您的参数比较复杂,请切换到String[],这可以避免您的命令中存在标点符号问题,不需要行连续值\\\n
:
String [] payload1 = new String[] { curl.getAbsolutePath(),
"--location", "--request", "POST"
, "https://some_url/abc"
, "--header", "Api-Key: RIQOWU-9asds943msd9d934nmwdw0d"
... 等等
, "--data-raw", "{\n" +
"\"AU\": \"EC\",\n" +
" \"is\": \"is\",\n" +
" \"ls\": \"js\",\n" +
" \"Timestamp\": \"2021-06-26T18:43:00Z\"\n" +
"}"
};
现在您遇到了管道错误,所以如果您将数据发送到进程的getOutputStream,您应该关闭()它,并最好将进程的输出和错误流重定向到文件:
process.redirectOutput(file);
process.redirectError(file);
英文:
Are you certain the path to curl is correct - does it end ".exe"? I would have thought Mac version had no ".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 ...)
. 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 [] 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:
process.redirectOutput(file);
process.redirectError(file):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论