Link:- www.github.com/mnnkhndlwl/compilerXpress
So, build this ide using nodejs and reactjs so let me start by first explaining to you how I implemented functionality to compile and execute Java program with input so here is the code snippet
exports.compileJavaWithInput = function (envData, code, input, fn) {
var dirname = uuid.createId();
path = "./codes/" + dirname;
fs.mkdir(path, 0777, function (err) {
if (err) console.log(err.toString().red);
else {
fs.writeFile(path + "/Main.java", code, function (err) {
if (err) console.log("ERROR: ".red + err);
else {
console.log("INFO: ".green + path + "/Main.java created");
fs.writeFile(path + "/input.txt", input, function (err) {
if (err) console.log("ERROR: ".red + err);
else {
var command = "cd " + path + " & " + " javac Main.java";
exec(command, function (error, stdout, stderr) {
if (error) {
console.log(
"INFO: ".green +
path +
"/Main.java contained an error while compiling"
);
var out = { error: stderr };
fn(out);
} else {
console.log("INFO: ".green + "compiled a java file");
var command = "cd " + path + " & java Main < input.txt";
exec(command, function (error, stdout, stderr) {
if (error) {
console.log(
"INFO: ".green +
path +
"/Main.java contained an error while executing"
);
if (
error
.toString()
.indexOf("Error: stdout maxBuffer exceeded.") != -1
) {
var out = {
error:
"Error: stdout maxBuffer exceeded. You might have initialized an infinite loop.",
};
fn(out);
} else {
var out = { error: stderr };
fn(out);
}
} else {
console.log(
"INFO: ".green +
path +
"/Main.java successfully compiled and executed !"
);
var out = { output: stdout };
fn(out);
}
});
}
});
}
});
}
});
}
});
};
In this code:
The function takes four parameters:
envData
,code
,input
, andfn
.It generates a unique directory name using
uuid.createId()
and assigns it to thedirname
variable.The
path
variable is set to the directory path where the Java files will be stored.The function creates a directory using
fs.mkdir
with the specifiedpath
and permissions of0777
.It then writes the
code
(Java source code) to aMain.java
file within the directory usingfs.writeFile
.Additionally, it writes the
input
to aninput.txt
file within the directory.The function compiles the
Main.java
file by executing the command"cd " + path + " & " + " javac
Main.java
"
usingexec
.If there is an error during the compilation, it logs an error message and calls the
fn
function with an error object.If the compilation is successful, it executes the
Main
class with the provided input by executing the command"cd " + path + " & java Main < input.txt"
usingexec
.If there is an error during execution, it logs an error message and calls the
fn
function with an error object.If execution is successful, it logs a success message, and the
fn
function is called with an output object containing the program's output.I hope it's clear further you will understand by looking at the repo and if you know the basics of nodejs and react.js then you will have no problem understanding it
Subscribe to our newsletter
Read articles from directly inside your inbox. Subscribe to the newsletter, and don't miss out.