Initializing help system before first use

Passing single values from a Mosel model to a Java method


Description: Demonstrates calling a Java method from a Mosel model, passing in a string and an integer, and receiving a string. You must compile the Java file before running the example - e.g. javac PassValues.java
File(s): passvalues.mos, PassValues.java


passvalues.mos
(!*******************************************************
   file passvalues.mos
   ```````````````````
   a model that demonstrates passing values from Mosel to a Java method

   This example requires an installation of Java, see
   the manual 'mosjvm Module for Mosel' for
   compatible versions and setup instructions.
   
   The compiled PassValues.class file must be in the current working
   directory.

   (c) 2016 Fair Isaac Corporation
       author: J. Farmer, 2016
  *******************************************************!)


model myModel
  uses 'mosjvm'
  
  declarations
    classAndMethodName = "PassValues.repeatValue"
    valueToRepeat = "hello "
    numRepetitions = 3
  end-declarations
  
  ! Abort model if we encounter a Java exception
  setparam('jvmabortonexception',true)
  
  ! Tell Java to look for classes in work directory
  setparam('jvmclasspath',getparam('workdir'))

  ! PassValues.repeatValue(string A, int N) is a Java function that returns string A repeated N times
  writeln('Repeated string: ', jvmcallstr( classAndMethodName, valueToRepeat, numRepetitions ))
end-model

PassValues.java
/**
 * Example Java class, containing a method that returns a string based on arguments passed to it
 *
 * @author  J.Farmer, (c) Fair Isaac Corporation, 2016
 **/
public class PassValues {
	public static String repeatValue(String value, int nRepetitions) {
		StringBuilder result = new StringBuilder();
		for (int i=0;i<nRepetitions;i++) {
			result.append(value);
		}
		return result.toString();
	}
}