0

The implementation of the following functionality is required - the user writes code on the site, for example, a method or class in java is sent for verification. The system checks the code for compilation and tests the business logic of the method (or class). I know how to implement compilation at runtime of a "simple" class, for example: File classFile = new File("FirstTest.java"); Files.write(classFile.toPath(), source.getBytes(StandardCharsets.UTF_8));

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

OutputStream outputStreamErr = new ByteArrayOutputStream();
int compilingResult = compiler.run(null, null, outputStreamErr, classFile.getPath());

But when the compiled class contains any additional import (even just a collection, not to mention JUnit), then an error occurs during compilation. What are the options to resolve this issue. How can you add dependencies during dynamic compilation, or are there any other ways to implement this functionality?

0 Answers0