我不确定这是否可能,特别是因为Java通过虚拟机运行,但我能否从Java中调用gnuplot?也许我可以让Java打开终端并输入命令。
gnuplot
plot ...
等等?
使用gnujavaplot。
ProcessBuilder
就没有问题。使用JavaGnuplotHybrid库。
它非常轻量级(仅有3个核心类),并且可以实现Java和Gnuplot的混合编程。
更多详情请查看:
这在 Debian 上可行:
String[] s = {"/usr/bin/gnuplot",
"-e",
"set term jpeg large size 800,600;set autoscale; set grid;set format y \"%0.f\";set output \"plot.jpg\";set xdata time;set timefmt \"%Y-%m-%d-%H:%M:%S\";set xlabel \"Dates\";set ylabel \"Data transferred (bytes)\";plot \""+x+"\" using 1:2 title \"Total:"+tot+"\" with linespoints;"
};
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(s);
InputStream stdin = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.err.println("gnuplot:"+line);
int exitVal = proc.waitFor();
if (exitVal != 0)
log("gnuplot Process exitValue: " + exitVal);
proc.getInputStream().close();
proc.getOutputStream().close();
proc.getErrorStream().close();
} catch (Exception e) {
System.err.println("Fail: " + e);
}
你可以使用“exec”命令启动任何外部应用程序。
http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html
请参考此页面以获取一些示例。 http://www.rgagnon.com/javadetails/java-0014.html
编辑:我忘记了ProcessBuilder。Michael Borgwardt的答案是更健壮的解决方案。