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();
	}
}

© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.