5

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

gnuplot
plot ...

etc?

5 Answers 5

20

Use gnujavaplot.

Sign up to request clarification or add additional context in comments.

4 Comments

I just LOLed when I saw this answer. :)
@kevincline SourceForge says "Last Update: 3 hours ago." sourceforge.net/projects/gnujavaplot
@mcandre: I am repeating my correspondence with the author
The latest release was on the 15th of January, after quite a gap, so I guess it's active again?
3

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.

1 Comment

I did something similar a while back. I'm hazy on the details, but I can confirm Gnuplot is happy to slurp all of its input from files. It's very scriptable.
1

This works on 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);
}

Comments

1

Use the JavaGnuplotHybrid library.

It is very light weight (only 3 core classes) and enables hybrid programming with Java and Gnuplot.

  1. Hybrid programming with Java and Gnuplot
  2. Very light weight (just three core Classes)
  3. Use tags in Gnuplot code to execute functions or get fields' values in Java.
  4. 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.)
  5. Capture error/normal text output of Gnuplot to the java terminal
  6. Read Gnuplot code from xml files
  7. Support Gnuplot code template.

For more details:

  1. Project page: https://github.com/mleoking/JavaGnuplotHybrid
  2. Examples: https://github.com/mleoking/JavaGnuplotHybrid/blob/master/javagnuplothybrid/doc/examples.md

1 Comment

Does it have a maven repository? I will not to put a jar to my project.
0

You can launch any external application using the "exec" commands.

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

EDIT: I forgot about ProcessBuilder. Michael Borgwardt's answer is a more robust solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.