find /usr/lib/jvm/java-1.x.x-openjdkvim /etc/profilePrepend sudo if logged in as not-privileged user, ie.
sudo vim- Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH- logout and login again, reboot, or use
source /etc/profileto apply changes immediately in your current shell
find /usr/lib/jvm/java-1.x.x-openjdkvim /etc/profilePrepend sudo if logged in as not-privileged user, ie.
sudo vim- Press 'i' to get in insert mode
add:
export JAVA_HOME="path that you found" export PATH=$JAVA_HOME/bin:$PATH- logout and login again, reboot, or use
source /etc/profileto apply changes immediately in your current shell
For all users, I would recommend creating a file in /etc/profile.d/java_home.sh the following lines
# Set JDK installation directory according to selected Java compiler
export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
This will update dynamically and works well with the alternatives system. Do note though that the update will only take place in a new login shell.
Videos
You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment.
Open /etc/environment in any text editor like nano or gedit and add the following line:
JAVA_HOME="/usr/lib/jvm/open-jdk"
(java path could be different)
Use source to load the variables, by running this command:
source /etc/environment
Then check the variable, by running this command:
echo $JAVA_HOME
Update
Usually most linux systems source /etc/environment by default. If your system doesn't do that add the following line to ~/.bashrc (Thanks @pje)
source /etc/environment
To set JAVA_HOME environment variable, do the following:
- Launch Terminal by pressing Ctrl+Alt+T on your keyboard.
- Enter the following command:
$gksudo gedit /etc/environment - Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the
/usr/lib/jvm/java-7-oracledirectory.
Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME - Save your file and exit gedit.
- Lastly, reload the system PATH with the following command:
$. /etc/environment
The above method will save you the hassle in having to run the commands every time you log in to your computer.
You shouldn't refer to the jre/ subfolder, as Maven will need a Java compiler and potentially other JDK-only tools.
So just use:
JAVA_HOME=/home/z222189/jdk1.8.0_31
EDIT: (making sure that a correct, working JDK is installed at that location, of course...)
Seems you are in Linux, be sure you get Java for Linux!
Set JAVA_HOME using
export JAVA_HOME=/home/z222189/jdk1.8.0_31
Does not work because java is not there, or the version is not the correct, if you already installed correct java linux version and you dont really know where your oracle-java is installed, you can find this running
which java
which will tell you which binary of java is being called, normally is something like
/usr/bin/java
but you can
readlink -f /usr/bin/java
that will give you something like
/usr/lib/jvm/java-7-oracle/jre/bin/java
ergo
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre/
For obtaining only one System Variable use the following code:
String sysEnvStr = System.getenv("JAVA_HOME");
If it returns null then make changes in your .bashrc file. Try exporting that particular variable.
See this howto:
// just one
System.out.println("PATH = " + System.getenv("PATH"));
// all of them
Map env = System.getenv();
for (Iterator it=env.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
Using 12.04, placing the information in /etc/environment worked for me:
tku@buster:~$ cat /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
[...]
LC_MEASUREMENT="de_DE.UTF-8"
JAVA_HOME=/home/tku/work/jdk1.7.0_05
You wanna make sure your Java is the default:
Set Default Java Version
Use the alternatives command to set the default java version.
~ ~โโ$โโ sudo alternatives --config java
Select Java:
If your system has multiple Java versions, the above command will list all Java versions.
There are 3 programs which provide 'java'.
Selection Command
-----------------------------------------------
+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-4.fc35.x86_64/bin/java)
2 java-latest-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.1.0.12-2.rolling.fc35.x86_64/bin/java)
* 3 /usr/java/jdk-17.0.1/bin/java
Enter to keep the current selection[+], or type selection number: whateverisyours
Verify Java Version
Check the java version using the following command.
~ ~โโ$โโ java --version
output feks.:
java 18.0.2 2022-07-19
Java(TM) SE Runtime Environment (build 18.0.2+9-61)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)
Then, add the required variables.
To set the environment variables for a particular user, place the below variables in the .bash_profile file in the home directory or in the /etc/environment file for system wide settings.
~ ~โโ$โโ sudo nano /etc/environment
and your information there is current version of Java:
export PATH=$PATH:/usr/java/jdk-18.0.2/bin/
export JAVA_HOME=/usr/java/jdk-18.0.2/
if your using JRE then it will look something like this:
export PATH=$PATH:/usr/java/jre*****/bin/
export JAVA_HOME:/usr/java/jre*****/
Test it by executing:
~ ~โโ$โโ echo $JAVA_HOME
~ ~โโ$โโ echo $PATH
~ ~โโ$โโ java --version