-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun.java
More file actions
130 lines (108 loc) · 3.91 KB
/
Copy pathRun.java
File metadata and controls
130 lines (108 loc) · 3.91 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
/*
* File: proj6BayyurtWenZhang.Run.java
* Names: Izge Bayyurt, Muqing Wen, Chloe Zhang
* Class: CS361
* Project 6
* Date: 3/17/2022
*/
package proj6BayyurtWenZhang;
import javafx.application.Platform;
import org.fxmisc.richtext.StyleClassedTextArea;
import java.io.*;
public class Run extends Thread{
private File fileToCompile;
private AlertHandler alertHandler;
private StyleClassedTextArea console;
private String errorMessage;
private boolean killProcess;
public Run(File fileToCompile, StyleClassedTextArea console) {
this.fileToCompile = fileToCompile;
this.alertHandler = new AlertHandler();
this.console = console;
this.errorMessage = null;
this.killProcess = false;
}
/**
* Runs a compiled java file and redirects the output to the console
* */
public void run() {
ProcessBuilder pb = new ProcessBuilder();
try {
pb.command("java", "-cp", this.fileToCompile.getCanonicalPath()
.replace("/"+ this.fileToCompile.getName(), ""),
this.fileToCompile.getName().replace(".java", ""));
pb.redirectErrorStream(true);
Process p = pb.start();
Controller.outputStream = p.getOutputStream();
InputStream processInputStream = p.getInputStream();
int byteValueFromRead;
String terminalOutput = "";
while(p.isAlive()) {
if (killProcess == true){
break;
}
while (processInputStream.available() != 0 && (byteValueFromRead = processInputStream.read()) != -1) {
terminalOutput += (char) byteValueFromRead;
if (byteValueFromRead == 10)
break;
}
if (terminalOutput != ""){
// reassign it to a new variable to avoid error in lambda
// try swapping output with terminalOutput in Platform.runLater()
// for demonstration
String output = terminalOutput;
Platform.runLater(() -> {
console.append(output, "");
console.requestFollowCaret();
});
terminalOutput = "";
}
}
killProcess = false;
p.waitFor();
if (p.exitValue() != 0) {
BufferedReader r = new BufferedReader(
new InputStreamReader(p.getErrorStream()));
String readLine;
String errorMessage = "";
while ((readLine = r.readLine()) != null) {
errorMessage += readLine + "\n";
}
this.errorMessage = errorMessage;
}
} catch (IOException e) {
Platform.runLater(() -> {
this.alertHandler.showAlert("File not found or not accessible. "+
"Ensure a valid path is specified and try again.",
"Failed to Open File");
});
this.errorMessage = "File could not be found.\n";
return;
} catch (InterruptedException e) {
Platform.runLater(() -> {
this.alertHandler.showAlert("User interrupted the process, exiting.",
"Process interrupted");
});
this.errorMessage = "Process interrupted.\n";
return;
}
}
/**
* @return a boolean that tells if an Error Message exists
* */
public boolean hasErrorMessage(){
return errorMessage != null;
}
/**
* @return the error message
* */
public String getErrorMessage() {
return errorMessage;
}
/**
* Kills the running process
*/
public void killProcess(){
killProcess = true;
}
}