Reputation: 463
I have a C executable which I can run it from CYGWIN. I also want to run same file from JAVA. The C program gets input and output via stdin and stdout. It mainly gets string and outputs string.
I think I can start the program with process builder successfully. However I can not interact with the C program. To start .exe I use ProcessBuilder, see following.
Process cmd = new ProcessBuilder("path to exe").start();
The main method of my C program is here:
int main(argc, argv)
{
/* set command line or config file parms */
config(argc, argv);
/* read grammar, initialize parser, malloc space, etc */
init_parse(dir, dict_file, grammar_file, frames_file, priority_file);
/* for each utterance */
while( fgets(line, LINE_LEN-1, fp) ) {
/* assign word strings to slots in frames */
parse(line, gram);
/* print parses to buffer */
for(i= 0; i < num_parses; i++ )
print_parse(i, out_ptr, extract, gram);
/* clear parser temps */
reset(num_nets);
}
}
My goal is to send input and get output from Java.
Upvotes: 0
Views: 442
Reputation: 194
If you only need stdinput/output then you can get the appropriate streams using a ProcessBuilder or some form of System.exec quite easily.
After that just generate output and parse input but beware. The input and output streams generally should be processed in different threads. Otherwise it is very easy to get a deadlock, since most programs won't expect stdin and stdout to be tied to a single process (e.g. the stdout fills your input buffer while you are still trying to write to the stdin stream. Your write is blocked waiting for the program to read more and it won't since its write is blocked waiting for you to read more. Classic.)
Be careful with threads but have fun!
Upvotes: 1
Reputation: 1159
Other good library allow easy acces to native file is JNA
. Maybe Runtime
class might help you.
Upvotes: 0
Reputation: 31161
You need to start reading about JNI before going any further. Google is your friend here.
Frankly, your main C method is short. Why don't you want to write this in Java again?
Upvotes: 0