你好,我想从java中生成gams,我已经安装了gams,这是代码
gams是用于建模的应用程序,我想从Java调用它,我确定路径是正确的,我应该怎么做才能更正它?它给了我例外
例外情况:
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
这是我从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");
}
}
最佳答案
看下面的代码:
String[] cmdArray = new String[5];
cmdArray[0] = ...;
cmdArray[1] = ...;
cmdArray[2] = ...;
cmdArray[3] = ...;
您正在创建一个包含五个元素的数组,但仅填充其中四个。
如果只需要四个参数,请尝试将数组大小更改为4。