User Tools

Site Tools


env:spawning_gams_from_java

Spawning GAMS from Java

Java has a class Runtime that implements spawning of processes using the Exec() method. This is quite trivial to use in applications, but in applets the default security settings don't allow this operation (in general). The problem can be solved by loading the applet from a local drive or by using a signed applet. Similarly if the Java classes are loaded from inside a database, this operation may require additional privileges. For an example see the previous section, where GAMS is called from Oracle using a Java Stored Procedure. The relevant code may look like:

import java.io.File;
 
class RunGAMS {
 
        public static void main(String[] args) {
                System.out.println("Start");
                String[] cmdArray = new String[5];
                cmdArray[0] = "<PATH/TO/GAMS>" + File.separator + "gams";
                cmdArray[1] = "<PATH>" + File.separator + "trnsport.gms";
                cmdArray[2] = "WDIR=<PATH>" + File.separator + "TMP";
                cmdArray[3] = "SCRDIR=<PATH>" + File.separator + "TMP";
                cmdArray[4] = "LO=2";
                try {
                       Process p = Runtime.getRuntime().exec(cmdArray);
                       p.waitFor();
                }
                catch (java.io.IOException e )
                {
                       System.err.println(">>>>" + e.getMessage() );
                       e.printStackTrace();
                }
                catch (InterruptedException e )
                {
                       System.err.println(">>>>" + e.getMessage() );
                       e.printStackTrace();
                }
                System.out.println("Done");
        }
}

Below another example, which avoids problems, if the model has a long screen log (the buffer gets filled and locks the execution), which could happen if GAMS does not write the log to the file like above. This example is based on suggestion made by Edson Valle.

import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
 
class RunGAMS {
 
      public static void main(String[] args) {
              System.out.println("Start");
              String[] cmdArray = new String[5];
              cmdArray[0] = "<PATH/TO/GAMS>" + File.separator + "gams.exe";
              cmdArray[1] = "<PATH>" + File.separator + "trnsport.gms";
              cmdArray[2] = "<PATH>" + File.separator + "tmp";
              cmdArray[3] = "LO=3";
 
              try {
                     Process p = Runtime.getRuntime().exec(cmdArray);
                     BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                     String s = null;
                     while((s=stdInput.readLine()) !=null){
                        System.out.println(s);
                     }
                     p.waitFor();
 
              }
              catch (java.io.IOException e )
              {
                     System.err.println(">>>>" + e.getMessage() );
                     e.printStackTrace();
              }
              catch (InterruptedException e )
 
              {
                     System.err.println(">>>>" + e.getMessage() );
                     e.printStackTrace();
              }
              System.out.println("Done");
      }
}

For an example of GAMS usage from a Java based server environment see Alexander Sokolov, Information environment and architecture of decision support system for nutrient reduction in the Baltic Sea, Department of Systems Ecology, Stockholm University.

env/spawning_gams_from_java.txt · Last modified: 2011/03/01 21:39 by interfaces