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, 2022 in Node-js by Vaani
• 7,070 points
892 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, 2022 by Neha
• 9,020 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,840 points
3,440 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,840 points
6,323 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,840 points
9,544 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,840 points
13,783 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
1,939 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,465 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,457 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,899 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,673 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP