Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

gcc

cygwin

I am trying to compile a C file from Java by calling Cygwin's gcc or gcc-4, but nothing I try seems to work. What I am trying is 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.

like image 731
Mike Avatar asked Mar 18 '26 18:03

Mike


1 Answers

I would need to see more about what you are doing with theProcess after this statement to understand this fully. But simply calling "exec" will not output anything to the Std output, if that is what you are expecting. In some cases, commands will not execute at all if their output is not consumed. That being said, you will need to read the output from the Process object that was created. 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 print the output from the process' standard output to the JVM's standard output.

like image 182
rmlan Avatar answered Mar 21 '26 09:03

rmlan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!