Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JNI Alternative?

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JNI Alternative?

    I've got a problem that I *know* jni will solve, but I'm wondering if there's any alternatives.

    I have a simple program, let's call it foo.exe

    When you run foo.exe it asks for your name, and then plays a nice text move based game of chess by just spitting out the name of where it plays its move (i.e. 1. e2-e4 ...then it waits for you to move).

    Input exists only through strings only, and all possible output from the foo.exe is known and are also strings.

    Is it possible to avoid any kind of serious IPC setup? Maybe have java launch the program and then output messages to stdout and read from stdin? Or is that just not going to happen without me either setting up IPC via a named pipe or use JNI?

    Best!
    Janus


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: JNI Alternative?

    May not be pretty, but you can do this by launching the program via Runtime.exec() and retrieving the Process

    Process proc = Runtime.exec("Your Program Here");
    Once you have the Process reference, you can retrieve the InputStream, OutputSream, and ErrorStream to read and write. Reading and writing from the Process should (ideally) be performed in separate threads. One problem I've noticed in doing it this way that I haven't researched thoroughly is when a program receives an error from the ErrorStream (possibly through stderr from the exe), the connection becomes severed.

  3. The Following User Says Thank You to copeg For This Useful Post:

    janusmccarthy (November 14th, 2009)