Calling gnuplot from java? (ubuntu)

前端 未结 5 1167
余生分开走
余生分开走 2021-01-19 02:52

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

5条回答
  •  清酒与你
    2021-01-19 03:10

    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);
    }
    

提交回复
热议问题