gpt4 book ai didi

java - 通过java运行curl命令时出错

转载 作者:行者123 更新时间:2023-12-01 21:54:33 30 4
gpt4 key购买 nike

我正在尝试通过java运行以下curl命令。这可以从命令行正常运行,但当我使用java运行它时抛出错误

curl -XPOST -H'内容类型:application/json' "http://localhost:8084/druid/v2/?pretty"-d'{"queryType":"timeBoundary","dataSource ":"维基百科"}'

这是使用processbuilder的java代码:

ProcessBuilder pb1 = new ProcessBuilder(
"-XPOST",
"-H", "Content-Type:application/json",
"http://localhost:8084/druid/v2/?pretty",
"-d", "{\"queryType\":\"timeBoundary\",\"dataSource\":\"wikipedia\"}"

);

Process shell = pb1.start();

这是我遇到的错误。

Exception in thread "main" java.io.IOException: Cannot run program "-XPOST": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at com.druid.DruidConnection.main(DruidConnection.java:69)

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:187)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 1 more

最佳答案

您需要添加curl.exe作为第一个参数:

ProcessBuilder pb1 = new ProcessBuilder(
"curl.exe",
"-XPOST",
"-H", "Content-Type:application/json",
"http://localhost:8084/druid/v2/?pretty",
"-d", "{\"queryType\":\"timeBoundary\",\"dataSource\":\"wikipedia\"}"

);

此外,您还需要确保包含curl.exe 的文件夹位于您的路径中

PS。当然是针对Windows的,对于*NIX只是“curl”

关于java - 通过java运行curl命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34586146/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com