javaenvironment-variables

What order should programs look at JAVA_HOME, JRE_HOME, JDK_HOME for locating a Java version?


What order should programs look at JAVA_HOME, JRE_HOME, JDK_HOME for locating a version of Java to execute Java programs?

I want my program to find Java automatically using an environment variable, but there are at least three to choose from.


My guess is that I should go by semantics; I need any JRE to run java (both JDK or JRE would do) so I'm guessing the following order:

and since I don't specifically need/want a JDK, and JDK_HOME looks less common in the wild than JAVA_HOME, perhaps not try to use JDK_HOME, and rely on my users to set JRE_HOME or JAVA_HOME appropriately.


Solution

  • Your guess order looks good.

    You could try all of them in any particular order since how those variables are set depends on the operating system and/or on the user. The script below is part of OpenSuse Linux distro that does the job for the user.

    As it can be seen it tries both java and javac default commands to set variables.
    It could be perfectly possible that both command belong to different versions depending on user's configuration.

    cat /etc/profile.d/alljava.sh

    #                                                                               
    #    /etc/profile.d/alljava.sh                                                    
    #                                                                               
    # send feedback to http://bugs.opensuse.org
    
    #
    # This script sets some environment variables for default java.
    # Affected variables: JAVA_BINDIR, JAVA_HOME, JRE_HOME, 
    #                     JDK_HOME, SDK_HOME
    #
    
    if test -L /etc/alternatives/java -a -e /etc/alternatives/java; then
        ALTERNATIVES_JAVA_LINK=`realpath /etc/alternatives/java 2> /dev/null`
        export JRE_HOME=${ALTERNATIVES_JAVA_LINK%/bin/java}
        unset ALTERNATIVES_JAVA_LINK
    fi
    
    if test -L /etc/alternatives/javac -a -e /etc/alternatives/javac; then
        ALTERNATIVES_JAVAC_LINK=`realpath /etc/alternatives/javac 2> /dev/null`
        export JAVA_HOME=${ALTERNATIVES_JAVAC_LINK%/bin/javac}
        export JAVA_BINDIR=$JAVA_HOME/bin
        export JDK_HOME=$JAVA_HOME
        export SDK_HOME=$JAVA_HOME
        unset ALTERNATIVES_JAVAC_LINK
    fi
    

    The mentioned script is installed by this package

    rpm -qf /etc/profile.d/alljava.sh
    aaa_base-84.87+git20180409.04c9dae-150300.10.28.2.x86_64
    

    values as shown by env here

    env | grep -E 'JDK|JRE|JAVA' 
    JAVA_HOME=/usr/lib64/jvm/java-17-openjdk-17
    JDK_HOME=/usr/lib64/jvm/java-17-openjdk-17
    JRE_HOME=/usr/lib64/jvm/java-17-openjdk-17
    JAVA_BINDIR=/usr/lib64/jvm/java-17-openjdk-17/bin
    

    Users may override those values

    export JDK_HOME='/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0'
    
    env | grep -E 'JDK|JRE' 
    JDK_HOME=/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0
    JRE_HOME=/usr/lib64/jvm/java-17-openjdk-17
    

    Finally, those variables could not be set at all so version should be obtained from the java command itself

    java -XshowSettings:properties -version 2>&1 | grep -E -e 'java\.(home|specification\.version)'

        java.home = /usr/lib64/jvm/java-17-openjdk-17
        java.specification.version = 17