Appel de gnuplot via Java
Bonjour à tous,
Je suis débutant en java, et en ce moment j'ai un soucis pour envoyer une commande à un exécutable qui s'appelle gnuplot (cet outil permet de tracer des graphes).
J'ai trouvé sur le net une classe permettant une communication entre Java et gnuplot. En exécutant la classe, l'outil se lance bien mais malheureusement les commandes ne sont pas exécutées (il doit tracer la fonction sin(x))
J'ai passé du temps pour comprendre mais en vain. Merci beaucoup pour votre aide :(
Voila les deux classes:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| package essaisGnup;
//////////////////////////////////////////////////////////////////////////
// //
// Interface Java with Gnuplot //
// //
// Author: Nicolas Pouvesle (nicolas@pouvesle.fr) //
// Created: December 5, 2007 //
// Free class released under the GNU General Public License //
// //
// Please feel free to send me any comments about this class //
// //
//////////////////////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.lang.InterruptedException;
/**
* <p align=justify> Communication with Gnuplot.
*/
public class jgnuplot {
Process p = null;
BufferedReader stdInput = null;
BufferedWriter stdOutput = null;
BufferedReader stdError = null;
/**
* Default Constructor
*/
public jgnuplot() {
String s = null;
try {
// run gnuplot through a process
p = Runtime.getRuntime().exec("C://Users//louarradi//Desktop//gnuplot//bin//wgnuplot_pipes");
// Get the Process standart input
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
// Get the Process standart output
stdOutput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// Get the Process standart output error
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
} catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
/**
* Send command to Gnuplot
*
* @param cmd Gnuplot shell command.
*/
public void cmd(String cmd) {
try {
stdOutput.write(cmd + "\n");
stdOutput.flush();
} catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
/**
* Get Gnuplot current outputstream line.
*/
public String getLine() {
String aws = null;
try {
//if (stdError.ready()) {
aws = stdError.readLine();
//}
} catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
return aws;
}
/**
* Get next no empty outputstream line.
*/
public String getNoEmptyLine() {
String aws = getLine();
if (aws != null) {
if (aws.equals("")) {
aws = getNoEmptyLine();
}
}
return aws;
}
/**
* Get Gnuplot variable.
*
* @param var Gnuplot variable name.
*/
public String getVar(String var) {
// Ask for the variable
cmd("if (defined(" + var + ")) print " + var + "; else print \"None\"");
// Get the answer
String aws = getNoEmptyLine();
// Test if the variable was valid
if (aws.equals("None")) aws = null;
return aws;
}
} |
la classe de test est :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package essaisGnup;
import java.io.PrintWriter;
//////////////////////////////////////////////////////////////////////////
// //
// How to use Gnuplot in a Java program ? //
// //
// Author: Nicolas Pouvesle (nicolas@pouvesle.fr) //
// Created: November 30, 2007 //
// Updated: December 5, 2007
// License: GPL //
// //
// Please feel free to send me any comments about this //
// script //
// //
//////////////////////////////////////////////////////////////////////////
public class gpReadMouseTest {
public static void main(String args[]) throws InterruptedException {
jgnuplot gp = new jgnuplot();
System.out.println("toto");
//PrintWriter pw = new PrintWriter(gp.getOutputStream());
gp.cmd("set mouse");
System.out.println("toto1");
gp.cmd("plot sin(x)");
System.out.println("toto2");
// Do the Job 3 times
for(int i=0;i<3;i++) {
System.out.println("Click somewhere in the graph:");
gp.cmd("pause mouse");
String x = gp.getVar("MOUSE_X");
String y = gp.getVar("MOUSE_Y");
System.out.println("Clicked mouse coordinates (x,y)=(" + x + "," + y + ")");
}
}
} |