}
final File toolsJar = new File( System.getProperty( "java.home" ), "../lib/tools.jar" );
if ( !toolsJar.exists() )
{
throw new CompilerException( "tools.jar not found: " + toolsJar );
}
try
{
// Combined classloader with no parent/child relationship, so classes in our classloader
// can reference classes in tools.jar
URL[] originalUrls = ((URLClassLoader) JavacCompiler.class.getClassLoader()).getURLs();
URL[] urls = new URL[originalUrls.length + 1];
urls[0] = toolsJar.toURI().toURL();
System.arraycopy(originalUrls, 0, urls, 1, originalUrls.length);
ClassLoader javacClassLoader = new URLClassLoader(urls);
final Thread thread = Thread.currentThread();
final ClassLoader contextClassLoader = thread.getContextClassLoader();
thread.setContextClassLoader( javacClassLoader );
try
{
//return Class.forName( JavacCompiler.JAVAC_CLASSNAME, true, javacClassLoader );
return javacClassLoader.loadClass( JavacCompiler.JAVAC_CLASSNAME );
}
finally
{
thread.setContextClassLoader( contextClassLoader );
}
}
catch ( MalformedURLException ex )
{
throw new CompilerException(
"Could not convert the file reference to tools.jar to a URL, path to tools.jar: '"
+ toolsJar.getAbsolutePath() + "'.", ex );
}
catch ( ClassNotFoundException ex )
{
throw new CompilerException( "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.", ex );
}