• 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

executing java expression from a text file!

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

This might be far fetched, but i really need to know if its possible to read a java expression from a text file and execute it.
Example : width = imgwidth - 100;
Where the variables r defined and known by the class that read this line.

Thanks in advance!
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

that is possible. In the general case it is quite hard. For a simple construct like your example there is an easy way, however:
  • read the file as a properties file using java.util.Properties
  • get the property "width"
  • replace imgwidth by x using String#replaceAll
  • use jfunction to evaluate the resulting String: width=Terms.evaluate(resultingString, imgwidth);


  • Kai
     
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    beanshell?
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    There are a number of libraries that allow you to do that. Google for JEP or JEL. JEP builds an expression tree and interprets it using values, which makes it a bit slow. JEL compiles the expression, which is much faster, but can be tricky to handle to situations where you have multiple classloaders. For the ultimate Java integration, look for Javassist, which lets you build your own Java classes from Strings of code - very cool.
     
    Mike Mass
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would like ot thank each of you for your contribution, particularly Mr Dittmer. JEP is the answer to what i have in mind, since i will integrate it to an ant task, speed is not an issue.

    Thanks again guys,

    Mike
    [ September 08, 2005: Message edited by: Mike Mass ]
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic