java - Run execute file in Runtime.exec() and get user input -
how run c execute file user input in java runtime().exec(). without user input in c program execute file runs in runtime().exec():
for example: below c program if n predefined , it's execute file runs in java runtime()...if n user input how run execute file?
#include <stdio.h> #include <time.h> #include <unistd.h> int main() { setbuf(stdout, null); //set buffer null int i,n; scanf("%d",&n); for(i=0;i<=n;i++) { printf("%d\n",i); sleep(1); //delay } } //main java code ....
import java.io.*; import java.lang.runtime; import java.lang.*; import java.io.file; import java.util.timer; import java.util.timertask; public class run3 { public static void main(string[] args) { try { string[] cmd = {"file path"}; process p = java.lang.runtime.getruntime().exec(cmd); string s; bufferedreader stdinput = new bufferedreader(new inputstreamreader(p.getinputstream())); bufferedreader stderror = new bufferedreader(new inputstreamreader(p.geterrorstream())); while ((s = stdinput.readline()) != null) { system.out.println(s); } system.out.println("done."); }//try catch (ioexception ex) { ex.printstacktrace(); } } //void } //main
there various ways this, depending on trying achieve:
the simplest way pass
ncommand line argument.you have c program read
nstandard input ... , write java side usingprocessobject'sgetoutputstreammethod.you have c program open "/dev/console" or "/dev/tty" , read
nthere. on windows believe can in other ways.
ok. given that, need call p.getinputstream(), write input stream, , close ... before attempting read command's output.
Comments
Post a Comment