Results 1 to 5 of 5

Thread: Java inperprocess communication

  1. #1
    Join Date
    Nov 2007
    Beans
    108

    Java inperprocess communication

    I'm using Runtine.getRuntime().exec(command); to launch another process, and I wanna communicate with it via the input and output streams.
    When the process is something like "ls", it is very simple - just keep reading from process.getInputStream() until the end of file...

    However, when the new process communicates back, I start running in trouble...
    So, basically, I need some tutorials that deal with this issue (i.e. two-way communication), and I haven't been able to find anything useful so far
    Any links?

    P.S. As an example, I'm using the crafty chess engine. It accepts input in plain text and outputs also in plain text... However, my input (I use a PrintWriter obtained from processes' getOutputStream()) doesn't seem to be getting to the destination

  2. #2
    Join Date
    Feb 2007
    Location
    Heaven
    Beans
    486
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Java inperprocess communication

    Well I'm too lazy to try out and do what you're doing to find out what's the matter.. as a side note, since you didn't post the source code I can only guess, perhaps you are using multiple println() and since (prolly) the target app reads till the first \n character it might eventually stop reading after the first call of println and since the info is crippled it shows no sign of having gotten any command from you.
    Do a test and see what you actually _print_ to that app, and then send that stuff manually (from the console) and see whether that app now works, draw conclusions from the results.

  3. #3
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Java inperprocess communication

    Yes, do as xlinuks suggests. The cat command is perfect for such testing. If you run cat with no arguments, it will simply output to stdout each line you input to stdin, untill eof. Flushing the output-stream might help as well.

  4. #4
    Join Date
    Nov 2007
    Beans
    108

    Re: Java inperprocess communication

    Well I'm too lazy to try out and do what you're doing to find out what's the matter..
    I know, that's why I merely need a working example, from which I can build my own.

    I get a feeling I'm missing something really silly and obvious, though it isn't the miltiple printlns.

    P.S. I'm gonna go try it with cat, good idea.

  5. #5
    Join Date
    Nov 2007
    Beans
    108

    Re: Java inperprocess communication

    Ok, here's what I have:
    PHP Code:
            try {proc Runtime.getRuntime().exec("cat");} catch (IOException e) {}
            
    in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            
    out = new PrintWriter(new OutputStreamWriter(proc.getOutputStream()));
            try {
                
                
    out.write("hello");
                
                
    System.out.println(">> ");
                
    String s in.readLine();
                
    System.out.println(">> " s);
            } catch (
    Exception e) {e.printStackTrace();} 
    The output of above is ">>".
    In other works, it just hangs (doesn't crash) at in.readLine()
    I tried separating the reading and writing in separate threads but to the same effect.

    P.S. ooookay, wait a sec, I just flushed the output stream and it seems to work... I'll do some more testing.
    P.S. Yep, it works
    Basically, the solution was to use println instead of write (or add "\n" to the string), and flush the stream right away.
    Makes me feel so smart hehe.
    Last edited by Phristen; June 28th, 2008 at 09:53 PM.

Tags for this Thread

Bookmarks

Posting Permissions

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