Need urgent help. Calling of exe files from java program
This program can execute small .exe files that donot take inputs but doesn't work for exe files that takes input. what could be the problem.
Server code :-
import java.io.*;
import java.net.*;
public class Server1 {
private Player[] players;
private ServerSocket server;
private ExecHelper exh;
String command = null;
String message = null;
public Server1() {
players = new Player[5];
try {
server = new ServerSocket( 12345, 5 );
System.out.println("Server started...");
System.out.println("Waiting for request...");
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
} //end Server constructor
public void execute() {
for( int i = 0; i < players.length; i++ )
try {
players = new Player( server.accept() );
players.start();
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
}
public static void main( String args[] ) {
Server1 ser = new Server1();
ser.execute();
System.exit( 1 );
}
private class Player extends Thread {
private Socket connection;
private ObjectOutputStream output;
private ObjectInputStream input;
public Player( Socket socket ) {
connection = socket;
try {
input = new ObjectInputStream( connection.getInputStream());
output = new ObjectOutputStream( connection.getOutputStream());
output.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
System.exit( 1 );
}
}
public void run(){
try {
message = "Enter a command:";
output.writeObject( message );
output.flush();
do {
command = ( String ) input.readObject();
String osName = System.getProperty( "os.name" );
String[] cmd = new String[3];
if( osName.equals( "Windows 2000" )) {
cmd[0] = "cmd.exe";
cmd[1] = "/c";
cmd[2] = command;
}
else if( osName.equals( "Windows NT" ) ) {
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = command ;
}
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec( cmd );
exh = new ExecHelper( proc, output, input);
} while( !command.equals( "TERMINATE" ) );
}
catch( Throwable t ) {
t.printStackTrace();
}
}
} //end class Player
public class ExecHelper implements Runnable {
private Process process;
private InputStream pErrorStream;
private InputStream pInputStream;
private OutputStream pOutputStream;
private InputStreamReader isr;
private InputStreamReader esr;
private PrintWriter outputWriter;
private ObjectOutputStream out;
private ObjectInputStream in;
private BufferedReader inBuffer;
private BufferedReader errBuffer;
private Thread processThread;
private Thread inReadThread;
private Thread errReadThread;
private Thread outWriteThread;
public ExecHelper( Process p, ObjectOutputStream output, ObjectInputStream input ) {
process = p;
pErrorStream = process.getErrorStream();
pInputStream = process.getInputStream();
pOutputStream = process.getOutputStream();
outputWriter = new PrintWriter( pOutputStream, true );
in = input;
out = output;
processThread = new Thread( this );
inReadThread = new Thread( this );
errReadThread = new Thread( this );
outWriteThread = new Thread( this );
processThread.start();
inReadThread.start();
errReadThread.start();
outWriteThread.start();
}
public void processEnded( int exitValue ) {
try {
Thread.sleep( 1000 );
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
public void processNewInput( String input ) {
try {
out.writeObject( "\n" + input );
out.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void processNewError( String error ) {
try {
out.writeObject( "\n" + error );
out.flush();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public void println( String output ) {
outputWriter.println( output + "\n" );
}
public void run() {
if( processThread == Thread.currentThread()) {
try {
processEnded( process.waitFor());
}
catch( InterruptedException ie ) {
ie.printStackTrace();
}
}
else if( inReadThread == Thread.currentThread() ) {
try {
isr = new InputStreamReader( pInputStream );
inBuffer = new BufferedReader( isr );
String line = null;
while(( line = inBuffer.readLine()) != null ) {
processNewInput( line );
}
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
else if( outWriteThread == Thread.currentThread() ) {
try {
String nline = null;
nline = ( String ) in.readObject();
println( nline );
}
catch( ClassNotFoundException cnfe ) {
//cnfe.printStackTrace();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
else if( errReadThread == Thread.currentThread() ) {
try {
esr = new InputStreamReader( pErrorStream );
errBuffer = new BufferedReader( esr );
String nline = null;
while(( nline = errBuffer.readLine()) != null ) {
processNewError( nline );
}
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
}
}
}
Client code :-
// client.java
import java.io.*;
import java.net.*;
public class Client {
private ObjectOutputStream output;
private ObjectInputStream input;
private String chatServer;
private String message = "";
private Socket client;
public Client( String host ) {
chatServer = host;
}
private void runClient() {
try {
connectToServer();
getStreams();
processConnection();
}
catch( EOFException eofe ) {
System.err.println( "Client terminated connection ");
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
finally {
closeConnection();
}
} //end method runClient
private void connectToServer() throws IOException {
System.out.println( "Attempting connection...\n");
client = new Socket( InetAddress.getByName( chatServer ), 12345);
System.out.println( "Connected to : "+ client.getInetAddress().getHostName());
}
private void getStreams() throws IOException {
output = new ObjectOutputStream( client.getOutputStream());
output.flush();
input = new ObjectInputStream( client.getInputStream());
}
private void processConnection() throws IOException {
while( true ){
try {
message = ( String ) input.readObject();
System.out.print( message );
InputStreamReader isr = new InputStreamReader( System.in);
BufferedReader br = new BufferedReader( isr );
String line = null ;
line = br.readLine();
output.writeObject(line);
output.flush();
}
catch( ClassNotFoundException cnfe) {
System.out.println( "\nUnknown object type received");
}
}
} //end processConnection
private void closeConnection() {
System.out.println( "\nClosing connection");
try {
output.close();
input.close();
client.close();
}
catch( IOException ioe ) {
ioe.printStackTrace();
}
}
public static void main( String args[] ) {
Client c;
if( args.length == 0 )
c = new Client( "127.0.0.1" );
else
c = new Client( args[0] );
c.runClient();
}
}

