How to install and switch between multi JDK?

How to install and switch between multi JDK?

Table of Contents

1 How to install and switch between multi JDK?

I suppose that you are installed both java 7 and 8 into your system. So how to switch between them.

1.1 Linux

#!/bin/bash
# java1.7.sh
# source java1.7.sh to switch jdk 1.7

export JAVA_HOME="/opt/tools/jdk1.7.0_60"
export CLASSPATH=.:${JAVA_HOME}/lib:${CLASSPATH}
export JDK_HOME="/opt/tools/jdk1.7.0_60"

export PATH="/opt/tools/jdk1.7.0_60/bin":${PATH}
#!/bin/bash
# source java1.8.sh to switch to JDK 1.8

export JAVA_HOME="/opt/tools/jdk1.8.0_05"
export CLASSPATH=.:${JAVA_HOME}/lib:${CLASSPATH}
export JDK_HOME="/opt/tools/jdk1.8.0_05"

export PATH="/opt/tools/jdk1.8.0_05/bin":${PATH}

1.2 Mac OS X

# ~/.bash_profile
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)

alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'

use java7 and java8 command to switch between JDK 7 and 8.

2 Common incompatible errors between different version

Actually, there are bunch of incompatible problems after JDK upgrade and downgrade, check the official document.

http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html

The good news is most of its upgrade features are not needed by myself. But get familiar with them is at least let us to wrote JDK compatible code.

In my case, I met two errors:
  • Type Inference for Generic Instance Creation
       substitute the parameterized type of the constructor with an empty set of type parameters. Those feature was not compatible in JDK 1.6.
Map<String, List<String>> myMap = new HashMap<>();
  • doclint features in java 1.8, which check invalid Html and accessibility
      So, always wrote correct java doc.

Comments

Popular posts from this blog

Bluedroid stack in android

How to setup a NAT server?

Network programming in elisp