Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java Techniques to Simulate VB's "Evaluate"

Posted on 2001-06-13
6
Medium Priority
?
221 Views
Last Modified: 2010-03-31
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(name);
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--

0
Comment
Question by:sengsational
6 Comments
 
LVL 1

Expert Comment

by:wgilster
ID: 6187395
Yes, actually you can with the java.lang.reflect package!
0
 
LVL 4

Expert Comment

by:kylar
ID: 6188763
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
0
 
LVL 3

Accepted Solution

by:
dnoelpp earned 100 total points
ID: 6189785
You have two solutions. I tried out both, and both work very well.

1. Use BeanShell: http://www.beanshell.org/

You could do things like:

Interpreter.execute("System.out.println(\"Hello, world!\");");

BeanShell syntax is not exactly the same as Java, but very, very similar.



2. Compile the class yourself and run it.

This is more complicated, and because sun.tools.javac.Main is not a standard Java class you lose 100% pure Java this way, I am afraid.

The idea is: Generate a complete, simple class, put it in a string, and give it as a string reader to the javac compiler. Then load the class with the ClassLoader and invoke the function using reflection.

I used it to interactively build up a GUI till I was satisfied and then serialized it for later using in my project. I was satisfied till I discovered BeanShell. By the way, BeanShell does the same behind the scenes (compiling, loading and invoking).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:dnoelpp
ID: 6189789
Oops, it should be:

Interpreter.eval("  ");

not

Interpreter.execute("  ");

Sorry!
0
 
LVL 1

Expert Comment

by:wgilster
ID: 6191083
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.
0
 

Author Comment

by:sengsational
ID: 6200029
Thanks, all, for your ideas and expertise.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question