Java and system environment variables

586 views Asked by At

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?

1

There are 1 answers

1
Roberto Attias On

I would go for one of the following:

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

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

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