我试图通过java运行以下curl命令。这从命令行正常运行,但是当我使用java运行它时抛出错误
curl -XPOST -H'Content-type: application/json' "http://localhost:8084/druid/v2/?pretty" -d'{"queryType":"timeBoundary","dataSource":"wikipedia"}'
这是使用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
答案 0 :(得分:3)
您需要将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的文件夹位于PATH
中PS。当然它适用于Windows,因为* NIX只是“卷曲”