tags:

views:

35

answers:

2

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.

+4  A: 

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

e.g.

command.ls = /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.

Brian Agnew
+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'
DaveParillo
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.
Max Spring
A: 

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.

ennuikiller