Skip to content

Commit

Permalink
Java Support Added (#4)
Browse files Browse the repository at this point in the history
* Base Python Code

* Python Compiler

* Update Dockerfile

* Update Dockerfile

* Python

* Java Added

* Java Added
  • Loading branch information
codewithvk authored Oct 29, 2021
1 parent 5e15fb2 commit 9af26de
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ RUN apt-get update && \
rm -rf /var/lib/apt/lists/* && \
apt-get -y install python3

RUN apt update -y && apt-get install -y software-properties-common && \
apt-add-repository 'deb http://security.debian.org/debian-security stretch/updates main' && apt-get update -y && \
apt-get install -y openjdk-8-jdk-headless && \
apt-get clean
ENV java /usr/lib/jvm/java-8-openjdk-amd64/
RUN export java

COPY package*.json ./

RUN npm install
Expand Down
24 changes: 24 additions & 0 deletions api/javaApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const express = require('express');
const { javaCompile} = require('../compiler/java')
const router = express.Router();

//
router.post('/', async (req, res) => {
const InputCode = Buffer.from(req.body.code, 'base64').toString('binary')
const DeCode = Buffer.from(req.body.input, 'base64').toString('binary')
const CentralClass = req?.headers?.class || "Main"
let response = await javaCompile(InputCode, DeCode, CentralClass);
if (response.statusMes === "Compiler Error") {
res.status(202).json(response)
} else if (response.statusMes === "Run Time Error") {
res.status(201).json(response)
} else {
res.status(200).json(response)
}

});




module.exports = router;
1 change: 0 additions & 1 deletion api/pythonApi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const express = require('express');
const { CppCompile } = require('../compiler/cpp');
const { PythonCompile } = require('../compiler/python');
const router = express.Router();

Expand Down
138 changes: 138 additions & 0 deletions compiler/java.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const { v4: uuid } = require('uuid');
const { exec } = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const saveFile = (file, data) => {
return new Promise((resolve, reject) => {
fs.writeFile(file, data, function (err) {
if (err) {
reject(err);
} else {
resolve([file]);
}
});
});
};

async function deleteFiles(javaPath, inputPath, exePath) {
if (fs.existsSync(javaPath)) {
await fs.unlinkSync(javaPath);
}

if (fs.existsSync(inputPath)) {
await fs.unlinkSync(inputPath);
}

if (fs.existsSync(exePath)) {
await fs.unlinkSync(exePath);
}
}



function getExecutablePath(fileName) {
// console.log(os.platform());
if (os.platform() === 'win32') {
return `${path.join(__dirname, '..', 'upload', fileName)}.exe`;
}
if (os.platform() === 'linux') {
return `${path.join(__dirname, '..', 'upload', fileName)}`;
}
}

function getRunCommand(input, CentralClass) {

let path = getExecutablePath("")
return `cd ${path} && \ java ${CentralClass} < ${input}`;

}

function getjavaPath(fileName) {
return `${path.join(__dirname, '..', 'upload', fileName)}.java`;
}

function getInputPath(fileName) {
return `${path.join(__dirname, '..', 'upload', fileName)}-input.txt`;
}

function compileProgram(javaPath) {
return new Promise((resolve, reject) => {
exec(`javac ${javaPath}`, (error, stdout, stderr) => {
if (error) {
console.log({ error, stdout, stderr })
reject({ error, stdout, stderr });
} else {
console.log({ error, stdout, stderr })
resolve({ stdout, stderr });
}
});
});
}

function runProgram(inputPath, CentralClass) {
return new Promise((resolve, reject) => {
exec(getRunCommand(inputPath, CentralClass), (error, stdout, stderr) => {
if (error) {
console.log({ error, stdout, stderr })
reject({ error, stdout, stderr });
} else {
console.log({ error, stdout, stderr })
resolve({ stdout, stderr });
}
});
});
}


const javaCompile = async (code, input, CentralClass) => {
console.log({ code, input })
let state = {
stdout: null,
stderr: null,
statusMes: "",
}
let uniqueFileName = uuid();
// let executePath = getExecutablePath(uniqueFileName)
let javaPath = getjavaPath(uniqueFileName)
let ipPath = getInputPath(uniqueFileName)

await saveFile(javaPath, code);
await saveFile(ipPath, input);

try {
let { stdout, stderr } = await compileProgram(javaPath);
console.log({ stdout, stderr })
} catch (err) {
state.stderr = err.stderr;
state.statusMes = "Compiler Error";
deleteFiles(javaPath, ipPath);
return state;
}

try {
let { stdout, stderr } = await runProgram(ipPath, CentralClass);
state.stdout = stdout;
state.stderr = stderr;
console.log({ stdout, stderr })

} catch (err) {
state.stderr = err.stderr;
state.statusMes = "Run Time Error";
console.log({ state })
deleteFiles(javaPath, ipPath);
}

if (state.stderr === '') {
state.stderr = null;
}
state.statusMes = "Successfully Compiled";
await deleteFiles(javaPath, ipPath);
return state;

}




module.exports = { javaCompile };
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ app.get('/', (req, res) => {
})
app.use('/api', require('./api/cppApi'));
app.use('/api/python', require('./api/pythonApi'));

app.use('/api/java', require('./api/javaApi'))

app.listen(PORT, () => {
console.log(`Listening at port ${PORT}`);
Expand Down
Binary file added upload/Main.class
Binary file not shown.

0 comments on commit 9af26de

Please sign in to comment.