Calling Java
Add uses 'mosjvm' to the top of your model then you can call any static Java method by passing its full class and method name to one fo the jvmcall<type> functions, where <type> represents the return type of the function, for example:
model myModel uses 'mosjvm' writeln('Integer return value=', jvmcallint('com.fico.MyClassName.myIntMethod')) writeln('String return value=', jvmcallstr('com.fico.MyClassName.myStringMethod')) writeln('Real return value with two parameters=', jvmcallreal('com.fico.MyClassName.myRealMethod', 5.7, true)) writeln('Any object return value=', jvmcallobj('com.fico.MyClassName.myObjMethod')) end-model
You can call jvmcallvoid if your Java method doesn't return anything. Any number of method arguments may be specified, from the following types:
- boolean (translates to Java type: boolean)
- integer (translates to Java type: int)
- real (translates to Java type: double)
- string (translates to Java type: java.lang.String)
- text (translates to Java type: java.lang.String)
- jvmobject
- array of boolean (translates to Java type: boolean[])
- array of integer (translates to Java type: int[])
- array of real (translates to Java type: double[])
- array of string (translates to Java type: java.lang.String[])
- array of text (translates to Java type: java.lang.String[])
- array of jvmobject (translates to Java type: java.lang.Object[])
Where an argument is an array, it must be indexed by a Mosel range starting from 0, e.g.:
model myModelAr uses 'mosjvm' declarations MyArray: array(range) of string end-declarations MyArray(0) := "zero" MyArray(1) := "one" MyArray(2) := "two" jvmcallvoid("com.fico.MyClassName.myMethodTakingArrayOfString", MyArray) end-model
You can call instance methods on a Java object by passing the object reference as the first parameter and method name as second, e.g.:
model myModel uses 'mosjvm' ! Call static method to get object sysprops := jvmcallobj('java.lang.System.getProperties') ! Call non-static method on that object writeln('java.version property value = ',jvmcallstr(sysprops, 'getProperty', 'java.version')) end-model