RomeScape
November 21, 2017, 07:42:17 pm
Welcome, Guest. Please login or register.

Login with username, password and session length
News: My new email:
Pankers7@gmail.com
email me if u need anything Tongue
 
  Home   Forum   Help Search Arcade Staff List Login Register  

Sending Output to External Process


Pages: [1]   Go Down
  Print  
Author Topic: Sending Output to External Process  (Read 11 times)
Mazead
New-Member


Reputation +0/-0
Offline Offline

Posts: 10


View Profile
« on: October 11, 2014, 06:26:24 pm »

For some reason, I'm having problems sending output to a process that I've created in Java. The external process is running in a command prompt, and the peculiar thing is that I can click that, type, hit enter, and I'll get output from the program.

Anyways, here is the relevant code I'm using that just isn't working...

Quote
try {
    ProcessBuilder builder=new ProcessBuilder(args);
    builder.redirectErrorStream(true);
    final Process p=builder.start();
    // Process has been created and is running
    try {
        String b="";
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        final BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        new Thread(){public void run(){
            // This thread will periodically send "get_time" to the process to get an update on its progress
            while(true)
            {
                try {
                    Thread.sleep(1000);
                    p.exitValue();
                    // p.exitValue() only works when process has ended, so normal code goes in the catch block
                    output.close();
                    break;
                    // Leave the infinite loop if the program has closed
                } catch (IOException ex) {
                    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                    break;
                    // Leave the infinite loop if we tried closing our output stream, but it was already closed
                } catch (InterruptedException ex) {
                    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalThreadStateException e) {
                    try {
                        System.out.println("Outputted: get_time");
                        output.write("get_time" + System.lineSeparator());
                        // Give the process some input
                    } catch (IOException ex) {
                        Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }}.start();
        while((b = input.readLine()) != null){
            System.out.println(new Time(System.currentTimeMillis()).toString() + " " + b);
            // Log all output the process gives
        }
        input.close();
    } catch (IOException ex) {
        Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
    }
    // More code here
} catch (IOException ex) {
    Logger.getLogger(OvMusicUI.class.getName()).log(Level.SEVERE, null, ex);
}


If necessary, I can give an example command and the name of the external program being run so you can try it yourself...

Thanks!
Report Spam   Logged

Social Buttons



Pages: [1]   Go Up
  Print  
 
Jump to:  

Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum | Buy traffic for your forum/website
Powered by SMF | SMF © 2016, Simple Machines