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();

}

[568 byte] By [Daniel_camaraa] at [2007-11-26 16:21:13]
# 1
Look at the getenv() method: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html
kevjavaa at 2007-7-8 22:44:51 > top of Java-index,Java Essentials,Java Programming...
# 2

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...

Daniel_camaraa at 2007-7-8 22:44:51 > top of Java-index,Java Essentials,Java Programming...
# 3

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);

sabre150a at 2007-7-8 22:44:51 > top of Java-index,Java Essentials,Java Programming...
# 4

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();

}

Daniel_camaraa at 2007-7-8 22:44:51 > top of Java-index,Java Essentials,Java Programming...