FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Returning an object from a Java method

Description
Demonstrates calling a static Java function that returns a complex object.

mosjvm_returningobjects.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
passarray.mos[download]





returnobject.mos

(!*******************************************************
   file returnobject.mos
   `````````````````
   a model that demonstrates handling a Java object within Mosel

   This example requires an installation of Java, see
   the manual 'mosjvm Module for Mosel' for
   compatible versions and setup instructions.
   
   (c) 2017 Fair Isaac Corporation
       author: J. Farmer, 2017
  *******************************************************!)


model myModel
  uses 'mosjvm','mmsystem'
  
  declarations
    fileobj: jvmobject
    tempfile: text
  end-declarations
 
  ! Abort model if we encounter a Java exception
  setparam('jvmabortonexception',true)

  ! Create a temporary file from Java, using a public static method in the standard java.io.File class
  fileobj := jvmcallobj("java.io.File.createTempFile","xpress-example-",".dat")

  ! fileobj now contains reference to a java.io.File object representing our temporary file
  writeln("Java created temporary file ",fileobj)
  
  ! Store path in Mosel (as java.io.File.toString() returns path)
  tempfile := text(fileobj)
  
  ! Delete file from Mosel
  fdelete(tempfile)
  if getsysstat<>0 then
    writeln("FAILED to dete temporary file ",tempfile)
  else
    writeln("Mosel deleted temporary file ",tempfile)
  end-if
end-model

Back to examples browserPrevious exampleNext example