Hello. I was hoping you could help me out with a small problem I'm having. More points will be added to this question as I get them lol.
In any case, here's the problem:
I'm writing a java program that will act as a shell in windows xp / 2000 (can be adapted for 98 by launching a new instance of command instead of cmd I think. . . not sure though, haven't tried it). In any case, something's wrong with it as it either A) won't accept input or B) won't read the output right, I'm not quite sure how to decide. All this is new to me, I'm used to doing GUI work instead of trying to mess with buffers and the like.
import java.io.*;
public class remote{
static int start=0;
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec(
"cmd.exe")
;
BufferedInputStream is = (BufferedInputStream)p.get
InputStrea
m(); //Typecasting to buffered i/o streams
BufferedOutputStream os = (BufferedOutputStream)p.ge
tOutputStr
eam();
t thre = new t(os); //need to start an input thread so that input may be passed to the program
Thread thr = new Thread(thre);
thr.start();
int[] read = new int[50000];
int i=0;
while(true){
read[i] = is.read(); //read the stream forever
if(read[i]==10){ //test for ascii characters 10 and 13, both preceding a new line, so I assume that they're \r and \n, respectively
System.out.print("\r");
}
else if(read[i]==13){
System.out.print("\n");
}
else{
System.out.print((char)rea
d[i]); //if no special characters, print out the character
}
i++;
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
class t extends Thread{
/*
This thread is designed to accept input and pass it to the program.
*/
BufferedOutputStream os;
public t(BufferedOutputStream bos){
this.os = bos;
}
public void run(){
BufferedReader bufRead = new BufferedReader(new InputStreamReader(System.i
n)); //catching input from the system
String s;
while(true){
try{
s=bufRead.readLine(); //accepting input
os.write(s.getBytes()); //writing it to the buffer in a byte[] format
os.flush(); //passing output from the buffer to the command shell
}
catch(IOException err){
}
}
}
}
Any assistance would be appreciated. Thanks!