My current default version of Java is 7 (which, for now, can't be updated - I know it's reached EOL), but also have Java 8 installed.

However, I have some third party jars (all contained in a directory) that require Java 1.8. Each jar has an associated bash script to check relevant environment variables, and to construct the java command required to run the jar. Each bash script assumes that the command to invoke java is, unsurprisingly, java.

Is there a way for me to change the version of java that is executed when java is invoked from 7 to 8 in this folder only?


Sure: add your pwd to your PATH variable, in the most significant position, i.e.,

 export PATH=.:$PATH

You should use the above version, not export PATH=$PWD:$PATH because in this case $PWD is evaluated at the time of sourcing the bash file containing the command, while you want it to be evaluated every time.

Now create a symbolic to the java version you want to use, in the directory where you need this to differ from the standard version:

ln -s /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java ./java

or whatever your executable is called.

Notice that including your PWD in your PATH variable is however a potential security risk.

  • 1
    You may have to set/change JAVA_HOME and CLASSPATH as well ... – DavidPostill May 4 '16 at 13:28
  • @DavidPostill Yup, u r right! But I hope the OP gets the drift... – MariusMatutiae May 4 '16 at 13:42
  • Maybe a slightly naïve question, but why do I need to set add pwd to the PATH? – ChrisW May 4 '16 at 13:46
  • 1
    @ChrisW pwd is your current directory, and PATH is the list of directories where executables are searched for. By putting pwd on top of PATH, you are saying: whenever I issue a command, see whether there is a local version, and, if there is, let this take precedence over globally defined alternatives. – MariusMatutiae May 4 '16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.