Search code examples
javaunixprocessbuilderhome-directory

Using Java ProcessBuilder, how do I run a process located in a unix home directory?


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

Solution

  • 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");