Skartt
Skartt

Reputation: 591

Set java version from a script

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/

Upvotes: 1

Views: 8026

Answers (3)

Skartt
Skartt

Reputation: 591

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"

Upvotes: 0

J B
J B

Reputation: 341

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).

Upvotes: 1

deathangel908
deathangel908

Reputation: 9699

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

Upvotes: 2

Related Questions