Online java coding test compile submitted java code in nodejs

0 votes

I'm utilising the MEAN stack to create an online coding test app. Looking for a nodejs solution to compile and submit the Java code entered by the user in the editor. The user may enter the test parameters, execute it, and examine the results.

I found the solution below, which uses the exec approach

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

However, this method does not appear to be practicable because it requires building a jar file from the user's java code before compiling it.

Is there a better way to solve this issue?

Jun 13 in Node-js by Vaani
• 7,020 points
14 views

1 answer to this question.

0 votes

Given that the user's source code will be submitted as a single file:

You can use javac to convert the source code into an executable class, which you can then run 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 keep in mind the security implications. In essence, you're allowing the user to run untrusted code on your own server. Just make sure you run the code as an unprivileged user with no read/write privileges and no network access! Limit cpu shares and, if necessary, timeout the execution.

answered Jun 13 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How do I “include” functions from my other files in nodejs?

Hello @kartik, You require any js file,so you just ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,720 points
1,478 views
0 votes
1 answer

Error:npm WARN unmet dependency in nodejs

Hii @kartik, Following are the possible solution : Manually ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,720 points
2,633 views
0 votes
1 answer

How to write a test which expects an Error to be thrown in Jasmine?

Hello @kartik, Try using an anonymous function instead: expect( ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,720 points
2,662 views
0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,720 points
8,011 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
967 views
0 votes
1 answer
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,440 points
622 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP