-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompileAndRun.java
More file actions
240 lines (198 loc) · 8.96 KB
/
compileAndRun.java
File metadata and controls
240 lines (198 loc) · 8.96 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class compileAndRun implements Runnable {
Thread t;
File filePath;
NewApplication jframe;
String answer = "";
ArrayList<String> compilecommands = new ArrayList<>(); //for compilation commands
ArrayList<String> runcommands = new ArrayList<>(); // for run commands
compileAndRun(File filePath, NewApplication p, String whichFile) {
t = new Thread(this, "car"); // compile only ,flag =1 ; run only ,flag=2; compile and Run ,flag=3
jframe = p;
String temp = filePath.getName();
int yahaTak = temp.lastIndexOf(".");
temp = temp.substring(0, yahaTak);
runcommands.add("cmd");
runcommands.add("/c");
if (whichFile.equals("C")) {
this.compilecommands.add("gcc");
this.compilecommands.add(filePath.getName());
this.compilecommands.add("-o");
this.compilecommands.add(temp);
System.out.println(filePath + " " + filePath.getName());
} else if (whichFile.equals("C++")) {
this.compilecommands.add("g++");
this.compilecommands.add(filePath.getName());
this.compilecommands.add("-o");
this.compilecommands.add(temp);
} else if (whichFile.equals("JAVA")) {
this.compilecommands.add("javac");
this.compilecommands.add(filePath.getName());
this.runcommands.add("java");
}
this.runcommands.add(temp);
t.start();
}
@Override
public void run() {
if (jframe.flag == 1) {
try {
jframe.outputAndCompileLog.setText("Compilation Log :");
jframe.consoleWindow.setText("Compiling....");
this.compileKaro();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (jframe.flag == 2) {
jframe.outputAndCompileLog.setText("Output");
jframe.consoleWindow.setText("");
try {
this.runKaro();
} catch (InterruptedException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (jframe.flag == 3) {
try {
this.compileKaro();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
jframe.outputAndCompileLog.setText("Output");
jframe.consoleWindow.setText("");
try {
this.runKaro();
} catch (InterruptedException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void compileKaro() throws IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder(compilecommands);
processBuilder.directory(this.jframe.s.getParentFile()); //set ProcessBuilder's current working drectory
processBuilder.redirectErrorStream(true); //Sets this process builder's redirectErrorStream property
Process process = processBuilder.start();
StringBuilder processOutput = new StringBuilder(); //create an empty stringBuilder with a capacity = 16
try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()));) { //no need to close BufferedReader ,it is autoclosable
String readLine;
while ((readLine = processOutputReader.readLine()) != null) { //processOutputReader contains buffered output stream
processOutput.append(readLine + System.lineSeparator()); //lineSeparator() take system's lineSeparator
}
// process.waitFor();
process.destroy(); //to terminate the process
}
if (processOutput.toString().trim().length() != 0) {
jframe.consoleWindow.setText(processOutput.toString().trim()); //trim() omits leading and trailing whitespace
} else {
System.out.println(jframe.s);
jframe.compiledCFilePaths.addElement(jframe.s);
jframe.consoleWindow.setText("Compiled Successfully!!!!");
jframe.latestCompiledFilePath = jframe.s;
}
jframe.flag = 1;
this.jframe.flagOngoing = 0;
}
// Trying to make output process interactive
/* public static boolean isAlive(Process p) {
try {
p.exitValue();
return false;
} catch (IllegalThreadStateException e) {
return true;
}
}
public void runKaro() throws IOException {
ProcessBuilder pb = new ProcessBuilder(runcommands);
pb.directory(jframe.s.getParentFile());
Process process = null;
try {
process = pb.start();
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
InputStream out = process.getInputStream();
OutputStream in = process.getOutputStream();
byte[] buffer = new byte[4000];
while (isAlive(process)) {
int no = out.available();
if (no > 0) {
int n = out.read(buffer, 0, Math.min(no, buffer.length));
this.jframe.consoleWindow.setText(new String(buffer, 0, n));
}
int ni = System.in.available();
if (ni > 0)
{
int n = System.in.read(buffer, 0, Math.min(ni, buffer.length));
in.write(buffer, 0, n);
in.flush();
}
}
}
}*/
public void runKaro() throws InterruptedException {
ProcessBuilder pb = new ProcessBuilder(runcommands);
pb.directory(this.jframe.s.getParentFile());
Process process = null;
try {
process = pb.start();
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String input = this.jframe.inputArea.getText();
try {
writer.write(input + "\n");
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
try {
writer.flush();
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
String line = "", output = "";
try {
// System.out.println(reader.readLine());
while ((line = reader.readLine()) != null) {
output = output + line;
output = output + "\n";
}
this.answer = output;
this.jframe.consoleWindow.setText(this.answer);
System.out.println("xyz" + process.exitValue() + "abc");
if(process.exitValue() == -1073741676 )
{
this.jframe.outputAndCompileLog.setText("Compilation log :");
this.jframe.consoleWindow.setText("Divide by Zero error.....");
}
else if(process.exitValue() == -1073741819)
{
this.jframe.outputAndCompileLog.setText("Compilation log :");
this.jframe.consoleWindow.setText("Segmentation Fault(core Dumped).....");
}
process.destroy();
this.jframe.flag = 1;
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
try {
writer.close();
//System.out.println("Exit With value" + returnValue );
} catch (IOException ex) {
Logger.getLogger(compileAndRun.class.getName()).log(Level.SEVERE, null, ex);
}
}
}