File size: 1,489 Bytes
a55949b
 
314a3e4
a55949b
314a3e4
 
a55949b
 
 
 
 
 
 
 
 
336b464
a55949b
 
314a3e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7e6e29
 
314a3e4
 
 
 
 
 
 
 
 
 
 
 
e7e6e29
 
314a3e4
 
 
 
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
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require("fs");
const { exec } = require("child_process");
const compiler = require("compilex");
const options = { stats: true };
compiler.init(options);

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());

app.get("/", function (req, res) {
  res.send("Hello World!!");
});

app.post("/compile", (req, res) => {
  const { code, input } = req.body; // Assuming code and input are sent as POST parameters

  // Write the Java code to Main.java file
  fs.writeFile("/code/temp/Main.java", code, (err) => {
    if (err) {
      console.error(err);
      res.status(500).send("Error writing Java file");
      return;
    }

    // Compile and run the Java code with input
    const javaProcess = exec(
      "javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
      (error, stdout, stderr) => {
        if (error) {
          console.error(error);
          res.status(500).send("Error compiling or executing Java code");
          return;
        }

        console.log(stdout);
        console.error(stderr);
        res.send(stdout);
      }
    );

    // Pass input to the Java process
    if (input) {
      javaProcess.stdin.write(input);
      javaProcess.stdin.end();
    }
  });
});

const port = 7860;
app.listen(port, () => {
  console.log(`Open http://localhost:${port}`);
});