jerhynsoen
jerhynsoen

Reputation: 215

run program in java read what it is doing

I want to run a program like a video game in java and have it output everything it does in java, is this possible? To clarify on everything, I want to see the output of the game in terms of what it is doing. I understand it may not come out completely comprehensible since it would be in a different program language. Anyone here remember using a gameshark/gamegenie to hack a game for codes? That is kind of what I want to do.

I tried runtime and processbuilder so far, but it out puts nothing.

Below is my code:

public class ProcessW {
     public static void main(String[] args) throws IOException {
         String command = "...my game...";

         ProcessBuilder pb = new ProcessBuilder(command); 
         pb.redirectErrorStream(true); 
         Process proc = pb.start(); 

         java.io.InputStream is = proc.getInputStream(); 
         InputStreamReader isr = new InputStreamReader(is); 
         BufferedReader br = new BufferedReader(isr); 

         String line; 
         int exit = -1; 

         while ((line = br.readLine()) != null) { 
             // Outputs your process execution 
             System.out.println(line); 
             try { 
                 exit = proc.exitValue(); 
                 if (exit == 0)  { 
                     // WinProcess finished 
                 } 
             } catch (IllegalThreadStateException t) { 
                 // The process has not yet finished.  
                 // Should we stop it? 
                 //if (processMustStop()) 
                     // processMustStop can return true  
                     // after time out, for example. 
                     proc.destroy(); 
             } 
         } 
     }
}

Upvotes: 0

Views: 169

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328840

If the game prints something on the console when you start it without Java, then your problem is buffering. Java will see the output of the game only after 4096 bytes of output have been written.

The only way to prevent this is to change the code of the game to call stdout.flush() (or similar) after each line to force the OS to send the output to the Java process.

Upvotes: 1

Related Questions