File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes executing java expression from a text file! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "executing java expression from a text file!" Watch "executing java expression from a text file!" New topic
Author

executing java expression from a text file!

Mike Mass
Greenhorn

Joined: Aug 13, 2004
Posts: 12
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!
Kai Witte
Ranch Hand

Joined: Jul 17, 2004
Posts: 354
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


    Kai Witte's business website Kai Witte's private homepage Mock exam / preparation kit reviews
    Michael Lloyd Lee
    Greenhorn

    Joined: Sep 07, 2005
    Posts: 22
    beanshell?


    Please please please use code tags!<br /> <br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/" target="_blank" rel="nofollow">Java API</a> - <a href="http://java.sun.com/docs/books/tutorial/index.html" target="_blank" rel="nofollow">Java Tutorials</a>
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 39535
        
      27
    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.


    Ping & DNS - updated with new look and Ping home screen widget
    Mike Mass
    Greenhorn

    Joined: Aug 13, 2004
    Posts: 12
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: executing java expression from a text file!
     
    Similar Threads
    How can I read message from the text file and put it into MQseries using java
    is it possible in java?
    reverse the text file and write
    Java Regexp matching - not working in loop
    how to write and read excel file