我正在尝试运行一个使用我正在运行的Web服务的curl命令:
$(document).ready(function() {
$(".Left_Container").scrollTop(0); // go to the top instantly (no animation)
$(".Left_Container").animate({ // then animate down
scrollTop: $(".Home_Left").offset().top
}, 0);
});
如果我从Sysout复制日志消息并将其粘贴到我的终端上,它会按预期运行,但是当我运行java代码时,它似乎从代理返回一个没有找到服务的html页面。
我是否必须添加其他内容才能从java运行?
答案 0 :(得分:1)
问题是你没有读取输出。我修改了你的代码,所以它应该工作。我不是百分百肯定因为我出于某种原因无法正确测试它。
编辑 - 它工作,我只是执行错误的命令!这应该适合你。
for (auto it = testV.begin(); it != testV.end(); )
advance(it, step);
编辑 - 使用String curl = "curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{\"field1\": \"value1\", \"field2\": \"value2\"}' 'http://localhost:8080/service'";
System.out.println(curl);
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curl);
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); //BufferedReader to read the output
StringBuilder sb = new StringBuilder(); //What will hold the entire console output
String line = ""; //What will hold the text for a line of the output
while ((line = reader.readLine()) != null) { //While there is still text to be read, read it
sb.append(line + "\n"); //Append the line to the StringBuilder
}
System.out.println(sb); //Print out the full output
} catch (Exception e) {
e.printStackTrace();
}
代替ProcessBuilder
Runtime