简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM