docs-v7 icon indicating copy to clipboard operation
docs-v7 copied to clipboard

JAVA_HOME variable setup script on linux not compatible with Java-14

Open trane77 opened this issue 5 years ago • 0 comments

Please, tell us what's the problem?

[x] Wrong documentation [x] Improvement of existing article

Please, tell us more details

Currently the docs are telling to set the JAVA_HOME variable this way:

export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac/\1/p')

This can work, but not always: in a system where Java 14 is installed, then it will be reported as the "best" option, which in turn will not be compatible with Nativescript as it supports Java >= 8 and <= 13. "Best" is the most recent version .

I've found a good script, which is working fine, it depends on which version the user selects from update-alternatives:

#!/bin/bash
if [ -z "${JAVA_HOME}" ]
then
    JAVA_HOME=$(readlink -nf $(which java) | xargs dirname | xargs dirname | xargs dirname)
    if [ ! -e "$JAVA_HOME" ]
    then
        JAVA_HOME=""
    fi
    export JAVA_HOME=$JAVA_HOME
fi

To switch the default java version to be used:

sudo update-alternatives --config java
sudo update-alternatives --config javac

trane77 avatar Aug 13 '20 09:08 trane77