I'm having two files that I've to invoke from a java program:
- a batch file that sets the environment.
- an exe file
For running the exe, I need to have some environment variables to be set up, which am doing it with the batch file.
set MSDEV_HOME=C:\Program Files\Microsoft Visual Studio 8\VC
set FOO_ROOT=D:\UGS\Support_Dev\2005SR1
set FOO_DATA=X:
call %FOO_DATA%\FOO_profilevars
set FOO_BIN=B:
set FOO_LIB=L:
set FOO_INCLUDE=I:
FOO_profilevars
in the above batch file is another batch file(this also merely sets the environment) that exists on a different hard drive.
Once the environment is set, I'll be calling the exe.
For some reasons, I've to do this separately - i cannot club these two in a another batch file or something and get the things done.
I tried executing:
ProcessBuilder pb = new ProcessBuilder("D:\\newlogin\\setup.bat");
Process p = pb.start();
int exitValue = p.waitFor();
Map<String, String> env = pb.environment();
System.out.println("exitStatus > " + exitValue);
ProcessBuilder pb2 = new ProcessBuilder("d:\\newlogin\\tcelogin.exe",
"Eid123", "Eid123");
Process p2 = pb2.start();
int exitValue2 = p2.waitFor();
Map<String, String> env2 = pb2.environment();
System.out.println("exitStatus > " + exitValue2);
This is not working - may because the environment that is set in the first process is not used while executing the second process. Is there any way that I execute a file in the environment that is set runtime.
Update: Why separately means: 1. I've some 70env that needs to be set. So, a batch file has been preferred. 2. The EXE file will return data that needs to be processed.
...
System.out.println("Process completed");
BufferedReader reader = null;
int exitValue = p.exitValue();
//System.out.println("Exit Value" + exitValue);
if(exitValue == 0)
{
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
}
else
{
reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
}
StringBuffer sb = new StringBuffer();
String temp = reader.readLine();
while(temp != null)
{
sb.append(temp);
temp = reader.readLine();
}
reader.close();
System.out.println( sb.toString());