DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Can Anyone Help Please I Really Don't Find Solution For Long Time
my problem is that i can't receive the result of command, how to run external .exe in java, the problem run without error but i can't print the result of commandes
//-------the main -----------------------
public class test {
public static LanceMoteur l;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
l=new LanceMoteur("version");
l.lancerMoteur();
}
}
//----------------class run the .exe ---------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class LanceMoteur {
private static InputStream in;
private static OutputStream out;
private String comd;
public LanceMoteur(String cmd){
comd=cmd;
}
public void lancerMoteur(){
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("/home/syphax/Bureau/VoronoiMoteur "+comd);
in=pr.getInputStream();
out=pr.getOutputStream();
//new ThreadSendMoteur(out).start();
//new ThreadReceiveMoteur(in).start();
int exitVal = pr.waitFor();
BufferedReader buf=new BufferedReader(new InputStreamReader(pr.getErrorStream()));
String line="";
while((line=buf.readLine())!=null){
System.out.println(line);
}
System.out.println("ExitValue: " + exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
//---------------------------------------------------------- thread send commande ---------------------
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class ThreadSendMoteur extends Thread{
private OutputStream outstream;
static ObjectOutputStream bout;
public ThreadSendMoteur(OutputStream out){
outstream=out;
}
public void run() {
try {
sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
//bout=new ObjectOutputStream(outstream);
OutputStreamWriter ow = new OutputStreamWriter(outstream) ;
BufferedWriter wr = new BufferedWriter(ow) ;
wr.write("version");
wr.flush();
System.out.println("envoi de la commande");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//--------------------------thread receiv result -----------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ThreadReceiveMoteur extends Thread {
InputStream instream;
public ThreadReceiveMoteur(InputStream in){
instream=in;
}
public void run() {
while(true){
try {
sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader input = new BufferedReader(new InputStreamReader(instream));
try {
new BufferedWriter(new FileWriter("/home/syphax/Bureau/out.txt")).write(input.readLine());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
while(input != null)
{
String _ch = input.readLine();
if(_ch != null){
System.out.print((String)_ch);
System.out.println("send thread22222223333");
}else break;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
} }





