title: Can I provide runtime compiler access when running with JRE in Java 9+?
body: <p>I'm migrating an application to Java 10. Our application normally runs with the JRE, but we allow users to compile bits of their own custom code by bundling <code>tools.jar</code> and using reflection to load a <code>JavacTool</code> instance on demand. Our method looks like:</p>
  
  <pre><code>public static JavaCompiler getJavaCompiler() {
      String toolJarName = "tools.jar";
      File file = getResourceForClass("tools.jar");
      try {
          file = file.getCanonicalFile();
      } catch (IOException ignore) {
      }
      if (!file.exists())
          throw new RuntimeException("Can't find java tools file: '"+file+"'");
      try {
          URL[] urls = new URL[]{ file.toURI().toURL() };
          URLClassLoader cl = URLClassLoader.newInstance(urls);
          return Class.forName("com.sun.tools.javac.api.JavacTool", false, cl).asSubclass(JavaCompiler.class).newInstance();
      } catch (Exception e) {
          throw new RuntimeException("Can't find java compiler from '"+file+"': "+e.getMessage());
      }
  }
  </code></pre>
  
  <p>This is necessary because <code>javax.tools.ToolProvider.getSystemJavaCompiler()</code> returns null when running from the JRE. Our method worked well with Java 8, but <code>tools.jar</code> was removed in Java 9, and the class I need <code>com.sun.tools.javac.api.JavacTool</code> is in the <code>jdk.compiler</code> module which remains part of the JDK, but not the JRE.</p>
  
  <p>Is there any way to load the <code>jdk.compiler</code> module when launching the JRE? I suspect it's not, based on <a href="https://stackoverflow.com/a/39086501">this answer</a>, and my attempts to use <code>--add-module</code> result in:
  <code>java.lang.module.FindException: JMOD format not supported at execution time</code></p>
  
  <p>Is there another solution I'm missing?</p>