-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompile.java
More file actions
84 lines (68 loc) · 2.28 KB
/
Copy pathCompile.java
File metadata and controls
84 lines (68 loc) · 2.28 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
/*
* File: proj6BayyurtWenZhang.Compile.java
* Names: Izge Bayyurt, Muqing Wen, Chloe Zhang
* Class: CS361
* Project 6
* Date: 3/17/2022
*/
package proj6BayyurtWenZhang;
import org.fxmisc.richtext.StyleClassedTextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Handles a new thread to compile a java program.
*/
public class Compile extends Thread{
private File fileToCompile;
private AlertHandler alertHandler;
private StyleClassedTextArea console;
private String errorMessage;
public Compile(File fileToCompile, StyleClassedTextArea console) {
this.fileToCompile = fileToCompile;
this.alertHandler = new AlertHandler();
this.console = console;
this.errorMessage = null;
}
/**
* Compiles a java file and tells the user if it succeeded in doing so
* */
public void run() {
ProcessBuilder pb = new ProcessBuilder();
try {
pb.command("javac", this.fileToCompile.getCanonicalPath());
Process p = pb.start();
p.waitFor();
// if the compilation failed for some reason, set an error message
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 + "\n";
}
} catch (IOException e) {
this.alertHandler.showAlert("File not found or not accessible. Ensure a valid path is specified" +
"and try again.", "Failed to Open File");
return;
} catch (InterruptedException e) {
this.alertHandler.showAlert("User interrupted the process, exiting.", "Process interrupted");
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;
}
}