-1

I am trying to compile c file through java code using exec method

String inputFilePath = "\"D:\\Soft\\WebApplication\\build\\web\\code\\Demo.c\"";
String[] commands = {"cmd", "/c", "gcc",inputFilePath,"-o","Demo"};
Process p=Runtime.getRuntime().exec(commands);
DataInputStream din=new DataInputStream(p.getErrorStream());
String s="",temp;
while((temp=din.readLine())!=null)
      s+=temp;
      if(s.equals("")){
         cf.setResult("No Syntax Error");
      }
      else
        cf.setResult(s);

but it is not generating demo.exe file

saurabh
  • 6,687
  • 7
  • 42
  • 63

1 Answers1

1

Use ProcessBuilder to make this easier.

This should work on Windows (at the moment, I only have Linux here).

String directory = "D:\\Soft\\WebApplication\\build\\web\\code\\";
String[] commands = {"cmd", "/C", "gcc", "Demo.c", "-o", "Demo.exe"};

ProcessBuilder pb = new ProcessBuilder();
pb.directory(new File(directory));
pb.command(commands);

Process p = pb.start();

// process in/out streams
D.Kastier
  • 2,640
  • 3
  • 25
  • 40