CodeCompiler - Compile and Run Java Program from another Java Program

user3205479 :

We are trying to build a java code compiler for one of our clients to create online coding tests. We are posting the source code through REST API to an endpoint. Next, we are compiling and executing the output against test files then returning results back to the client.

The challenge here is we are spinning up a process javac - compile code and java - execute code on every request which actually takes 30MB of RAM and 600 ms for every process.

This is very huge we are unable to handle the load of 1000 users on octa-core and 16GB RAM. If We get 200 requests at once, it is spawning 200 process which is heavy and other requests are queued up.

The solutions we can think of

  1. Don't launch javac every time, reuse it but how (javac kills itself once compilation is done)?
  2. javac should compile string input rather than from a file.

We are just curious how does the online coding competition platforms solve these problems? We are targeting only for Java. Any help is greatly appreciated.

https://www.journaldev.com/937/compile-run-java-program-another-java-program

Luke Woodward :

Since Java 1.6, Java has had a programmatic API to access the Java compiler without using a subprocess. As a starting point, see the documentation for the JavaCompiler class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=99129&siteId=1