JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
The javadoc for JavaCompiler shows how to work and compile with this JDK facility. One snippet from there:
File[] files1 = ... ; // input for first compilation task
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits1 =
fileManager.getJavaFileObjectsFromFiles(Arrays.asList(files1));
compiler.getTask(null, fileManager, null, null, null, compilationUnits1).call();
It is by no means a simple task. But a general method for invoking a Java compiler via the shell - with all the trimmings - isn't easy either, and this, then, also applies to calls via Process.