CodeCompiler - Compile and Run Java Program from another Java Program

Viewed 467

We are trying to build a java code compiler 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

2 Answers

You can use the javax.tools.JavaCompiler class to compile the java file dynamically. Here is a code snippet to get you started.

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
List<String> optionList = new ArrayList<String>();
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null,
    fileManager.getJavaFileObjectsFromFiles(Arrays.asList(javaFiletoCompile)));
if (task.call())
{
    //success
} else
{
    //fail      
}
fileManager.close();
Related