Skip to content

Commit 9af26de

Browse files
authored
Java Support Added (#4)
* Base Python Code * Python Compiler * Update Dockerfile * Update Dockerfile * Python * Java Added * Java Added
1 parent 5e15fb2 commit 9af26de

File tree

6 files changed

+170
-2
lines changed

6 files changed

+170
-2
lines changed

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ RUN apt-get update && \
99
rm -rf /var/lib/apt/lists/* && \
1010
apt-get -y install python3
1111

12+
RUN apt update -y && apt-get install -y software-properties-common && \
13+
apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main' && apt-get update -y && \
14+
apt-get install -y openjdk-8-jdk-headless && \
15+
apt-get clean
16+
ENV java /usr/lib/jvm/java-8-openjdk-amd64/
17+
RUN export java
18+
1219
COPY package*.json ./
1320

1421
RUN npm install

api/javaApi.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const express = require('express');
2+
const { javaCompile} = require('../compiler/java')
3+
const router = express.Router();
4+
5+
//
6+
router.post('/', async (req, res) => {
7+
const InputCode = Buffer.from(req.body.code, 'base64').toString('binary')
8+
const DeCode = Buffer.from(req.body.input, 'base64').toString('binary')
9+
const CentralClass = req?.headers?.class || "Main"
10+
let response = await javaCompile(InputCode, DeCode, CentralClass);
11+
if (response.statusMes === "Compiler Error") {
12+
res.status(202).json(response)
13+
} else if (response.statusMes === "Run Time Error") {
14+
res.status(201).json(response)
15+
} else {
16+
res.status(200).json(response)
17+
}
18+
19+
});
20+
21+
22+
23+
24+
module.exports = router;

api/pythonApi.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express');
2-
const { CppCompile } = require('../compiler/cpp');
32
const { PythonCompile } = require('../compiler/python');
43
const router = express.Router();
54

compiler/java.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
const { v4: uuid } = require('uuid');
2+
const { exec } = require('child_process');
3+
const fs = require('fs');
4+
const os = require('os');
5+
const path = require('path');
6+
const saveFile = (file, data) => {
7+
return new Promise((resolve, reject) => {
8+
fs.writeFile(file, data, function (err) {
9+
if (err) {
10+
reject(err);
11+
} else {
12+
resolve([file]);
13+
}
14+
});
15+
});
16+
};
17+
18+
async function deleteFiles(javaPath, inputPath, exePath) {
19+
if (fs.existsSync(javaPath)) {
20+
await fs.unlinkSync(javaPath);
21+
}
22+
23+
if (fs.existsSync(inputPath)) {
24+
await fs.unlinkSync(inputPath);
25+
}
26+
27+
if (fs.existsSync(exePath)) {
28+
await fs.unlinkSync(exePath);
29+
}
30+
}
31+
32+
33+
34+
function getExecutablePath(fileName) {
35+
// console.log(os.platform());
36+
if (os.platform() === 'win32') {
37+
return `${path.join(__dirname, '..', 'upload', fileName)}.exe`;
38+
}
39+
if (os.platform() === 'linux') {
40+
return `${path.join(__dirname, '..', 'upload', fileName)}`;
41+
}
42+
}
43+
44+
function getRunCommand(input, CentralClass) {
45+
46+
let path = getExecutablePath("")
47+
return `cd ${path} && \ java ${CentralClass} < ${input}`;
48+
49+
}
50+
51+
function getjavaPath(fileName) {
52+
return `${path.join(__dirname, '..', 'upload', fileName)}.java`;
53+
}
54+
55+
function getInputPath(fileName) {
56+
return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`;
57+
}
58+
59+
function compileProgram(javaPath) {
60+
return new Promise((resolve, reject) => {
61+
exec(`javac ${javaPath}`, (error, stdout, stderr) => {
62+
if (error) {
63+
console.log({ error, stdout, stderr })
64+
reject({ error, stdout, stderr });
65+
} else {
66+
console.log({ error, stdout, stderr })
67+
resolve({ stdout, stderr });
68+
}
69+
});
70+
});
71+
}
72+
73+
function runProgram(inputPath, CentralClass) {
74+
return new Promise((resolve, reject) => {
75+
exec(getRunCommand(inputPath, CentralClass), (error, stdout, stderr) => {
76+
if (error) {
77+
console.log({ error, stdout, stderr })
78+
reject({ error, stdout, stderr });
79+
} else {
80+
console.log({ error, stdout, stderr })
81+
resolve({ stdout, stderr });
82+
}
83+
});
84+
});
85+
}
86+
87+
88+
const javaCompile = async (code, input, CentralClass) => {
89+
console.log({ code, input })
90+
let state = {
91+
stdout: null,
92+
stderr: null,
93+
statusMes: "",
94+
}
95+
let uniqueFileName = uuid();
96+
// let executePath = getExecutablePath(uniqueFileName)
97+
let javaPath = getjavaPath(uniqueFileName)
98+
let ipPath = getInputPath(uniqueFileName)
99+
100+
await saveFile(javaPath, code);
101+
await saveFile(ipPath, input);
102+
103+
try {
104+
let { stdout, stderr } = await compileProgram(javaPath);
105+
console.log({ stdout, stderr })
106+
} catch (err) {
107+
state.stderr = err.stderr;
108+
state.statusMes = "Compiler Error";
109+
deleteFiles(javaPath, ipPath);
110+
return state;
111+
}
112+
113+
try {
114+
let { stdout, stderr } = await runProgram(ipPath, CentralClass);
115+
state.stdout = stdout;
116+
state.stderr = stderr;
117+
console.log({ stdout, stderr })
118+
119+
} catch (err) {
120+
state.stderr = err.stderr;
121+
state.statusMes = "Run Time Error";
122+
console.log({ state })
123+
deleteFiles(javaPath, ipPath);
124+
}
125+
126+
if (state.stderr === '') {
127+
state.stderr = null;
128+
}
129+
state.statusMes = "Successfully Compiled";
130+
await deleteFiles(javaPath, ipPath);
131+
return state;
132+
133+
}
134+
135+
136+
137+
138+
module.exports = { javaCompile };

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ app.get('/', (req, res) => {
1212
})
1313
app.use('/api', require('./api/cppApi'));
1414
app.use('/api/python', require('./api/pythonApi'));
15-
15+
app.use('/api/java', require('./api/javaApi'))
1616

1717
app.listen(PORT, () => {
1818
console.log(`Listening at port ${PORT}`);

upload/Main.class

1.02 KB
Binary file not shown.

0 commit comments

Comments
 (0)