ProcessBuilder and Environment variables
There is any way to use environment variables, like in dos, with %% through java programming?
For example :
ProcessBuilder pb = new ProcessBuilder("c:/winnt/notepad.exe", "%TTT%");
// Would be even better :) ... But the previous line is Ok ;)
// ProcessBuilder pb = new ProcessBuilder("c:/winnt/notepad.exe %TTT%");
Map<String,String> env = pb.environment();
env.put("TTT", "M:/temp/testTTT.txt");
try{
Process process = pb.start();
}
catch(Exception e){
e.printStackTrace();
}
Ok but the problem is not that I can't read the variable, the problem is that I would like to use the %VARIABLE_NAME% without expand it by hand :) If you do this at the Dos prompt,
set TTT=M:/temp/testeTTT.txt
notepad %TTT%
and similarly at the Unix with $, it works. The shell automaticaly expands the content of the variable. I would like to know if there is any way to use this through java, using exec or ProcessBuilder
Regards...
Allow the shell or cmd.exe to do the expansion.
final String[] command =
{
"sh",
"-c",
"echo $PATH",
};
final ProcessBuilder pb = new ProcessBuilder(command);
or
final String[] command =
{
"cmd.exe",
"/C",
"echo %PATH%",
};
final ProcessBuilder pb = new ProcessBuilder(command);
Cool, it worked, Thanks sabre150 :)
The strange thing is that with echo works, with dir no!!! :)
//String[] command = { "cmd.exe", "/C", "echo %TTT%", };
String[] command = { "cmd.exe", "/C", "dir %TTT%", };
final ProcessBuilder pb = new ProcessBuilder(command);
Map<String,String> env = pb.environment();
env.put("TTT", "M:/temp");
try{
Process process = pb.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch(Exception e){
e.printStackTrace();
}