Those environment variables will only be set while the process (the .bat
file) is executing, so there is not a way to directly retrieve them after
the process completes. You could have the .bat file write out the
information (corresponding to the environment variables) to standard
output and have the Java program include "RETURNSTDOUT" on the PROCESS
START request, and then have the program parse through the standard
output. Here is an example using a test .bat file:
echo off
set RES_DIR1=MyDirectory1
echo RES_DIR1=%RES_DIR1%
set RES_DIR2=MyDirectory2
echo RES_DIR2=%RES_DIR2%
Here is a sample Java program that executes this .bat file and parses
through its standard output:
import com.ibm.staf.*;
import java.util.*;
public class processenv
{
public static void main(String[] args)
{
STAFHandle handle = null;
try
{
handle = new STAFHandle("Test");
}
catch(STAFException e)
{
e.printStackTrace();
System.exit(1);
}
String request = "START SHELL COMMAND C:/temp/test.bat
RETURNSTDOUT WAIT";
STAFResult result = handle.submit2("local",
"PROCESS",
request);
STAFMarshallingContext mc =
STAFMarshallingContext.unmarshall(result.result);
Map processCompletionMap = (Map)mc.getRootObject();
String processRC = (String)processCompletionMap.get("rc");
if (!processRC.equals("0"))
{
System.out.println(
"ERROR: Process RC is " + processRC + " instead of 0.");
System.exit(1);
}
List returnedFileList = (List)processCompletionMap.get(
"fileList");
Map stdoutMap = (Map)returnedFileList.get(0);
String stdoutRC = (String)stdoutMap.get("rc");
if (!stdoutRC.equals("0"))
{
System.out.println(
"ERROR retrieving process Stdout data. RC=" + stdoutRC);
System.exit(1);
}
String stdoutData = (String)stdoutMap.get("data");
String[] reslines =
stdoutData.split(System.getProperty("line.separator"));
for (int i = 0; i < reslines.length; i++)
{
System.out.println("line" + i + ": " + reslines[i]);
}
}
}
Here is an example of running this Java program:
$ java processenv
line0:
line1: c:\build>echo off
line2: RES_DIR1=MyDirectory1
line3: RES_DIR2=MyDirectory2
David Bender
STAF/STAX Development
8-1268 (512-838-1268)
IBM Austin Bldg. 903-5B002
Internet: [EMAIL PROTECTED]
[EMAIL PROTECTED]
Sent by: [EMAIL PROTECTED]
03/06/2008 05:09 AM
To
<staf-users@lists.sourceforge.net>
cc
Subject
[staf-users] Reading an environment variable.
Hi All,
I have a batch script which sets some environment
variables; for
example,
== test.bat ======
rem Steps to execute tests
.
.
.
.
set RES_DIR=%TRDIR%\%OS%\%JAVA_VERSION%
=========================================
Using STAF Java APIs to execute the batch file:
STAFResult result = handle.submit2(ip,"PROCESS", "START
SHELL
COMMAND C:\\test.bat WAIT" );
Is there a way that I can retrieve variable RES_DIR to my
Java
program?
Any help appreciated.
Many thanks,
Saneesh Joseph
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users