Click here to Skip to main content
Sign Up to vote bad
good
See more: JavaEclipseGCC
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[]{
     "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 3 Jul '12 - 19:04
Edited 3 Jul '12 - 19:25


1 solution

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:/cygwin/bin/bash",
"-c /usr/bin/gcc",
"/cygdrive/d/MyFile.c",
"-o /cygdrive/d/MyExe"});
or
Process process = Runtime.getRuntime().exec(
"C:/cygwin/bin/bash -c /usr/bin/gcc /cygdrive/d/MyFile.c -o /cygdrive/d/MyExe");
 
Cheers,
Peter
  Permalink  
Comments
NewMember2000 - 4 Jul '12 - 1:50
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 508
1 Arun Vasu 275
2 Maciej Los 238
3 OriginalGriff 215
4 Mahesh Bailwal 200
0 Sergey Alexandrovich Kryukov 9,660
1 OriginalGriff 7,329
2 CPallini 3,968
3 Rohan Leuva 3,339
4 Maciej Los 2,851


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