Examples
Passing values to a Java method
This example demonstrates calling a Java method and returning a value to Mosel.
model MosJvmExample1
  uses "mosjvm"
  parameters
    SRC_VALUE=5
  end-parameters
  writeln(SRC_VALUE, "*2=",
    jvmcallint("com.fico.examples.MathOperations.multiply", SRC_VALUE, 2))
  if jvmstatus=false then
    setmatherr("Java exception: "+ jvmgetexceptionclass)
  end-if
	
end-modelWhere the MathOperations class is defined as follows:
package com.fico.examples;
public class MathOperations {
  public static int multiply(int v1,int v2) {
    return v1*v2;
  }
}Calling a void Java method
This example demonstrates calling a Java method and displaying a value to the Mosel output stream.
model MosJvmExample2
  uses "mosjvm"
  parameters
    SRC_VALUE=5
  end-parameters
  jvmcallvoid("com.fico.examples.MathOperations.multiplyAndDisplay", SRC_VALUE, 2)
  if jvmstatus=false then
    setmatherr("Java exception: "+jvmgetexceptionclass)
  end-if
	
end-modelWhere the MathOperations class is defined as follows:
package com.fico.examples;
public class MathOperations {
  public static void multiplyAndDisplay(int v1,int v2) {
    System.out.println("The result of "+v1+"*"+v2+" is "+(v1*v2));
  }
}Passing an array to a Java method
This example demonstrates passing an array to a Java method and returning a value to Mosel.
model MosJvmExample3
  uses "mosjvm"
  declarations
    MyData: array(range) of real
    result: real
  end-declarations
  ! Create some data
  forall(x in 0..5000) MyData(x) := x*5
  ! Calculate sum
  result := jvmcallreal("com.fico.examples.MathOperations.sumOfArray", MyData)
  if jvmstatus=false then
    setmatherr("Java exception: "+jvmgetexceptionclass)
  end-if
  ! Print result
  writeln("Sum of values = ", result)
	
end-modelWhere the MathOperations class is defined as follows:
package com.fico.examples;
public class MathOperations {
  public static double sumOfArray(double[] values) {
    double tot = 0;
    for (double v : values) {
      tot += v;
    }
    return tot;
  }
}Calling a Java method that returns an Object
This example demonstrates calling a Java method that returns an Object
model MosJvmExample2
  uses "mosjvm"
  declarations
    o: jvmobject
  end-declarations
  writeln("Creating file")
  o := jvmcallobj("com.fico.examples.FileOperations.createFile")
  if jvmstatus=false then
    setmatherr("Java exception: "+jvmgetexceptionclass)
  end-if
	
  writeln("Deleting file")
  jvmcallvoid("com.fico.examples.FileOperations.deleteFile", o)
  if jvmstatus=false then
    setmatherr("Java exception: "+jvmgetexceptionclass)
  end-if
end-modelWhere the FileOperations class is defined as follows:
package com.fico.examples;
import java.io.*;
public class FileOperations {
  public static File createFile() throws IOException {
    File f = (new File("testfile.txt")).getAbsoluteFile();
    try (PrintWriter pw = new PrintWriter(new FileWriter(f))) {
      pw.println("Hello World");
    }
    return f;
  }
  public static void deleteFile(File f) throws IOException {
    if (!f.delete()) throw new IOException("Failed to delete file: "+f);
  }
} 
