0
我写一个代码执行命令和读取输出 如果我在命令提示符下运行命令,它看起来像这样如何从Java代码运行命令并读取输出?
命令是
echo 'excellent. awesome' | java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin
命令产生多行输出。我怎样才能打印这个输出在我的java代码?
我已经写了下面的代码,但它产生的输出命令本身就是
echo 'excellent. awesome' | java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin
,而不是实际的命令输出,我们可以看到它的屏幕截图
final String cmd = "java -cp \"*\" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin";
final String path = "C:/Project/stanford-corenlp-full-2015-01-29/stanford-corenlp-full-2015-01-29";
String input = "excellent";
String cmdString = "echo '" +input + "' | " + cmd;
Process process = Runtime.getRuntime().exec(cmdString,null, new File(path));
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
能否详细说明 - *它产生输出为命令本身而不是实际的命令输出*? – TheLostMind 2015-04-02 05:56:04
我认为你可能会犯错。第一个参数只是命令名,另一个是包含命令参数的字符串数组。 – Clashsoft 2015-04-02 06:16:56
您可以通过编程方式访问'SentimentModel',这比单独的流程中的管道要容易得多。 – 2015-04-02 16:07:18