PATH and programmatic usage?

Tag: unix Author: wtcsu Date: 2009-08-26

In programmatic usage of CLI commands (in Java on Linux), would you

  • rely on these commands being on the PATH, or
  • specify the absolute path of each command in the code?

Different for "standard" commands, e.g. "ls", vs. non-standard commands?

Addendum: By "in the code" I didn't mean "hard-coded". Having the the commands' paths configurable would be of course the way to go.

Best Answer

Neither(!). I'd provide a configuration, which may be as trivial as a properties file.

e.g. = /bin/ls

etc. The above is straightforward to implement, and very easy to change/override as required. I would be wary of relying on the PATH for all but the simplest scenarios.


+1 I agree. Assuming the path to 'ls' in a shell script is one thing, buried in an app is something else. Java makes using properties files easy. The more widely your app is distributed, the more important this becomes. It's trivial to edit a properties file, but a hardcoded assumption in your app is a showstopper for an end user. When you deliver products, you want them to 'just work'
I fully agree. Using a properties file approach is the right thing to do. But it still falls into the responsibility of the Java application.

Other Answer1

If the absolute path is a standard path like in /usr/bin, /usr/sbin etc I would use those, otherwise I would do a which on the command name and use the output of that.