I'm developing some Java application which requires to run some external programs from bash.
My preferred way to achieve this is with ProcessBuilder
, but i've some problems.
ProcessBuilder.environment()
returns a PATH=/usr/bin:/bin:/usr/sbin:/sbin
which is i guess truly System path.
But there is a trick, on my OSX machine these programs are installed under /usr/local/bin
and on other machines can be in complete different location.
I can hardcode environment.put()
with my path, but this does not solve me a problem for cross-platform.
The best option will be if i can copy-paste
bash PATH
if this is possible and how?
Which approach would you take?
- Hardcode most likely paths and put into ProcessBuilder
- Read files with path info (.profile, bash_profile, /etc/paths, etc..)
- Copy paste PATH from shell - how? (Win, Osx, Linux solution if possible)
- Other?
TL;DR
I want to execute command from ProcessBuilder which can be in any location and i can suppose that path for command is in shell PATH env. How?
I would go for one of the following:
try to execute from path. if fails, report an error to user indicating that the program is not in the path (also show the path). This way the user is induced to configure his path including the program
search for an environment variable specific to your program (say, program-name_BINPATH). If not found, tell the user he must define it. The variable should point to the dir containing the binaries
Try to execute the binaries. if failed load a config file (if present), which specifies where they are. If failed to load the config file, search for the binaries in the file system (reporting to the user what you're doing, and that it will be done only once) and once found store their location in the config file.