• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running code written in a string

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I heard in Java 6, it was possibly to write code in a String, then execute it, eg:

Is this possible?
Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic