Evaluation of environment variables in command run by Java's Runtime.exec()

Posted by Tom Duckering on Stack Overflow See other posts from Stack Overflow or by Tom Duckering
Published on 2010-01-19T02:20:29Z Indexed on 2010/04/02 23:53 UTC
Read the original article Hit count: 479

Filed under:
|

Hi,

I have a scenarios where I have a Java "agent" that runs on a couple of platforms (specifically Windows, Solaris & AIX). I'd like to factor out the differences in filesystem structure by using environment variables in the command line I execute.

As far as I can tell there is no way to get the Runtime.exec() method to resolve/evaluate any environment variables referenced in the command String (or array of Strings).

I know that if push comes to shove I can write some code to pre-process the command String(s) and resolve enviroment variables by hand (using getEnv() etc). However I'm wondering if there is a smarter way to do this since I'm sure I'm not the only person wanting to do this and I'm sure there are pitfalls in "knocking up" my own implementation.

Your guidance and suggestions are most welcome.

edit: I would like to refer to environment variables in the command string using some consistent notation such as $VAR and/or %VAR%. Not fussed which.

edit: To be clear I'd like to be able to execute a command such as:

perl $SCRIPT_ROOT/somePerlScript.pl args

on Windows and Unix hosts using Runtime.exec(). I specify the command in config file that describes a list of jobs to run and it has to be able to work cross platform, hence my thought that an environment variable would be useful to factor out the filesystem differences (/home/username/scripts vs C:\foo\scripts). Hope that helps clarify it.

Thanks. Tom

© Stack Overflow or respective owner

Related posts about environment-variables

Related posts about java