Both /etc/bashrc and ~/.bashrc are whats referred to as non-login files. Meaning they do not get sourced during a login shell unless called by /etc/profile or another file.
If /etc/profile calls it (as is typical) then if you want to test it in your current shell you have to either source it.
/root: #> source .bashrc (source with the command source)
/root: # . .bashrc (source with a period)
Or you have to instantiate a login shell by using su or sudo.
/root: # su - (the dash starts a login shell which will re-source login files)
/root: # sudo su - (using sudo to perform the switch user command)
and of course you could just re-login.
EDIT:
Typically though Java paths are not set using .bashrc they are set as separate scripts in the /etc/profile.d directory. At least on the enterprise distributions I use.
system1:/etc/profile.d # ll
-rw-r--r-- 1 root root 1438 Aug 30 2005 alljava.csh
-rw-r--r-- 1 root root 1682 Jul 13 2005 alljava.sh
system1:/etc/profile.d # cat alljava.sh
if [ -x /usr/$__libdir/jvm/java/bin/java ] || [ -x /usr/$__libdir/jvm/java/bin/jre ] ; then
export JAVA_BINDIR=/usr/$__libdir/jvm/java/bin
export JAVA_ROOT=/usr/$__libdir/jvm/java
export JAVA_HOME=/usr/$__libdir/jvm/java
#....cut for brevity...
Answer from 2bc on Stack ExchangeBoth /etc/bashrc and ~/.bashrc are whats referred to as non-login files. Meaning they do not get sourced during a login shell unless called by /etc/profile or another file.
If /etc/profile calls it (as is typical) then if you want to test it in your current shell you have to either source it.
/root: #> source .bashrc (source with the command source)
/root: # . .bashrc (source with a period)
Or you have to instantiate a login shell by using su or sudo.
/root: # su - (the dash starts a login shell which will re-source login files)
/root: # sudo su - (using sudo to perform the switch user command)
and of course you could just re-login.
EDIT:
Typically though Java paths are not set using .bashrc they are set as separate scripts in the /etc/profile.d directory. At least on the enterprise distributions I use.
system1:/etc/profile.d # ll
-rw-r--r-- 1 root root 1438 Aug 30 2005 alljava.csh
-rw-r--r-- 1 root root 1682 Jul 13 2005 alljava.sh
system1:/etc/profile.d # cat alljava.sh
if [ -x /usr/$__libdir/jvm/java/bin/java ] || [ -x /usr/$__libdir/jvm/java/bin/jre ] ; then
export JAVA_BINDIR=/usr/$__libdir/jvm/java/bin
export JAVA_ROOT=/usr/$__libdir/jvm/java
export JAVA_HOME=/usr/$__libdir/jvm/java
#....cut for brevity...
To enable the change which you have performed on ~/.bashrc file, you need to execute the command mentioned in the ~/.bashrc file . For this you have to close the terminal and open it again.
First, you have a typo in export line. It should be
export PATH=$PATH:$JAVA_HOME/bin
No space, add a $ character before JAVA_HOME, add /bin
However fixing above line may not fix your problem in installing java. The easiest way to install java is by using webup8 ppa:
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
After the last command, the script will automatically download the jdk from Oracle website and install it for you. You can read the tutorial here.
You have set the environment variable JAVA_HOME, but this is only a value used by applications that want to know where the JRE or JDK are.
The PATH is what your shell is looking in when you try to run executables. So when you type java -version, it is looking in the JDK folder. Note that java is actually inside $JAVA_HOME/bin. That's why it can't find it.
That's the explanation.
The fix is to either add it to the $PATH properly:
export PATH=$PATH:JAVA_HOME/bin
or to run update-alternatives and add the JDK path to the system's java alternatives.
For example
update-alternatives --install "/usr/bin/java" "java" "/home/myuser/sandbox/java/7u55/jdk1.7.0_55/bin/java" 3
java - JAVA_HOME in bashrc - Stack Overflow
java - how to update JAVA_HOME in bashrc file - Stack Overflow
Set custom JAVA_HOME path
java - How to set JAVA_HOME in Linux for all users - Stack Overflow
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.
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)
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.