Compiling a C Source through javacode using gcc

有些话、适合烂在心里 提交于 2019-12-25 17:10:08

问题


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


回答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


来源:https://stackoverflow.com/questions/35900749/compiling-a-c-source-through-javacode-using-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!