我不确定这是否可能,特别是因为 Java 通过 VM 运行,但我可以从 Java 中调用 gnuplot 吗?也许我可以让 Java 打开一个终端并输入
gnuplot
plot ...
ETC?
使用gnujavaplot。
如果您可以让 gnuplot 从命令行或标准输入(或从文件中读取)获取所有输入并将其输出也写入文件,那么使用ProcessBuilder
.
这适用于 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);
}
使用 JavaGnuplotHybrid 库。
它的重量非常轻(只有 3 个核心类),并且可以使用 Java 和 Gnuplot 进行混合编程。
更多细节:
您可以使用“exec”命令启动任何外部应用程序。
http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html
有关一些示例,请参见此页面。 http://www.rgagnon.com/javadetails/java-0014.html
编辑:我忘记了 ProcessBuilder。Michael Borgwardt 的答案是一个更强大的解决方案。