Solved

Java Techniques to Simulate VB's "Evaluate"

Posted on 2001-06-13
6
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
check java version using powershell 13 469
ejb entity bean example 2 57
Selenium findElement(By.classname  identifier 15 35
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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