GRASS and Java: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(prettified; still incomplete)
(cleanup)
Line 59: Line 59:
<source lang="java">
<source lang="java">
String cmd="g.version";
String cmd="g.version";
String[] exe = { "bash", "-c", cmd };
String[] exe = { "bash", "-c", cmd };
String[] env={"GISBASE=..","GISRC="".......};  
String[] env={"GISBASE=..","GISRC="".......}; // the environment variables which you set in the step 2.
 
// the environment variables which you used in the Process
p=Runtime.getRuntime.exec(exe,env);
p=Runtime.getRuntime.exec(exe,env);
</source>
</source>

Revision as of 14:04, 24 March 2010

ADD MORE PLEASE

Calling GRASS from JAVA:

Define:

       public String run(String[] cmd)  {
       ...
                       Process p1=rt.exec(cmd, env);

and use:

       gm.run({"r.info", "map=roads"});

Examples

Installation

It seems that calling grass in JAVA by using java.Lang.Runtime.exec() can be implemented easily in Linux platform (tested successfully on Ubuntu 9.10).

First install the grass use the apt-get:

 sudo apt-get install grass  # it will install the latest stable version

After the installation completed you can:

 #Test it in the terminal without JAVA (make sure that you have a GRASS database installed):

Open a terminal and enter:

 grass64

The following the instruction in the screen or check here.

Test the environment settings

Test using GRASS command directly in the terminal:

Set the environment (we assume GRASS being installed under /usr/lib/grass64):

export GISBASE=/usr/lib/grass64
export GISRC=/home/kk/gisrc
export LD_LIBRARY_PATH=$GISBASE/lib
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts

Then try some GRASS command directly, for example:

g.version
g.gisenv
...

Now call GRASS in JAVA if the above test 2 passed.

Just use the Runtime.getRuntime.exec(String cmd,String[] env):

String cmd="g.version";

String[] exe = { "bash", "-c", cmd };
String[] env={"GISBASE=..","GISRC="".......};  // the environment variables which you set in the step 2.
p=Runtime.getRuntime.exec(exe,env);