3

I installed openjdk with sudo snap install openjdk, and I want to set JAVA_HOME to enable my java related commands in terminal, but it is not working.

I tried the following:

  1. creating a script java_home.sh in /etc/profile.d with the content JAVA_HOME=/snap/openjdk/current/jdk and doing logout/login, but it is not working
  2. creating a script java_home.sh in /etc/profile.d with the content source $(openjdk) and doing logout/login, but it is not working
  3. adding JAVA_HOME=/snap/openjdk/current/jdk export PATH=$PATH:$JAVA_HOME to ~/.bashrc, and opening new terminal, but it is still not working

I don't know what to try, or what to do, so if anyone can help, I will be very thankful.

The output of ls /snap/openjdk is 909 current.

The output of cat $(openjdk) is:

# Source this file for OpenJDK environment variables and aliases
export JAVA_HOME=/snap/openjdk/909/jdk
export MANPATH=/snap/openjdk/909/jdk/man:
alias java='openjdk.java'
alias javac='openjdk.javac'
alias javadoc='openjdk.javadoc'
alias jar='openjdk.jar'
alias jarsigner='openjdk.jarsigner'
alias jlink='openjdk.jlink'
alias jpackage='openjdk.jpackage'
alias jwebserver='openjdk.jwebserver'

when doing source openjdk, it starts working, but I don't want to do that every time and want it loaded everywhere as a env variable.

3 Answers 3

3

found the solution in developpaper.com


sudo snap install openjdk

openjdk 

//generate .env
// in my case /var/snap/openjdk/1005/openjdk.env

/**
# Source this file for OpenJDK environment variables and aliases
export JAVA_HOME=/snap/openjdk/1005/jdk
export MANPATH=/snap/openjdk/1005/jdk/man:
alias java='openjdk.java'
alias javac='openjdk.javac'
alias javadoc='openjdk.javadoc'
alias jar='openjdk.jar'
alias jarsigner='openjdk.jarsigner'
alias jlink='openjdk.jlink'
alias jpackage='openjdk.jpackage'
alias jwebserver='openjdk.jwebserver'
*/

add .env in to file /etc/profile 

source /etc/profile

java --version

openjdk 18.0.2 2022-07-19
OpenJDK Runtime Environment (build 18.0.2+9-snap)
OpenJDK 64-Bit Server VM (build 18.0.2+9-snap, mixed mode, sharing)

https://developpaper.com/linux-system-uses-snap-to-install-jdk/

0

Everything that the OS needs to find java is in /snap/openjdk/909 or /snap/openjdk/current directories, it just needs the right paths.

So the answer to the quesiton:

Create java_home.sh in /etc/profile.d/ and in the sh add:

export JAVA_HOME=/snap/openjdk/909/jdk
export PATH=$PATH:$JAVA_HOME/bin

login/logout and java --version will work.

0

A better way is to 'source' /var/snap/openjdk/current/openjdk.env to /etc/bash.bashrc. Since you can sudo, I assume that you can edit it with nano.

sudo nano /etc/bash.bashrc

Then, at the bottom of the file, source the env file by typing

source /var/snap/openjdk/current/openjdk.env 

Using 'current', instead of the version number, is a good practice. In the future, if you update OpenJDK, that 'current' will still work. Also, there is no need to log off or restart. Any newly opened terminal or application that relies on bashrc will get the aliases and exports.

Note: instead of editing /etc/bash.bashrc, you may choose to edit ~/.bashrc if you prefer to make it visible only to your account.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .