Hi, thanks for your detailed reply.
And I have write a java class according to your instruction,however it can
not give the right output. I test with the spearfish60 data and the
r.infoprocess.
Attach my code, can you check it ?
Thanks anyway.
2010/3/5 Glynn Clements <[email protected]>
>
> maven apache wrote:
>
> > > > So, what is the problem?
> > >
> > > With java.lang.Runtime.exec(String cmd, String[] envp), the envp
> > > parameter specifies the entire environment for the process (i.e. it
> > > doesn't add the strings to the current process' environment). The
> > > result is that HOME isn't set, which means that $HOME/.grassrc6 cannot
> >
> > I can not understand, my grass command is "grass64 -text
> > /home/kk/grass/GrassDataBase/spearfish60/PERMANENT", so I have specified
> the
> > location and mapset, the gisrc file is required at any times?
>
> The grass64 script creates a copy of $HOME/.grassrc6 and sets GISRC to
> refer to the copy. If $HOME/.grassrc6 doesn't exist, the copy won't
> exist either, and the first-use screen will be shown.
>
> > > Beyond that, I'd suggest that you abandon the GRASS_BATCH_JOB approach
> > > and set up the environment yourself, without using the grass64 script
> > > at all.
> >
> > God!, in fact I tied to set up the env vars by myself, however many
> people
> > suggest me to use the grass_batch _job for easily using. Now I can not
> make
> > a choice.
> > Here I can show you my requirements, can you give me some advise?
> > ---my requirements----
> >
> > I am working with the Web Processing Service, and I want to wrap some
> grass
> > operation ( for example the buffer , the interpolation and ect...) as
> > processes in the web service, And my web application is developed by
> java. I
> > changed my work platform from win xp to ubuntu beacuase of the unexpected
> > errors when I call the grass command(Actually I can not do the
> interpolation
> > operation in the windows-xp command line, but it can work in the Ubuntu
> > shell).
> > I have a general idea:
> > Create a class named GrassManager, and it contain a method to set the env
> (a
> > gisrc file for each user) and a method to call the concrete grass command
> > (for example r.info .....) basing on the set env.
> >
> > However I need to solve the following problems:
> > 1) how to set up the grass env in java?
>
> As for what variables need to be set, refer to:
>
> 1. My answer to your previous post in the thread "grass env".
>
> 2. The thread "can I access mapset outside of grass, by using python?"
> by Nikos Alexandris.
>
> 3. The variables.html file in the GRASS distribution.
>
> You first need to make a copy of the existing environment, then add or
> replace any environment settings required by GRASS.
>
> > 2) after set up the env ,how to call the grass command in java, because
> it
> > reffed to command interpreter and separate executable, I do not know the
> > grass command is either?
>
> Once you have created the environment, I suggest using:
>
> Java.lang.Runtime.exec(String[] cmdarray, String[] envp)
> or:
> Java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)
>
> for each GRASS command. The versions which take the command as a
> single string are problematic if an argument contains spaces.
>
> > 3) As a web application, I should consider the multiple user situation,
> > create a unique gisrc file for each user?
>
> Ideally, you should use a separate $GISRC file and a separate mapset
> for each session. If you use a single mapset, the WIND and VAR files
> will be shared by all sessions. The VAR file probably won't be an
> issue (it only holds the database connection information). You can get
> around the WIND issue by setting WIND_OVERRIDE to the name of a
> specific region (created with e.g. "g.region save=..."), which will be
> used instead of the WIND file.
>
> But the main thing for a web application is to validate all inputs.
> Don't pass values from form fields directly to commands, as the code
> uses fixed-size buffers extensively and doesn't perform bounds
> checking. Also, shell scripts (and some compiled programs which use
> system() or popen()) may misbehave if arguments contain shell
> metacharacters.
>
> --
> Glynn Clements <[email protected]>
>
public class GrassMa {
private String grassBase;
private String grassRc;
private String ldLibraryPath;
private String pathSerp;
private String[] env;
private String uid;
public GrassMa(String uid) {
this.uid=uid;
setEnv();
}
private void setEnv() {
String os=System.getProperty("os.name");
if(os.contains("win")) {
this.pathSerp=";";
} else{
this.pathSerp=":";
}
//get the config from the file
grassBase="/usr/lib/grass64";
ldLibraryPath=grassBase+File.separator+"lib";
String oldGrassRc="/home/kk/grass/de.gisrc";
String path=grassBase+File.separator+"bin"+pathSerp+grassBase+File.separator+"scripts"+pathSerp+"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kk/ProgramFiles/jdk1.6.0_17/bin:/home/kk/ProgramFiles/apache-maven-2.2.1/bin";
//create gisrc file for each user,copy the Default gisrc file
//here for test ,just use the default gisrc
//GISDBASE: /home/kk/grass/GrassDataBase
//LOCATION_NAME: spearfish60
//MAPSET: PERMANENT
//GRASS_GUI: text
grassRc="/home/kk/grass/de.gisrc";
//set the env
if(grassRc!=null) {
Map<String , String> envMap=new HashMap<String, String>();
envMap.put("PATH", path);
envMap.put("GISBASE",grassBase);
envMap.put("GISRC",grassRc);
envMap.put("LD_LIBRARY_PATH", ldLibraryPath);
this.env=EnvironmentUtils.toStrings(envMap);
System.out.println(envMap);
} else {
throw new RuntimeException("gis rc is not ");
}
}
public String run(String cmd) {
if(this.env==null) {
throw new RuntimeException("The grass env is not set correctly....");
}
Runtime rt=Runtime.getRuntime();
try {
cmd="/bin/sh -c " + cmd;
String[] exe={"/bin/sh -c",cmd};
System.out.println(exe);
Process p1=rt.exec(exe,env);
new GrassThread("out", p1.getInputStream()).start();
new GrassThread("error", p1.getErrorStream()).start();
int exitVal = p1.waitFor();
System.out.println("ExitValue: " + exitVal);
return null;
}
public static void main(String[] args) {
GrassMa gm=new GrassMa("300");
gm.run("r.info map=roads");
}
}
_______________________________________________
grass-user mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-user