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
|
package RemoteC;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.rmi.RemoteException;
public class Rc {
private String cmd;
private Process process = null;
private String reponse=null;
private InputStream p_out;
private OutputStream p_in;
public InputStreamReader p_outReader;
private BufferedWriter p_inWriter;
public Rc(String commande)
{
cmd=commande;
}
public Rc()
{
}
public boolean envoieCommande()
{
try {
process = Runtime.getRuntime().exec("cmd.exe /k"+" "+cmd);
init();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (process == null)
{
// on envoie une réponse négative System.out.println("Process \"" + CmdeLine + "\" inconnu.");
return false;
}
// on envoie une réponse positive System.out.println("Process \"" + CmdeLine + "\" en cours d'exécution.");
return true;
}
public String recoisReponse()
{
String a;
try {
StringBuffer buffer = new StringBuffer();
//BufferedReader reader = new BufferedReader (new InputStreamReader(process.getInputStream()));
InputStreamReader reader=new InputStreamReader(process.getInputStream());
init();
try {
// String line = new String();
//char [] line = new char[1];
int line=0;
while((a=GetStdOutData ())!=null)
{
}
reponse = a;
}
finally
{
p_outReader.close();
}
// reponse = buffer.toString();
} catch(IOException e) {
e.printStackTrace();
}
return reponse;
}
public String GetStdOutData () throws RemoteException
{
char buffer[] = new char[2048];
int len = 0;
try
{
len = p_outReader.read(buffer, 0, 2048);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
return new String (buffer, 0, len);
}
public void init()
{
InputStream p_out = process.getInputStream();
OutputStream p_in = process.getOutputStream();
p_outReader = new InputStreamReader(p_out);
p_inWriter = new BufferedWriter(new OutputStreamWriter(p_in));
}
} |