Simple JNI task

Hi there,

I have to write a native executable, that first starts my java program and then continously transfers it all input recived from stdin and writes output from the Java program to stdout. Can anyone please show me how to do this (platform-independently) with VisualC++ or gcc?

Ideally there would be two methods in the Java code, one for writing a String and one to accept a String. I’m a complete Newbie in C/C++ and JNI. Are there any pitfalls?

Thanks a lot!

stdin and stdout can be read from and written to using System.in and System.out so I wouldn’t bother trying to do this in native code.
To start your java program from native code you need to use the jni invocation api. http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/invocation.html
There are also many tools that can generate this kind of wrapper executable for you (first google result: JSmooth).

[quote]stdin and stdout can be read from and written to using System.in and System.out so I wouldn’t bother trying to do this in native code.
[/quote]
The executable file will be called from an external program, that will communicate with it through stdin/out, so I haven’t the option to use System.out/in! To be specific, I’m going to write a UCI (universal chess interface) for my chess engine.

To write to stdout in C++ one uses “cout >> …” isn’t it? To read from it “cin”, but when do I know when new data is available? How to convert to a Java string?

For a C++/JNI expert this should be piece of cake to answer. Please post some code. Thanks!

UCI ruler.

For GUI writen in Java.

Runtime.exec(“aplication communicating with uci.exe”);
UCI reads from “in” stream. Warning there could be buffered stream. Look into the API at RUNTIME and PROCESS.
UCI writes commands to out.

No JNI.

For Java chess aplication, C something GUI

system(“java yourApplicaiton”);
UCI reads from in
UCI writes to out

Lots of code.

For Java - Java code

Full compilant UCI

Runtime.exec(“java Aplication”);
read from in Warning remove buffering.
write to out

Dual UCI/application

Call aplication.
Activate interthreadCommunication.

wait it was halfsript.

You get the Idea, however.

You should also post link with UCI specification. You should a little look around Java API as well.

Couldnt you just do this with Unix pipes??

Why do you need to write code?