Evaluator.java won't compile due to issues with javax.tools.*
/*
* Evaluator.java
*
* Created on January 23, 2007, 4:17 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package ObjectTools;
import java.io.*;
import java.net.*;
import javax.tools.*;
import java.lang.reflect.*;
/**
* Utilizes {@link java.lang.reflect} package
*
* @author ppowell-c
*/
publicabstractclass Evaluator{
publicstaticboolean writeSource(final String sourcePath,final String sourceCode)
throws FileNotFoundException{
PrintWriter writer =new PrintWriter(sourcePath);
writer.println(sourceCode);
writer.close();
returntrue;
}
publicstaticboolean compile(final String sourcePath)throws IOException{
final JavaCompilerTool compiler = ToolProvider.defaultJavaCompiler();
final JavaFileManager manager = compiler.getStandardFileManager();
final JavaFileObject source =
manager.getFileForInput(sourcePath);/* java.io.IOException */
final JavaCompilerTool.CompilationTask task = compiler.run(null, source);
return task.getResult();
}
publicstaticfinal java.lang.Class loadExpression(final String path,final String className)
throws MalformedURLException,
ClassNotFoundException{/* java.net.MalformedURLException */
final URLClassLoader myLoader =new URLClassLoader(new java.net.URL[]{
new File(path).toURI().toURL()
});
/* java.lang.ClassNotFoundException */
return Class.forName(className, true, myLoader);
}
publicstatic java.lang.Object evalExpression(final Class test)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException{
final Class[] parameterType =null;
/* java.lang.NoSuchMethodException */
final Method method = test.getMethod("expression", parameterType);
final Object[] argument =null;
Object instance =null;
/* java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException */
return method.invoke(instance, argument);
}
publicstatic Object eval(final String expression)
throws FileNotFoundException, IOException, MalformedURLException,
ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
InvocationTargetException{
final Object result;
final String path ="c:/";
final String className ="ExpressionWrapper";
final String sourcePath = path + className +".java";
writeSource(/* to */ sourcePath,
"public class " + className +"\n" +
"{ public static java.lang.Object expression()\n" +
" { return " + expression +"; }}\n" );
if(compile(sourcePath)){
final Class class_ =
loadExpression(/* from */ path, className);
result = evalExpression(class_);
}else{
result =null;
}
return result;
}
}
Produces compiler errors on javax.tools.ToolProvider along with nearly everything else in javax.tools to boot.
I'm using J2SE 1.5.0 with NetBeans 5.5 so they all should be there. I'm following the example at http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht9Ht/evaluating-expressions-with-java since all I want to do is come up with a Java equivalent of "eval()", which I use in PHP whenever I need it.
Help appreciated, I'm lost here.
Thanx
Phil

