blob: 5e32eea0cc3023e03d88e7e640f1f3613ca9ba9e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
package Compiler;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class ExecuteC {
public void compileAndExecuteC(List<String> code){
writeProgram(code);
if (!compileC()){
String output = runProgram();
System.out.println(output);
}
else{
Language.displayError("Runtime Error");
}
}
public void writeProgram(List<String> codeLines){
BufferedWriter output = null;
try {
File file = new File("main.c");
output = new BufferedWriter(new FileWriter(file));
for(String line:codeLines){
output.write(line+"\n");
}
output.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
public Boolean compileC(){
try{
String s= null;
Process p = Runtime.getRuntime().exec("cmd /C gcc main.c -o main.exe");
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
boolean error=false;
while ((s = stdError.readLine()) != null) {
error=true;
}
return error;
} catch (IOException e){
e.printStackTrace();
}
return false;
}
public String runProgram(){
try{
String[] command = {"cmd", "/C", "main.exe"};
ProcessBuilder probuilder = new ProcessBuilder(command);
Process p = probuilder.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String s = null;
String output="";
while ((s = stdInput.readLine()) != null) {
output+=s;
}
return output;
} catch (IOException e){
e.printStackTrace();
}
return null;
}
}
|