Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Java
Process process = Runtime.getRuntime().exec(new String[]{
     "C:/cygwin/bin/bash", 
     "-c", 
     "/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
Updated 3-Jul-12 19:25pm
v2

1 solution

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


Cheers,
Peter
 
Share this answer
 
v2
Comments
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:
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--protected
--rcfile
--restricted
--verbose
--version
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900