I am having below error while running curl command using java in Mac where it gives IOException and says cannot run the program

huangapple go评论55阅读模式
英文:

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 = &quot;curl --location --request POST &#39;https://some_url/abc&#39; \\\n&quot; +
					&quot;--header &#39;Api-Key: RIQOWU-9asds943msd9d934nmwdw0d&#39; \\\n&quot; +
					&quot;--header &#39;Content-Type: application/json&#39; \\\n&quot; +
					&quot;--header &#39;BarId: di292sdk/dmasdaskdnasd934n3sda0334n5Yz1QlVnnTmJ39U4aJT8/9sdns/Xwpen/asdksaeibwae+292nsadsaweg3ARyWwAA0LuSDIL3s+A/EVkLoVhc=&#39; \\\n&quot; +
					&quot;--header &#39;Accept: application/json;v=2&#39; \\\n&quot; +
					&quot;--header &#39;Cookie: JSESSIONID=9SDKSDSD9M23B4D46BF663F2B51E75137&#39; \\\n&quot; +
					&quot;--data-raw &#39;{\n&quot; +
					&quot;\&quot;AU\&quot;: \&quot;EC\&quot;,\n&quot; +
					&quot;    \&quot;is\&quot;: \&quot;is\&quot;,\n&quot; +
					&quot;    \&quot;ls\&quot;: \&quot;js\&quot;,\n&quot; +
					&quot;    \&quot;Timestamp\&quot;: \&quot;2021-06-26T18:43:00Z\&quot;\n&quot; +
					&quot;}&#39;&quot;;
			ProcessBuilder process = new ProcessBuilder(payload1);
			Process p;
			p = process.start();

Below is the relevant data from response

java.io.IOException: Cannot run program &quot;/usr/local/Cellar/curl/7.71.1/bin/curl.exe --location --request POST &#39;..........
: 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.&lt;init&gt;(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 = &quot;/usr/local/Cellar/curl/7.71.1/bin/curl.exe &quot;+&quot;--location --request POST......above payload......&quot;

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(&quot;/usr/local/Cellar/curl/7.71.1/bin/curl&quot;); 
 System.out.println(&quot;curl.exists()=&quot;+curl.exists());
 System.out.println(&quot;Files.isExecutable()=&quot;+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(), 
       &quot;--location&quot;, &quot;--request&quot;, &quot;POST&quot;
     , &quot;https://some_url/abc&quot;
     , &quot;--header&quot;, &quot;Api-Key: RIQOWU-9asds943msd9d934nmwdw0d&quot;
     
 ... etc

     , &quot;--data-raw&quot;, &quot;{\n&quot; +
                &quot;\&quot;AU\&quot;: \&quot;EC\&quot;,\n&quot; +
                &quot;    \&quot;is\&quot;: \&quot;is\&quot;,\n&quot; +
                &quot;    \&quot;ls\&quot;: \&quot;js\&quot;,\n&quot; +
                &quot;    \&quot;Timestamp\&quot;: \&quot;2021-06-26T18:43:00Z\&quot;\n&quot; +
                &quot;}&quot;
 };

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):

huangapple
  • 本文由 发表于 2020年7月22日 09:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63025469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定