How do I use the JavaCompiler in javax.tools to produce more detailed diagnostics or give me an exception stack trace?
<p>I want to be able to dynamically compile a Java file and handle any compile errors and exceptions one by one. So I looked at <a href="http://docs.oracle.com/javase/6/docs/api/javax/tools/JavaCompiler.html" rel="nofollow">JavaCompiler</a>, and there's an example used to collect diagnostics:</p>

<pre><code>Iterable&lt;? extends JavaFileObject&gt; compilationUnits = ...;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector&lt;JavaFileObject&gt; diagnostics = new DiagnosticCollector&lt;JavaFileObject&gt;();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
compiler.getTask(null, fileManager, diagnostics, null, null, compilationUnits).call();

for (Diagnostic diagnostic : diagnostics.getDiagnostics())
    System.out.format("Error on line %d in %d%n",
                      diagnostic.getLineNumber()
                      diagnostic.getSource().toUri());

fileManager.close();
</code></pre>

<p>This is all fine and dandy, except that the <a href="http://docs.oracle.com/javase/6/docs/api/javax/tools/Diagnostic.html" rel="nofollow">Diagnostic</a> objects I get back are not very helpful in determining exactly what compile error occurred during compilation. There is a <code>getMessage()</code> and <code>getCode()</code> method that helps determine more specifics, but the API says:</p>

<blockquote>
  <p>The actual message is implementation-dependent.</p>
</blockquote>

<p>Which seems not very useful for portability, and if I was to parse it, I'd be doing it in a round-about way. So I'm wondering if I'm missing something when reading the API, or is there a better alternative to the <code>javax.tools</code> package.</p>
