0

hello I want to spawn gams from java I have installed gams and here is the code gams is an application for modelling I want to call it from java and I am sure the path is exact what should i do to correct it? it gives me exception EXCEPTIONS:

    Exception in thread "main" java.lang.NullPointerException
            at java.lang.ProcessBuilder.start(ProcessBuilder.java:441)
            at java.lang.Runtime.exec(Runtime.java:593)
            at java.lang.Runtime.exec(Runtime.java:466)
            at gams.RunGAMS.main(RunGAMS.java:27)

Java Result: 1

here is the code which I changed from this link

package gams;

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] ="C:"+File.separator+"Program Files"+File.separator+"GAMS23.7" + File.separator +"gams.exe";
              cmdArray[1] ="C:"+File.separator+"Documents and Settings"+File.separator+"Parsa"+File.separator+"My Documents"+File.separator+"gamsdir"+File.separator+"projdir" + File.separator +"trnsport.gms";
              cmdArray[2] ="C:"+File.separator+"Documents and Settings"+File.separator+"Parsa"+File.separator+".nbi"+ 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");
      }
}

3 Answers 3

7

Look at this code:

String[] cmdArray = new String[5];
cmdArray[0] = ...;
cmdArray[1] = ...;
cmdArray[2] = ...;
cmdArray[3] = ...;

You're creating an array of five elements but only populating four of them.

Try changing the array size to 4, if you only want four arguments.

Sign up to request clarification or add additional context in comments.

Comments

2

A way to avoid creating array which is the wrong sized is to use a one liner.

String sep = File.separator;
String[] cmdArray = {
          "C:"+sep+"Program Files"+sep+"GAMS23.7" + sep +"gams.exe",
          "C:"+sep+"Documents and Settings"+sep+"Parsa"+sep+"My Documents"+sep+"gamsdir"+sep+"projdir" + sep +"trnsport.gms",
          "C:"+sep+"Documents and Settings"+sep+"Parsa"+sep+".nbi"+ sep+"tmp",
          "LO=3"};

or to make it shorter.

String[] cmdArray = "C:/Program Files/GAMS23.7/gams.exe,C:/Documents and Settings/Parsa/My Documents/gamsdir/projdir/trnsport.gms,C:/Documents and Settings/Parsa/.nbi/tmp,LO=3".replaceAll("/", File.separator).split(",");

Comments

0

There is a space in your path , just look at following lines,

cmdArray[0] ="C:"+File.separator+"Program Files"+File.separator+"GAMS23.7" + File.separator +"gams.exe";  

Here in this line you have defined "Program Files" which contans a space, so basically your variable cmdArray[0] is storing only holding value like = C:\Program , it is terminating after the space. Same way it is happening with other array elements , so it is giving a Nul Pointer Exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.