how to write a .put file in gams and execute the gams file via java?

I create a gams file, let say a.gams. In this file, I write a loop result in to a results.put file. If I execute the a.gams file directly from gams, I can see the results.put file in the prodir folder. However, if I execute the gams via java, I cant file any results.put file. I guess something is wrong with my code, can some one help me? Thanks.

Following is the java code.

package main;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class CallGAMS {
public String cmdArray = new String[5];

public String GetcmdArray(){
return cmdArray;
}
public void setcmdArray(String gamsfilename) {
cmdArray[0] = “C:\GAMS\win64\23.8” + File.separator + “gams.exe”;
// cmdArray[0] = “C:\Program Files\GAMS23.7” + File.separator + “gams.exe”;
cmdArray[1] = “C:\Users\Documents\gamsdir\projdir” + File.separator + gamsfilename;
cmdArray[2] = “WDIR=C:\Users\Documents\gamsdir\projdir” + File.separator + “tmp”;
cmdArray[3] = “SCRDIR=C:\Users\Documents\gamsdir\projdir” + File.separator + “tmp”;
cmdArray[4] = “LO=3”;
}
// modify to read float number
public int GAMSResult() throws IOException {
int resultnumber;
BufferedReader readerMGOProfit = new BufferedReader(new FileReader(“C:\Users\heyanyi\Documents\gamsdir\projdir\tmp\results.txt”));
return resultnumber= readerMGOProfit.read();
}
public void setup(){

System.out.println(“Start GAMS”);
for (String s : cmdArray) {
System.out.println(s);
}

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();
}
int errnr = new int[1];
System.out.println(“GAMS Done”);

}


public static void main(String args) {

CallGAMS CallGAMSMarketClearing = new CallGAMS();
CallGAMSMarketClearing.setcmdArray(“trnsport.gms”);
System.out.println(“Start GAMS for market clearing”);
CallGAMSMarketClearing.setup();
System.out.println(“End of GAMS for market clearing”);


}

}


To view this discussion on the web visit https://groups.google.com/d/msg/gamsworld/-/BafLPn_ZPm8J.
To post to this group, send email to gamsworld@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

Any Exception is Thrown?


2012/7/27 hyy

I create a gams file, let say a.gams. In this file, I write a loop result in to a results.put file. If I execute the a.gams file directly from gams, I can see the results.put file in the prodir folder. However, if I execute the gams via java, I cant file any results.put file. I guess something is wrong with my code, can some one help me? Thanks.

Following is the java code.

package main;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class CallGAMS {
public String cmdArray = new String[5];

public String GetcmdArray(){
return cmdArray;
}
public void setcmdArray(String gamsfilename) {
cmdArray[0] = “C:\GAMS\win64\23.8” + File.separator + “gams.exe”;
// cmdArray[0] = “C:\Program Files\GAMS23.7” + File.separator + “gams.exe”;
cmdArray[1] = “C:\Users\Documents\gamsdir\projdir” + File.separator + gamsfilename;
cmdArray[2] = “WDIR=C:\Users\Documents\gamsdir\projdir” + File.separator + “tmp”;
cmdArray[3] = “SCRDIR=C:\Users\Documents\gamsdir\projdir” + File.separator + “tmp”;
cmdArray[4] = “LO=3”;
}
// modify to read float number
public int GAMSResult() throws IOException {
int resultnumber;
BufferedReader readerMGOProfit = new BufferedReader(new FileReader(“C:\Users\heyanyi\Documents\gamsdir\projdir\tmp\results.txt”));
return resultnumber= readerMGOProfit.read();
}
public void setup(){

System.out.println(“Start GAMS”);
for (String s : cmdArray) {
System.out.println(s);
}

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();
}
int errnr = new int[1];
System.out.println(“GAMS Done”);

}


public static void main(String args) {

CallGAMS CallGAMSMarketClearing = new CallGAMS();
CallGAMSMarketClearing.setcmdArray(“trnsport.gms”);
System.out.println(“Start GAMS for market clearing”);
CallGAMSMarketClearing.setup();
System.out.println(“End of GAMS for market clearing”);


}

}


To view this discussion on the web visit https://groups.google.com/d/msg/gamsworld/-/BafLPn_ZPm8J.
To post to this group, send email to gamsworld@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.



\

Edson Valle
edsoncv@gmail.com


To post to this group, send email to gamsworld@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.

Hello,

in your explanation you say you write to results.put but in your Java-Code you’re trying to read from results.txt (different extensions - put vs txt). That is probably already the error.

I’ve written a simple/naive GDX-Reader/Writer in Java which works good enough for small files. If you/anybody is interested i could extract the necessary bits and put that up on github. :slight_smile:

Best Regards,
Johannes

Am Samstag, 28. Juli 2012 01:22:58 UTC+2 schrieb hyy:

I create a gams file, let say a.gams. In this file, I write a loop result in to a results.put file. If I execute the a.gams file directly from gams, I can see the results.put file in the prodir folder. However, if I execute the gams via java, I cant file any results.put file. I guess something is wrong with my code, can some one help me? Thanks.

Following is the java code.


To view this discussion on the web visit https://groups.google.com/d/msg/gamsworld/-/r3otf-r8U7kJ.
To post to this group, send email to gamsworld@googlegroups.com.
To unsubscribe from this group, send email to gamsworld+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gamsworld?hl=en.