Click here to Skip to main content
12,699,214 members (24,995 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Eclipse GCC
I am developing a Java program in Eclipse. I need to compile a input C file in this Java program. So I used the below code.
Process process = Runtime.getRuntime().exec(new String[]{
     "/usr/bin/gcc /cygdrive/d/MyFile.c -o /cygdrive/d/MyExe"});

I have installed Cygwin as well. But I could not create MyExe.exe from executing above code.

Inserting "-c" next to gcc command successfully creates object file (MyExe.o). Also when I run this program outside Eclipse (using command prompt) it creates .exe correctly.

Can somebody tell me what is the wrong with this code?
Posted 3-Jul-12 20:04pm
Updated 3-Jul-12 20:25pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I'm pretty sure it's because you are trying to mix two ways of passing the arguments to exec().
Try this:
Process process = Runtime.getRuntime().exec(new String[]{
"-c /usr/bin/gcc",
"-o /cygdrive/d/MyExe"});
Process process = Runtime.getRuntime().exec(
"C:/cygwin/bin/bash -c /usr/bin/gcc /cygdrive/d/MyFile.c -o /cygdrive/d/MyExe");

NewMember2000 4-Jul-12 1:50am
thank you for the solution. But the above 2 methods also does not creates .exe

1 methods outputs the below error...
/usr/bin/bash: - : invalid option
Usage: /usr/bin/bash [GNU long option] [option] ...
/usr/bin/bash [GNU long option] [option] script-file ...
GNU long options:
Shell options:
-irsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP or -o option

2nd method gives the below error...
gcc: no input files

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100