• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Interpret user input as code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a collection of mathematical analysis programs and would like to allow the user to input a valid Java math expression which could then be interpreted by the program as new code. I realize that this may have security issues but the programs are only for my personal use. A snippet below shows what I'm trying to do. A user input of e.g. "x*x" would result in the output of 25 i.e. 5 squared. In Perl I was able to do this quite easily via eval($input).
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

That's what the JEP library does. A GPL'd version is available at http://www.singularsys.com/download/jep-2.4.1-ext-1.1.1-gpl.zip, and examples and documentation are at http://www.singularsys.com/jep/
 
David Lamb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link. I circumvented the problem in C by first taking in the input in Perl (so I guess I didn't really circumvent the problem in C) and writing out a C program via Perl inserting the user input and then compiling/executing via Perl's system(). Pretty clunky - so I'll definitely check out JEP. Thanks again, D.
 
reply
    Bookmark Topic Watch Topic
  • New Topic