简体   繁体   English

我在 Mac 中使用 java 运行 curl 命令时遇到以下错误,它给出 IOException 并说无法运行程序

[英]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.

相关问题 通过java程序在IBM云中运行shell命令,将抛出java.io.IOException错误:无法运行program = 13,权限被拒绝 - Running a shell command in IBM cloud through java programme throwing error as java.io.IOException: Cannot run program =13, Permission denied sc.textFile() 在运行命令以获取文件权限时出错:java.io.IOException: (null) entry in command string - sc.textFile() gives Error while running command to get file permissions : java.io.IOException: (null) entry in command string 通过Java运行curl命令时出错 - Error while running curl command through java 在没有main()的情况下运行Java程序时出现错误 - I am getting error while running Java program without main() java.io.IOException:无法运行程序“ scala”:CreateProcess错误= 2, - java.io.IOException: Cannot run program “scala”: CreateProcess error=2, java.io.IOException: 无法运行程序“...”: java.io.IOException: error=2, No such file or directory - java.io.IOException: Cannot run program “…”: java.io.IOException: error=2, No such file or directory 无法运行程序java.io.IOException - Cannot run program java.io.IOException 我的 java 编译器在命令提示符下处理我的程序,但是当我尝试运行该程序时,它给了我一个错误 - My java compiler works on my program in the command prompt but when i try to run the program it gives me an error 无法在Java中运行cURL命令 - Cannot run the cURL command in java 运行第一个HIPI程序时,出现JSON错误。 我应该在哪里添加它以及如何添加它? - While running the first HIPI program, I am getting error of JSON. Where should I add it and how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM