Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Techniques to Simulate VB's "Evaluate"

Posted on 2001-06-13
6
Medium Priority
?
216 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 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

722 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