| Author |
Running code written in a string
|
colin shuker
Ranch Hand
Joined: Apr 11, 2005
Posts: 712
|
|
Hi,
I heard in Java 6, it was possibly to write code in a String, then execute it, eg:
Is this possible?
Thanks
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
Is this possible?
It depends.... You have two options (which are close, but requires work).
You can use the "java scripting api", which allows you to call out to scripting engines. By default, it only supports Javascript, which is close, but isn't exactly Java. However, you can load drivers for it, and there does seem to be some that "look and feel" like java.
You can use the "java compiler api", which allows you to compile java. So, you can use it to compile and run dynamically generated code.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Nitesh Kant
Bartender
Joined: Feb 25, 2007
Posts: 1638
|
|
Java Scripting api provide supports for third party JSR 223 implementations. Jython provides a JSR 223 implementation and hence can be executed using the JDK apis.
Jython can also execute java code, so this means the example you have given can be executed. For more information look here
|
apigee, a better way to API!
|
 |
 |
|
|
subject: Running code written in a string
|
|
|