Solved

Java Techniques to Simulate VB's "Evaluate"

Posted on 2001-06-13
6
175 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now