2

I have C code that takes input from the console as,

main() {

 int value;
 printf("Enter a value");
 scanf("%d", &value);
 printf("the value is, %d", value);
 return;
}

now I compile and create an .exe file. (say test.exe)

After that I want to call that executable file from a java code.

so, I used

public class JavaTest {
static String s=null;
public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {

    java.lang.Runtime rt = java.lang.Runtime.getRuntime();


    System.out.println("start");
    java.lang.Process p = rt.exec("test.exe");

    BufferedReader stdInput = new BufferedReader(new 
            InputStreamReader(p.getInputStream()));


    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
    }

    p.waitFor();

    System.out.println("done.");
 }
}

Obviously, that doesn't work. My question is, how do I pass the integer to the process during runtime? I don't want to pass it as command line parameter.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
se7en
  • 87
  • 1
  • 3
  • 16

1 Answers1

0

use the outputStream before you use the inputStream of the Process. Write the integer to the output.Stream

John Smith
  • 2,282
  • 1
  • 14
  • 22
  • do I write this? int a=1; BufferedWriter stdOut = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); stdOut.write(a); – se7en Feb 21 '13 at 12:23