Integrating with JAVA Applications

Builds provide downloadable artifacts for integrating your calculations with JAVA (on either the server side or in an Android Build).

Integration is done using the J2V8 project until we natively generate to the Java VM.

“J2V8 is a set of Java bindings for V8. J2V8 focuses on performance and tight integration with V8.”

The generated sample source is using the Maven build system. You can find the sample source here: java-integration-j2v8-example

Build the project with the Java option

In order to have the artifacts you need to check Java in the Build Options. Navigate to Deployment, click Option and check Java, see below:

../_images/integrate-java-build-option.png

Download the Build Artifacts

Once you have done the build, download the build result: navigate into the build (click into it) and open the Java tab:

../_images/integrate-java-download.png

Maven Project Setup

J2V8 is a C++ Java native interface and therefore it is platform dependent. You will need to include the platform specific dependency from a repo like maven central.

On macOS this dependency would be:

<dependency>
    <groupId>com.eclipsesource.j2v8</groupId>
    <artifactId>j2v8_macosx_x86_64</artifactId>
    <version>4.6.0</version>
</dependency>

Give it a try

Assuming you have done the Getting Started tutorial, you should have the Mortgage Project set up and you could easily do the followings.

Set up a test scenario

According to the followings our monthly payment should be 1168.29 (some currency) when the Score=620, T(term)=30 and P(principal) is 200 000.

../_images/integrate-ios-test-case.png

Download and do the maven build

Following the download you could set up a Java main class (we chose the same formulator.bundle package) with the following code:

package formulator.bundle;

import com.eclipsesource.v8.V8Object;
import com.google.gson.JsonObject;

/**
 *
 * @author abc
 *
 */
public class __main__ {

    public static void main(String[] args) {
        try {
            // initialize resources, load Js bundle from fs
            MortgageCalculations mortgageCalculations = new MortgageCalculations();

            // setup parameters
            JsonObject params = new JsonObject();
            params.addProperty(MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT__CLIENT_SCORE, "620");
            params.addProperty(MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT__TERM, "30");
            params.addProperty(MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT__PRINCIPAL, "200000");

            // call calculation
            V8Object result = mortgageCalculations.doCalculation(
                    MortgageCalculations.MORTGAGE_CALCULATIONS__MORTGAGE_PMT_FROM_SCORE,
                    MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT,
                    params);

            // print result
            System.out.println("monthlyPayment = " + result.get(
                    MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT__MONTHLY_PAYMENT));

            // you have to release j2v8 V8Object object yourself if you asked for it
            result.release();

            // if you prefer the result as JsonObject call this
            JsonObject resultAsJsonObj = mortgageCalculations.doCalculationAsJsonObject(
                    MortgageCalculations.MORTGAGE_CALCULATIONS__MORTGAGE_PMT_FROM_SCORE,
                    MortgageCalculations.MORTGAGE_PMT_FROM_SCORE__PMT,
                    params);

            // print result
            System.out.println("result as a JSON object: " + resultAsJsonObj);

            // call close() before releasing "calc" object
            mortgageCalculations.close();
            mortgageCalculations = null;
        } catch(Exception ex) {
            System.out.println(ex);
        }
    }

}

Run the project

Do the following to run the maven project

mvn clean install
mvn exec:java -D"exec.mainClass"="formulator.bundle.__main__"

You should see the following result:

../_images/integrate-java-mvn-output.png