{
cl.addURL( toolsJar.toURI().toURL() );
}
catch ( MalformedURLException e )
{
throw new CompilerException( "Could not convert the file reference to tools.jar to a URL, path to tools.jar: '" + toolsJar.getAbsolutePath() + "'." );
}
}
Class c;
try
{
c = cl.loadClass( "com.sun.tools.javac.Main" );
}
catch ( ClassNotFoundException e )
{
String message = "Unable to locate the Javac Compiler in:" + EOL + " " + toolsJar + EOL
+ "Please ensure you are using JDK 1.4 or above and" + EOL
+ "not a JRE (the com.sun.tools.javac.Main class is required)." + EOL
+ "In most cases you can change the location of your Java" + EOL
+ "installation by setting the JAVA_HOME environment variable.";
return Collections.singletonList( new CompilerError( message, true ) );
}
StringWriter out = new StringWriter();
Integer ok;
List messages;
try
{
Method compile = c.getMethod( "compile", new Class[] { String[].class, PrintWriter.class } );
ok = (Integer) compile.invoke( null, new Object[] { args, new PrintWriter( out ) } );
messages = parseModernStream( ok.intValue(), new BufferedReader( new StringReader( out.toString() ) ) );
}
catch ( NoSuchMethodException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( IllegalAccessException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( InvocationTargetException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
catch ( IOException e )
{
throw new CompilerException( "Error while executing the compiler.", e );
}
if ( ( ok.intValue() != 0 ) && messages.isEmpty() )
{
// TODO: exception?