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

[6534 byte] By [ppowell777a] at [2007-11-26 16:00:32]
# 1
Probably the jar that contains javax.tools isn't on your classpath. It's not automatically put there.
ChuckBinga at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 2
That would be tools.jar and it does reside within my CLASSPATH
ppowell777a at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 3
> That would be tools.jar and it does reside within my> CLASSPATHMaybe. Maybe not.Could you post the actual error messages please?
cotton.ma at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 4

> That would be tools.jar and it does reside within my

> CLASSPATH

It should not be on your classpath. The fact that it is should not be what's causing the problem, but with a proper install, it gets found relative to JAVA_HOME or to the java executable or something. Take it off your classpath. That *might* help, but I doubt it. There's probably something wrong with your install, or possibly with what you're doing.

jverda at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 5

> > That would be tools.jar and it does reside within

> my

> > CLASSPATH

>

> It should not be on your classpath. The fact that it

> is should not be what's causing the problem, but with

> a proper install, it gets found relative to JAVA_HOME

> or to the java executable or something. Take it off

> your classpath. That *might* help, but I doubt it.

> There's probably something wrong with your install,

> or possibly with what you're doing.

Probably, however, it might be easier if I were to just ask the most dreaded Java question of all:

How do you do the Java equivalent of eval()?

ppowell777a at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 6

> How do you do the Java equivalent of eval()?

The language provides no support for that.

Reflection lets you call any method, where you specify that method's name as a string and go through a few other steps to build the proper Method object with the proper args. (http://java.sun.com/docs/books/tutorial/reflect/) It won't let you just evaluate Java source code as a script interpreter though. To do that, see www.beanshell.org. Jython and I think Ruby on Rails also give you scriptingin Java. Jython uses Python's syntax, but gives you access to your Java classes. I don't know ayhthing about RoR.

jverda at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 7

> > How do you do the Java equivalent of eval()?

>

> The language provides no support for that.

>

> Reflection lets you call any method, where you

> specify that method's name as a string and go through

> a few other steps to build the proper Method object

> with the proper args.

> (http://java.sun.com/docs/books/tutorial/reflect/) It

> won't let you just evaluate Java source code as a

> script interpreter though. To do that, see

> www.beanshell.org. Jython and I think Ruby on Rails

> also give you scriptingin Java. Jython uses Python's

> syntax, but gives you access to your Java classes. I

> don't know ayhthing about RoR.

I am going to look up PHP/Java myself as I know no Python right offhand (but probably could learn it in about 10,000 years). Tried to figure out reflection but couldn't figure out how to do what in PHP we do like this:

$msg = eval('JButton ' . $nameArray[$i][0] . ' = new JButton("' . $nameArray[$i][1] . '");');

ppowell777a at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...
# 8

> I am going to look up PHP/Java myself as I know no

> Python right offhand (but probably could learn it in

> about 10,000 years).

Python's pretty easy to learn, but if you already know PHP and kind find some tool to let you evaluate PHP from your Java code, and that PHP can in turn make use of your Java classes, then that should be fine.

> Tried to figure out reflection

> but couldn't figure out how to do what in PHP we do

> like this:

>

> > $msg = eval('JButton ' . $nameArray[$i][0] . ' = new

> JButton("' . $nameArray[$i][1] . '");');

>

You can't do that with reflection. You can invoke methods and read and set member variables. If you want to evaluate Java syntax statements at runtime, beanshell is the best tool I know of. There may be others that are better, but I'm not familiar with them.

jverda at 2007-7-8 22:21:57 > top of Java-index,Java Essentials,New To Java...