Reputation: 9062
Within a Java 1.8 program, how can I execute a program on the user's PATH? For simplicity let's assume that only *nix systems have to be supported. What I want to do is something like:
Runtime.getRuntime().exec("myProgram")
where myProgram
is somewhere on the user's PATH.
Runtime.getRuntime().exec(cmd)
can only "see" programs on the system's path. For example in my case (mac os) /usr/bin:/bin:/usr/sbin:/sbin
, but what if myProgram
is in /usr/local/bin
or ~/bin
?
I tried to get the directories on the user's PATH by parsing the output of Runtime.getRuntime().exec("echo $PATH")
, but I get back only the string "$PATH" itself, not the actual content of the PATH variable.
EDIT Executing System.getenv()
gives me only PATH=/usr/bin:/bin:/usr/sbin:/sbin
, i.e. not the user's PATH.
Upvotes: 3
Views: 341
Reputation: 616
String userPath = System.getenv("PATH");
If it returns a null string then try exporting that $PATH variable before running your java program.
Upvotes: 0
Reputation: 182
If you want to get the actual content of the PATH variable you can use:
System.getenv():
Good luck!
Upvotes: 1