forked from Bit-Developer/code-editor-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRunner.js
More file actions
66 lines (59 loc) · 1.78 KB
/
Copy pathJavaRunner.js
File metadata and controls
66 lines (59 loc) · 1.78 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
const { spawn } = require('child_process');
const Runner = require('./Runner');
class JavaRunner extends Runner {
defaultFile() {
return this.defaultfile;
}
constructor() {
super();
this.defaultfile = 'Hello.java';
}
run(file, directory, filename, extension, callback) {
if (extension.toLowerCase() !== '.java') {
console.log(`${file} is not a java file.`);
}
this.compile(file, directory, filename, callback);
}
// compile java source file
compile(file, directory, filename, callback) {
// set working directory for child_process
const options = { cwd: directory };
// var compiler = spawn('javac', ['CodeJava.java']);
const argsCompile = [];
argsCompile[0] = file;
const compiler = spawn('javac', argsCompile);
compiler.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
compiler.stderr.on('data', (data) => {
console.log(`compile-stderr: ${String(data)}`);
callback('1', String(data)); // 1, compile error
});
compiler.on('close', (data) => {
if (data === 0) {
this.execute(filename, options, callback);
}
});
}
// execute the compiled class file
execute(filename, options, callback) {
const argsRun = [];
argsRun[0] = filename;
const executor = spawn('java', argsRun, options);
executor.stdout.on('data', (output) => {
console.log(String(output));
callback('0', String(output)); // 0, no error
});
executor.stderr.on('data', (output) => {
console.log(`stderr: ${String(output)}`);
callback('2', String(output)); // 2, execution failure
});
executor.on('close', (output) => {
this.log(`stdout: ${output}`);
});
}
log(message) {
console.log(message);
}
}
module.exports = JavaRunner;