1

I have written a script to change the java environment variables of the shell:

#!/bin/bash                                                                                                                                                                                                   

#env variables can be changed only if we call the script with source setJavaVersion.sh                                                                                                                         
case $1 in
  6)
     export JAVA_HOME=/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
     export PATH=$JAVA_HOME:$PATH     ;
  ;;
  7)
     export JAVA_HOME=/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51
     export PATH=$JAVA_HOME:$PATH     ;
  ;;
  8)
     export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.91-0.b14.el7_2.x86_64/jre/
     export PATH=$JAVA_HOME:$PATH     ;
  ;;
  *)
     error java version can only be 1.6, 1.7 or 1.8
  ;;
esac

To execute it, I enter:

source setJavaVersion.sh 6

to set the environment with jdk6, source setJavaVersion.sh 7 for jdk7 and so.

when I look at the environment variables with:

$ echo $JAVA_HOME

and

$ echo $PATH

I see that the variables are well updated.

However, when I execute the command

java -version

it is not updated.

If I enter the same export commands directly in the shell, java -version returns the updated result.

Why ?

Edit: I have updated my script with the deathangel908 answer. Here is the output of which java and PATH before and after the script execution:

$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java

$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin

$ source setJavaVersion 6

$ which java
/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin/java

$ echo $PATH
/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/CPD/SDNT/tools/bin:/CPD/SDNT/tools/x86_64-pc-unix11.0/bin:/atgl/product/java/jdk-1.7.0_51/linux-redhat_x86_64/jdk1.7.0_51/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/t0059888/bin:/users/t0059888/bin:/atgl/product/java/jdk-1.6.0_43/linux-redhat_x86_64/jdk1.6.0_43/
4
  • Are you closing shell after executing the script? Commented Mar 16, 2018 at 15:05
  • No I test JAVA_HOME and PATH after source setJavaVersion.sh. Commented Mar 16, 2018 at 15:40
  • Have a look at update-java-alternatives. Commented Mar 16, 2018 at 16:47
  • 1
    Don't reinventing the wheel. Have you tried jenv.be ? Commented Mar 19, 2018 at 9:31

3 Answers 3

2

You're appending path every time, you need to remove it and add again export

: export a="$a:3"
$ echo $a # :3
: export a="$a:3"
: echo $a # :3:3

When you executing java, bash starts lookup in PATH variable, finds the first occurrence and executes it.

You can use which java to check the real path of java command that's executed.

So to solve your issue just remember the path w/o java:

if [ -z ${PATH_SAVE+x} ]; then
 export PATH_SAVE="$PATH"
fi
export PATH="$PATH_SAVE:$JAVA_HOME"

Remember to quote variables, in case they contain special symbols or spaces.

Also you can debug your script by running echo $PATH

Sign up to request clarification or add additional context in comments.

3 Comments

The behavior is the same with your solution, PATH is updated without duplicate, with the java jdk path at the end of the variable instead of the beginning. But "which java" or java -version still does not change. It is very strange because when I enter the export commands directly in the shell, even with the duplicates, it works.
How comes which java doesn't change? Please produce the output of which java before the script and after. And $PATH b4 and after.
Ok I have edited my question with the variables output
1

Based on the output you added in your Edit, the new PATH was added at the end. Since Java 7 is at the beginning of your PATH that one is used when you run which java.

When you execute a command, the first occurrence found on on the PATH will be used, so, try adding it at the beginning of the variable (as you did in your original script, without the changes proposed in the other answer. I mean, it is a good idea what the other answer suggested, that you should not be appending the same paths over and over again, but if you add the Java path at the end of your PATH variable, make sure no other java is found on a previous path).

For what I can see in your original script, it should be working fine.

Try adding set -x at the beginning of your original script, and look at the output. It would be helpful if you could share that output as well.

Finally, make sure the binaries in Java 6 have the right file permissions (make sure java is executable).

Comments

0

My error was that the java executable was not accessible in the path. It is located in the bin folder. What I did before was wrong:

export PATH="$JAVA_HOME:$PATH"

This is the solution:

export PATH="$JAVA_HOME/bin:$PATH"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.