Reputation: 7268
We have a Java server-client application that allows us to run processes on different boxes (i.e. the clients), which are started by the Java ProcessBuilder
. I want to run a process that will be copied/synced to the user's home directory (i.e. the user who started the client).
How do I reference the unix home directory in the String
that is passed to the ProcessBuilder
? (Due to the design of the server-client application, only a String of the process, args etc. is passed to the ProcessBuilder.)
It works if I state the home directory explicitly:
/home/user/processes/process.sh
However, that assumes that I know which user is running the client. (Part of the design is that we can switch/substitute boxes/clients to run jobs, without necessarily knowing who started the client on a given box.)
I've also tried, but to no avail:
$HOME/processes/process.sh
~/processes/process.sh
Upvotes: 2
Views: 1826
Reputation: 7268
In the end, the approach I took was to pass the script and its arguments to bash - along the lines of:
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "$HOME/processes/process.sh args");
Upvotes: 1
Reputation: 36703
The issue is that both ~ and $HOME are only understood by your shell, probably BASH, not by ProcessBuilder or Java.
$HOME should be available via the property user.home
. See System Properties documentation
String home = System.getProperty("user.home");
i.e.
File fullpath = new File(System.getProperty("user.home"), "processes/process.sh");
ProcessBuilder processBuilder = new ProcessBuilder(fullpath.getAbsolutePath());
or could call it relative to current directory
ProcessBuilder processBuilder = new ProcessBuilder("processes/process.sh");
processBuilder.directory(new File(System.getProperty("user.home")));
Upvotes: 3
Reputation: 1
From a command line, both $HOME
and ~
are expanded to a user's home directory by the shell.
ProcessBuilder
runs the process directly with no shell, so those expansions will not work.
Upvotes: 0
Reputation: 4044
Not tried it myself but that should work:
Change into the home before and then execute the process.sh
cd && ./processes/process.sh
Upvotes: -1