How can I call the Cygwin C GCC compiler with Java?

I'm trying to compile a C file from Java by calling Cygwin gcc or gcc-4, but nothing I'm trying seems to work. I am trying to use the following line of code:

theProcess = Runtime.getRuntime().exec("cmd /c C:/cygwin/bin/gcc-4.exe -o C:/work/source.exe C:/work/source.c");

      

However, it did not output anything.

+3


source to share


1 answer


I will need to know more about what you are doing with theProcess

after this statement to fully understand this. But simply calling "exec" does not output anything to the Std output if that is what you expect. In some cases, commands won't run at all unless their output is consumed. In doing so, you will need to read the result from the created object Process

. Try something like this:

BufferedReader br = new BufferedReader (new InputStreamReader (theProcess.getInputStream());
String line = br.readLine();
while (line != null) {
    System.out.println(line);
    line = br.readLine();
}

      



This will output the output from the process's standard output to the JVM's standard output.

+1


source







All Articles