0

I am doing an online coding test application using MEAN stack. Looking for a solution in nodejs, to compile the Java code that user enters in editor and submits. User can enter the test input, run and check for the output.

I have come across below solution with exec method,

exec('java -jar userCode.jar',function (error, stdout, stderr){
});

But this solution does not sound feasible as it involves creating jar file out of the user submitted java code before compiling.

Is there a better solution for this problem?

Madhura KM
  • 140
  • 1
  • 10

2 Answers2

2

Given the user will submit his source code as a single file:

What you can do is compile the source into an executable class using javac, then simply execute it with java.

Compiling:

javac -cp /path/to/libs/jar/files MyProgram.java

Executing:

java -cp .;/path/to/libs/jar/files  MyProgram

It'll work but beware of the security implications of that. You are basicly enabling the user to execute untrusted code on your own server. Just be sure to execute code as an unprivileged user, with no read/write access to anything, and no network access at all! Also limit cpu shares and timeout the execution as appropriate.

Atrakeur
  • 4,126
  • 3
  • 17
  • 22
0

I found solution with nodeJS library compilex, which allows to compile multiple languages like Java, C, C++, C#, Python etc. It also supports input from User.

Madhura KM
  • 140
  • 1
  • 10