Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

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.

One other way is :

 type -p java

Expect this to return the correct JAVA installation folder.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top