问题
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