Skip to content

Commit

Permalink
Use spawn for java execution
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynmh committed Nov 6, 2023
1 parent 068e436 commit 4b7d094
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Execution/src/ExecutionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ const executeJava = async (req, res) => {
const maxExecutionTime = 5000; // 5 seconds
const javaCode = req.body.code;
const javaFilename = 'Main.java';
const javaClassName = 'Main.class';
const javaClassName = 'Main';

// Write the Java source code to a .java file
fs.writeFileSync(javaFilename, javaCode);

// Compile the Java source code
const compileProcess = spawn('javac', [javaFilename]);

compileProcess.on('error', (err) => {
res.json({ output: err.message });
});
Expand Down Expand Up @@ -92,23 +93,25 @@ const executeJava = async (req, res) => {
clearTimeout(timeout);

if (code !== 0) {
res.json({ output: `Execution failed with code ${code}` });
logger.log('Java program timedout');
res.json({ output: `Timeout: Your code ran longer than 5 seconds.` });
} else {
console.log('Java program executed successfully:');
logger.log('Java program executed successfully:');
fs.unlinkSync(javaFilename);
fs.unlinkSync('Main.class');
res.json({ output: programOutput });
}
});

// Set a timeout to kill the script if it runs for too long
const timeout = setTimeout(() => {
console.error('Java script execution timed out. Killing the script...');
logger.error('Script execution timed out. Killing the script...');
executeProcess.kill();
res.json({ output: 'TimeoutError' });
}, maxExecutionTime);
}
});
} catch (err) {
console.log(err);
logger.log(err);
}
};

Expand Down

0 comments on commit 4b7d094

Please sign in to comment.