java 从java调用gnuplot?(Ubuntu)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3196563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling gnuplot from java? (ubuntu)
提问by rhombidodecahedron
I'm not sure if this is possible, especially since Java runs through a VM, but can I call gnuplot from within Java? Perhaps I could have Java open a terminal and input
我不确定这是否可行,尤其是因为 Java 通过 VM 运行,但是我可以从 Java 内部调用 gnuplot 吗?也许我可以让 Java 打开一个终端并输入
gnuplot
plot ...
etc?
等等?
回答by mcandre
Use gnujavaplot.
使用gnujavaplot。
回答by Michael Borgwardt
If you can get gnuplot to take all input from the command line or standard input (or read it from a file) and write its output to files as well, then there should be no problem doing this using ProcessBuilder.
如果您可以让 gnuplot 从命令行或标准输入获取所有输入(或从文件中读取它)并将其输出也写入文件,那么使用ProcessBuilder.
回答by Yohann Sulaiman
This works on Debian:
这适用于 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);
}
回答by Changwang Zhang
Use the JavaGnuplotHybrid library.
使用 JavaGnuplotHybrid 库。
It is very light weight (only 3 core classes) and enables hybrid programming with Java and Gnuplot.
它非常轻量级(只有 3 个核心类),并且支持使用 Java 和 Gnuplot 进行混合编程。
- Hybrid programming with Java and Gnuplot
- Very light weight (just three core Classes)
- Use tags in Gnuplot code to execute functions or get fields' values in Java.
- Support both synchronized and asynchronized running of Gnuplot in Java. (synchronized: your java program will wait until you close the popped Gnuplot window; asynchronized: you java program will not wait.)
- Capture error/normal text output of Gnuplot to the java terminal
- Read Gnuplot code from xml files
- Support Gnuplot code template.
- 使用 Java 和 Gnuplot 进行混合编程
- 重量很轻(只有三个核心类)
- 在 Gnuplot 代码中使用标签来执行函数或在 Java 中获取字段的值。
- 支持 Java 中 Gnuplot 的同步和异步运行。(同步:您的 Java 程序将等到您关闭弹出的 Gnuplot 窗口;异步:您的 Java 程序将不会等待。)
- 将 Gnuplot 的错误/正常文本输出捕获到 java 终端
- 从 xml 文件中读取 Gnuplot 代码
- 支持 Gnuplot 代码模板。
For more details:
更多细节:
回答by Tansir1
You can launch any external application using the "exec" commands.
您可以使用“exec”命令启动任何外部应用程序。
http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html
http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html
See this page for a few examples. http://www.rgagnon.com/javadetails/java-0014.html
有关一些示例,请参阅此页面。 http://www.rgagnon.com/javadetails/java-0014.html
EDIT: I forgot about ProcessBuilder. Michael Borgwardt's answer is a more robust solution.
编辑:我忘记了 ProcessBuilder。Michael Borgwardt 的答案是一个更强大的解决方案。

