Cannot find Java homepage

I am writing an application that uses jsvc to start a Java service as a daemon. I need to use something like jsvc , because my application uses ports under 1024, and yet I really don't want to run it as root so that the created files belong to another user. I would also like to keep dependencies and configuration to a minimum, so all client needs are the JVM and the jsvc binary.

However, it seems that jsvc has one main catch; it cannot detect the Java home folder on this Unix operating system, which is rather complicated:

 $ ./startup.sh Cannot locate Java home 

I managed to solve the problem on Ubuntu, at least manually, by installing the JVM home directory:

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

Is there a way to dynamically determine the Java home directory from a Bash script so that I can do this work on most Unix / Linuxes? I could sleep 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 binary java , I can find the home directory of its JVM / JRE?

+7
source share
4 answers

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

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

I tested it on Ubuntu, but it works, but it does not work for OSX.

+5
source

My solution was to compile the linux source source, as the jsvc main page says at 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, go to ..... / commons -daemon-1.0.13-src / src / native / unix

in the terminal as the root do the following:

$ support / buildconf.sh

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

$ make

jsvc test generated binary application

$. / jsvc -help

It works! purely.

+3
source

Use the dirname and which commands to find the bin bin directory:

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

... only works if Java is already on $PATH .

0
source

Another way:

  type -p java 

Expect you to return the correct JAVA installation folder.

0
source

All Articles