title: ToolProvider.getSystemJavaCompiler() with jre + tools.jar
body: <p>Note: Its a repeat question from oracle forums as nobody answered it over there.</p>
  
  <p>Our apps ships with a JRE and not a JDK and in the classpath we also have tools.jar.</p>
  
  <p>From our app we compile the abc.java with the command: com.sun.tools.javac.Main.compile(file);</p>
  
  <p>Then next step is that we do some annotation processing with the apt tool e.g.</p>
  
  <pre><code>args[0] = "-nocompile";
  args[1]=abc.java
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              int aptReturnCode = com.sun.tools.apt.Main.process(processor, new PrintWriter(baos), fl); //where processor is the custom annotation processor.
  </code></pre>
  
  <p>With java-8 we have found that oracle have made the apt tool defunct so instead of using the apt tool we are supposed to use the java compiler for the same task.(I got this tip from this forum in the past) So I came up with code below which needed some reworking of the annotation processor but the problem is that <code>ToolProvider.getSystemJavaCompiler( )</code> always returns null. Is there any way that I could do this annotation processing or load the java compiler with just the tools.jar in the class path without using a jdk ?</p>
  
  <pre><code>ArrayList&lt;String&gt; customClasses = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; arguments = new ArrayList&lt;String&gt;();//arguments for the java compiler
  arguments.add( "-proc:only" ); // only do annotations processing and do not compile the source code.
  customClasses.add("full path/abc.java");
  
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // always returns null
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  DiagnosticCollector&lt;JavaFileObject&gt; diagnostics = new DiagnosticCollector&lt;JavaFileObject&gt;();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
    Iterable&lt;? extends JavaFileObject&gt; compilationUnits = fileManager.getJavaFileObjectsFromStrings(customClasses);
  
    JavaCompiler.CompilationTask task =
    compiler.getTask(new PrintWriter(baos), fileManager, diagnostics, arguments, null, compilationUnits);
  
    ArrayList&lt;Processor&gt; processors = new ArrayList&lt;Processor&gt;();
    processors.add( new MyCustomProcessor() );
    task.setProcessors( processors );
  
    boolean success = task.call();
  </code></pre>
  
  <p>Many Thanks</p>