Results 1 to 1 of 1
  1. #1
    Pluto is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Sending Output to External Process

    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...

    Java Code:
    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!

    EDIT: I think I'm gonna post this in another Forum because I'm practically ready to deploy my app, I'm just waiting on solving this issue and testing it for Macs.
    Last edited by Pluto; 07-26-2013 at 06:11 PM. Reason: See last line.

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2012, 12:39 PM
  2. Open external file with external program
    By Wietse de Vries in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 05:52 PM
  3. Replies: 0
    Last Post: 11-07-2011, 10:48 AM
  4. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM
  5. Sending output to a Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 10:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •