Math Parser for Java

JbcParser math parser

JbcParser is a mature, well tested Java formula parser for Java programmers!

JbcParser is a Math Parser Library for Java. JbcParser evaluates mathematical expressions given as strings at runtime. JbcParser works with JDK 5 and later versions. Java 8 has a change in it’s implementation of String.substring method which impacts parser software performance in general. JbcParser is specially optimized to work around this JDK 8 change and it runs faster than ever.
This Math parser comes as an easy to use, simple class API. JbcParser has predefined functions. Users can create custom functions/variables. Constant expressions are eliminated for repeated tasks. Analytic operators that JbcParser supports are +, -, /, *, ^. Logical operators supported are =(equals), & (and), |(or), !(not), <>(not equals), <=(less than or equals), >=(greater than or equals) The expression can contain (, {, or [ as parenthesis. Functions are in the form of: f(x, y, z,…). Function parameters are not calculated until they are actually needed. This makes it easier to avoid division by zero or overflow errors when used along with IF() function. List of predefined functions is available in the documentation. Java source code is included.
An example of a simple expression is : LN(X)+SIN(10/2-5)
When parsed, this expression will be represented as: since the SIN(10/2-5) is in fact SIN(0) which is a constant and is 0.
Thus, in a loop, if you change the value of X and ask for the value of the expression, it will be evaluated quite fast since SIN(10/2-5) is not dependent on X.
X and Y are predefined variables. You can remove them or you can create your own variables as needed. There are many predefined mathematical functions listed in the documentation. You can create your functions as needed. IF logic is implemented through a predefined IF(A,B,C) function. Similar logical functions can be created as needed.

JbcParser Documentation

JbcParser – Java Math Parser documentation is generated by the Javadoc tool from the source code. You can find other examples here as well. If you have questions, please post it using the comment form below this page.

JbcParser Evaluation Download

The evaluation version of this math parser library can be downloaded here:
JbcParser Evaluation Download
There is a time bomb in the evaluation downloaded. After a period of time it will stop functioning. Please do not ship the evaluation version with your applications. If you find that the download has already expired, it is our bad. Let us know ( and we will update it.

Example of User Defined Variable in Math Parser

This is a small code snippet that shows how to define and use user defined variables in JbcParser.

  IMathParser parser = MathParserFactory.create();
  parser.setExpression("SIN( X / Y  )+MYVARIABLE");
  parser.setVariable("MYVARIABLE", 6); //user defined variable

Undefined Variables in the Expression

Sometimes it is not possible to predefine all possible variables that a user can use in an expression. In that case, JbcParser allows you to set an implementation of IVariableResolver interface to provide values for variable that were not previously defined:

 *  IVariableResolver is implemented by the user and used in
 *  IMathParser.setVariableResolver(IVariableResolver) to enable the
 *  Math Parser allow variables that are not defined before parse time.
 *  if set, IVariableResolver will be used to return the values of variables
 *  at evaluation time. This is typically needed when the problem domain
 *  is too large to define all possible variables ahead of time.
 *  When IMathParser.setVariableResolver is set, parser will tolerate
 *  undefined variables at parse time and it will invoke IVariableResolver
 *  to retrieve variable values at evaluation time.
public interface IVariableResolver {
	 * getValue function is implemented by the user and it is called by
	 * the parser to get the value of an undefined variable at evaluation time.
	 * @param parser - current parser instance
	 * @param varName - variable whose value is being asked for
	 * @return value of the variable named varName
	public double getValue(IMathParser parser, String varName);

Here is an example of using IVariableResolver:

IMathParser p = MathParserFactory.create();
IVariableResolver varRes = new IVariableResolver(){
  // Implement getValue:
  public double getValue(IMathParser parser, String varName) {
    System.out.println("Variable resolver called for: "+varName);
      return 5;
    throw new RuntimeException("Unknown variable name: "+varName);
p.setVariableResolver(varRes); // Set the variable resolver
p.setExpression("3+ K "); // K was not defined, variable resolver will get called for it.

Example of User Defined Function in JbcParser

Below is a small class that demonstrates the use of user defined functions in JbcParser.

package com.bestcode.mathparser.test;
import com.bestcode.mathparser.IMathParser;
import com.bestcode.mathparser.IParameter;
import com.bestcode.mathparser.MathParserFactory;
import com.bestcode.mathparser.OneParamFunc;
 * This class demonstrates the use of user defined functions in JbcParser.
public class UDFExample {
  static public void main(String[] args){
      IMathParser p = MathParserFactory.create();
      //Introduce a user defined function:
      p.createFunc("FACT", new Fact());
    }catch(Exception e){
   * A static function that you have.
   * @param x - should be integer
   * @return factorial x.
  static public double factorial (double x){
      return x*factorial(x-1);
      return 1;
 * A user defined function implementation for the JbcParser.
 * In order to implement a user defined function for JbcParser, you need
 * to extend one of OneParamFunc or TwoParamFunc classes or simply implement
 * IFunction interface.
 * Documentation contains the descriptions for these classes and interfaces:
class Fact extends OneParamFunc {
  public double run(IParameter[] p){
    return UDFExample.factorial(p[0].getValue());

JbcParser License

Licensing is per developer. Once you purchase the component, you can ship the binaries royalty free with your applications. The source code is provided so that you can change to fit your needs. You may read the JbcParser license here.

Purchasing JbcParser Math Parser Component for Java

You can pay with credit card and download JbcParser immediately from our online store for only $29.95. The download includes Java source code. Upgrades are free for registered users. Licensing is per developer. You can deploy the the component royalty free with your applications as many times as you want. Site license allows any number of developers use the component at your development site. Site License is $290.95. Site licenses can be purchased here.

Online Order Form