7

I'm writing an application that leverages jsvc to start up a Java service as a daemon. I need to use something like jsvc 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 jsvc binary installed.

However, it seems that jsvc has one major catch; it can't detect the home folder of Java on a given Unix operating system, which is quite frustrating:

$ ./startup.sh
Cannot locate Java home

I have been able to work around the issue on Ubuntu at least by manually setting the JVM home directory:

jsvc ... -home /usr/lib/jvm/default-java/ ...

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:

JAVA_HOME="$( ... )"

jsvc ... -home "$JAVA_HOME" ...

...rather than hard-coding for each individual operating system. Is there a way that, given a java binary, I can find the home directory of its JVM/JRE?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • possible duplicate of [What is the correct target for the JAVA_HOME envrionment variable for a Linux OpenJDK debian-based distribution?](http://stackoverflow.com/questions/663658/what-is-the-correct-target-for-the-java-home-envrionment-variable-for-a-linux-op) – paulsm4 Aug 30 '12 at 22:10
  • 1
    Not a duplicate; I'm trying to locate Java home generically, not just for Debian distributions but also for Red Hat, OSX and other Linuxes/Unixes. – Naftuli Kay Aug 30 '12 at 22:11
  • Please read the SO link. Especially the parts about "update-java-alternatives/update-alternatives". – paulsm4 Aug 30 '12 at 22:36

4 Answers4

5

Not sure if this works across *nixes, but found this solution:

JAVA_HOME="$( readlink -f "$( which java )" | sed "s:bin/.*$::" )"

I've tested it on Ubuntu and it works, however it does not work for OSX.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
3

My solution was compiling the native linux source as the main jsvc page says in http://commons.apache.org/proper/commons-daemon//jsvc.html

Here is my step by step procedure

Download www.fightrice.com/mirrors/apache/commons/daemon/source/commons-daemon-1.0.13-src.tar.gz

Once you extract the file then go to ...../commons-daemon-1.0.13-src/src/native/unix

in terminal as a root do the following:

$ support/buildconf.sh

$ ./configure --with-java=/usr/lib/jvm/default-java

$ make

test generated jsvc binary app

$ ./jsvc -help

It works! cleanly.

Jorge Sanchez
  • 1,619
  • 1
  • 13
  • 14
0

Use dirname and which commands to find Java's bin directory:

echo `dirname \`which java\``
JAVA_HOME=`dirname \`which java\``

... Only works if Java is already on the $PATH.

EthanB
  • 4,239
  • 1
  • 28
  • 46
  • 2
    That returns `/usr/bin`, which undoubtedly (and reproducibly) fails. Since `which java` returns `/usr/bin/java`, that's what I get. – Naftuli Kay Aug 30 '12 at 22:19
0

One other way is :

 type -p java

Expect this to return the correct JAVA installation folder.

  • It does not. It fails in exactly the same way as [EthanB's answer](https://stackoverflow.com/a/12206347/712526). Since java is aliased to `/usr/bin`, which is on the PATH, while the jvm location often is not. – jpaugh Nov 22 '21 at 04:35