Wikk Wikk - 1 year ago 130
Java Question

Loading GAMS Java API in JRuby

I'm working on a Java/JRuby project which needs to be able to be able to interact with GAMS. I know we can use the Java API, but I would really like to be able to access it using JRuby if possible, since we're hoping to eventual add a DSL and some other complexity I'm not really excited about having to implement in pure Java.

Following the official Java API documentation for GAMS, I have downloaded and setup everything necessary to run GAMS from the command line, but I can't figure out how to include the GAMS directory in LD_LIBRARY_PATH and still run JRuby irb. When I run

export LD_LIBRARY_PATH=/home/wikk/Downloads/gams24.0_linux_x64_64_sfx

Then try to run irb with JRuby, I get

jruby: /home/wikk/Downloads/gams24.0_linux_x64_64_sfx/libstdc++.so.6: version 'GLIBCXX_3.4.15' not found (required by jruby)

I think this is what the documentation is asking me to do to run a Java program that calls the API, is there maybe some way to set LD_LIBRARY_PATH in irb, but before importing all the Java class files? I can do this successfully if I don't set LD_LIBRARY_PATH, but then GAMS tells me it can't find the main program when I try to create a new GAMSWorkspace object:

irb(main):002:0> ws = GAMSWorkspace.new
Java::ComGamsApi::GAMSException: could not find a GAMS system directory from
your environment variable, please set up properly before running a program!
from com.gams.api.GAMSWorkspace.verifySystemDirectory(GAMSWorkspace.java:335)

Am I doing this wrong? or does the API require some Java feature that isn't implemented in JRuby?

Answer Source

Finally came back to this problem, got it working through some trial and error. I also needed to run jruby with the -J-Djava.library.path=[GAMSDIR]/apifiles/Java/api flag, and add [GAMSDIR]/apifiles/Java/api/GAMSJavaAPI.jar to the classpath.

Once this is all in place, you can run gams models from ruby scripts:

    import com.gams.api.GAMSWorkspace
    import com.gams.api.GAMSJob
    import com.gams.api.GAMSVariable
    import com.gams.api.GAMSVariableRecord
    import com.gams.api.GAMSWorkspace

    ws = GAMSWorkspace.new

    j1 = ws.addJobFromGamsLib('trnsport')


    j1.out_db.get_variable('x').each_entry do |rec|
      puts "x(#{rec.get_keys[0]}, #{rec.get_keys[1]}): level = #{rec.get_level},   marginal = #{rec.get_marginal}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download