title: Cannot locate Java home
body: <p>I'm writing an application that leverages <code>jsvc</code> to start up a Java service as a daemon. I need to use something like <code>jsvc</code> because my application utilizes ports under 1024 and yet I'd really like to not run it as root so that created files are owned by another user. I'd also like to keep dependencies and configuration  to a minimum so that all the client needs is a JVM and the <code>jsvc</code> binary installed. </p>
  
  <p>However, it seems that <code>jsvc</code> has one major catch; it can't detect the home folder of Java on a given Unix operating system, which is quite frustrating:</p>
  
  <pre><code>$ ./startup.sh
  Cannot locate Java home
  </code></pre>
  
  <p>I have been able to work around the issue on Ubuntu at least by manually setting the JVM home directory:</p>
  
  <pre><code>jsvc ... -home /usr/lib/jvm/default-java/ ...
  </code></pre>
  
  <p>Is there any way to determine the Java home directory dynamically from a Bash script so I can make this work across most Unixes/Linuxes? I'd be able to sleep much better at night doing something like:</p>
  
  <pre><code>JAVA_HOME="$( ... )"
  
  jsvc ... -home "$JAVA_HOME" ...
  </code></pre>
  
  <p>...rather than hard-coding for each individual operating system. Is there a way that, given a <code>java</code> binary, I can find the home directory of its JVM/JRE?</p>