This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interpret user input as code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interpret user input as code" Watch "Interpret user input as code" New topic

Interpret user input as code

David Lamb

Joined: Jan 21, 2009
Posts: 2
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).
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

That's what the JEP library does. A GPL'd version is available at, and examples and documentation are at
David Lamb

Joined: Jan 21, 2009
Posts: 2
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.
I agree. Here's the link:
subject: Interpret user input as code
It's not a secret anymore!