Click here to Skip to main content
Rate this: bad
good
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[]{
     "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:04pm
Edited 3-Jul-12 19:25pm
v2

1 solution

Rate this: bad
good
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:/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  
v2
Comments
NewMember2000 at 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)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2014
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