Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...