5

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
  • i don't have that much experience to build a java compiler, but i'm curious to know some of the answers, 1) are you producing .java files in response? Commented Sep 14, 2018 at 16:57
  • @Deadpool Yup we are creating a.java file because javac needs file input. Commented Sep 14, 2018 at 16:58

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.