sengsational
asked on
Java Techniques to Simulate VB's "Evaluate"
VB and Rexx (both interpreted) had a way to RUN some text as a program statement.
I know you can do similar things, like this:
String name = "BJ" + someStringVar
java.lang.Class c = java.lang.Class.forName(na me);
mbj = (BatchJob)(c.newInstance() );
1) Can you think of other examples where java can build then run something?
But maybe you get some text out of a database or something and you build a valid Java program statement. Now you want to run it. I guess you can't do that in Java because you've already gone to bytecode with it. You'd need to generate bytecode on the fly and make it available to the JVM. 2) I think I've answered my own question, but can you do that in Java?
--Dale--
I know you can do similar things, like this:
String name = "BJ" + someStringVar
java.lang.Class c = java.lang.Class.forName(na
mbj = (BatchJob)(c.newInstance()
1) Can you think of other examples where java can build then run something?
But maybe you get some text out of a database or something and you build a valid Java program statement. Now you want to run it. I guess you can't do that in Java because you've already gone to bytecode with it. You'd need to generate bytecode on the fly and make it available to the JVM. 2) I think I've answered my own question, but can you do that in Java?
--Dale--
Yes, actually you can with the java.lang.reflect package!
Umm, actually I don't believe that you can, using the refection package. For example, if I had a line like this:
String command = "System.out.println("Hello World");";
There is no command that would allow you to execute it, like what sengsational wants. You could make a big hack for it by having it write the string to a file, invoking javac on it and then loading the resultant class into memory using a custom classloader, but that is an extremely long and torturous route.
Kylar
String command = "System.out.println("Hello
There is no command that would allow you to execute it, like what sengsational wants. You could make a big hack for it by having it write the string to a file, invoking javac on it and then loading the resultant class into memory using a custom classloader, but that is an extremely long and torturous route.
Kylar
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oops, it should be:
Interpreter.eval(" ");
not
Interpreter.execute(" ");
Sorry!
Interpreter.eval(" ");
not
Interpreter.execute(" ");
Sorry!
Your right, kylar. But without dynamically compliling as you spoke, it's as close as your going to get in Java.
But if you were pulling the commands out of a database anyway you could make it interpret any non-nested, reflection supported field, method etc. You could put the Object in one field the method in a field, and the parameters could be related in a different table. You could achieve something very close, but yes, you wouldn't be able to put the entire string "System.out.println("Hello , world!");" in one field and interpret it.
Unless of course he want's to parse the string himself, in which, I think he would be better off taking the dynamic compile route.
But if you were pulling the commands out of a database anyway you could make it interpret any non-nested, reflection supported field, method etc. You could put the Object in one field the method in a field, and the parameters could be related in a different table. You could achieve something very close, but yes, you wouldn't be able to put the entire string "System.out.println("Hello
Unless of course he want's to parse the string himself, in which, I think he would be better off taking the dynamic compile route.
ASKER
Thanks, all, for your ideas and expertise.