Solved

Java Techniques to Simulate VB's "Evaluate"

Posted on 2001-06-13
6
183 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 25 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 134
Where to store the queries for modification of table 4 54
Arrays.asList  VS  ArrayList 4 44
tomcat not starting 6 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now