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.
@AdamSiemion is close - You should add these lines to ~/.bashrc, which is sourced by interactive shells, while ~/.bash_profile is only sourced by interactive login shells.
cat >> ~/.bashrc <<'EOF'
JAVA_HOME=/etc/jdk1.7.0_07/
export JAVA_HOME
export PATH=$PATH:$JAVA_HOME/bin
EOF
To verify, open a new shell and echo "$JAVA_HOME".
Append these lines to the .bash_profile file in your HOME directory, i.d. ~/.bash_profile.
It seems that you do not have a Java JDK (Java Development Kit) installed, if you try run the following command you:
javac -version
probably you will get the following:
The program 'javac' can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install <selected package>
So, you have to run the following command will install the OpenJDK version of the Java JDK:
sudo apt-get install default-jdk
Export the JAVA_HOME variable.
export JAVA_HOME=/path/to/java/home
Depending on your installation, you can also use java_home with the version.
JAVA_VERSION=1.7
export JAVA_HOME=$(/usr/libexec/java_home -v $JAVA_VERSION)
From man bash:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
and
Bash attempts to determine when it is being run with its standard input connected to a network connection, as when executed by the remote shell daemon, usually rshd, or the secure shell daemon sshd. If bash determines it is being run in this fashion, it reads and executes commands from ~/.bashrc and ~/.bashrc, if these files exist and are readable. It will not do this if invoked as sh. The --norc option may be used to inhibit this behavior, and the --rcfile option may be used to force another file to be read, but rshd does not generally invoke the shell with those options or allow them to be specified.
In other words, ~/.profile and ~/.bashrc will normally both be read when you start bash, so you can put it in either.
You can also use the source luke in either file to load the file luke where you would define all your Javan variables. See help . or help source for details.
First, You will never add JAVA_HOME to your PATH. They serve different purposes as environment variables. The first, sets where your java binaries are installed so other java based programs can know. The second, is a set of places where bash will look for commands you type in terminal, like ls, cd etc.
Second, both the files will work, but you have to be careful when setting variables inside .bashrc, because you may overwrite the ones already configured in other files, as PATH. The most safe way of doing this is by adding ":$PATH" at the end of the path. It would be something like this (adds java binaries to your PATH to be able to call commands such as javac):
export PATH="/usr/java/jdk1.8.0_121/bin:$PATH"
To set JAVA_PATH, the most safe (and automatic... rsrs) way to do so is by adding the following code in the end of .bashrc:
export JAVA_HOME=$(update-alternatives --query javac | sed -n -e 's/Best: *\(.*\)\/bin\/javac/\1/p')
After you have done so, you will have do execute exec bash, or close and reopen the terminal window. To check if your variables are set, run on the terminal:
echo $JAVA_HOME
echo $PATH
You should be able to see the value inside the variables.
UPDATE:
Remember that all these files like .bashrc are located in your home directory. Something like /home/<your name> or just by typing cd without any other arguments. Remember too, that files that start with a 'dot' . are not visible just by typing ls on terminal. if you want to see these files you have to ls -a