Difference between revisions of "JDK setup"

Line 10: Line 10:
  
 
=Get Java=
 
=Get Java=
 +
 
2 versions of Java are available:
 
2 versions of Java are available:
 +
 
* '''JRE''' as Java Runtime Environment. For end-users. Download JRE: https://java.com/getjava‎
 
* '''JRE''' as Java Runtime Environment. For end-users. Download JRE: https://java.com/getjava‎
* '''JDK''' as Java Devolpment Kit. For developers and application servers. The JDK includes the corresponding JRE. Download JDK: http://www.oracle.com/technetwork/java/javase/downloads/index.html  
+
* '''JDK''' as Java Devolpment Kit. For developers and application servers.  
 +
 
 +
The JDK includes the corresponding JRE. Download JDK: http://www.oracle.com/technetwork/java/javase/downloads/index.html  
  
 
You need to install the JDK on your development station & servers.
 
You need to install the JDK on your development station & servers.
 +
  
  
  
 
=Windows installation=
 
=Windows installation=
 +
  
 
1. Create a new environment variable
 
1. Create a new environment variable
 +
 
* Name:  JAVA_HOME
 
* Name:  JAVA_HOME
 
* Value:    path to your JDK installation (C:\Program Files\java\jdk1.7.0_45)
 
* Value:    path to your JDK installation (C:\Program Files\java\jdk1.7.0_45)
 
   
 
   
 +
 
2. Update the PATH  
 
2. Update the PATH  
 +
 
* Add following sequence to the end of the PATH variable:
 
* Add following sequence to the end of the PATH variable:
 +
<syntaxhighlight lang="bash">
 
       > %JAVA_HOME%\bin
 
       > %JAVA_HOME%\bin
 +
</syntaxhighlight>
 
   
 
   
 +
 
3. Check installation
 
3. Check installation
 +
 
* Open a new terminal console [Run -> "cmd"]
 
* Open a new terminal console [Run -> "cmd"]
 
* Run:  java -version
 
* Run:  java -version
Line 43: Line 56:
  
 
==ORACLE JVM automatic installation (ubuntu)==
 
==ORACLE JVM automatic installation (ubuntu)==
 +
 
This is for Ubuntu based distributions only.
 
This is for Ubuntu based distributions only.
 +
  
 
Add a new repository:
 
Add a new repository:
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
add-apt-repository ppa:webupd8team/java
 
add-apt-repository ppa:webupd8team/java
 
apt-get update
 
apt-get update
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
Install ORACLE JDK packages:
 
Install ORACLE JDK packages:
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
apt-get install oracle-java7-installer oracle-jdk7-installer
 
apt-get install oracle-java7-installer oracle-jdk7-installer
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
This will download the latest JDK, install it automatically and set the new JVM as the default one.
 
This will download the latest JDK, install it automatically and set the new JVM as the default one.
 +
  
 
To remove the repository:
 
To remove the repository:
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
apt-add-repository --remove ppa:webup8team/java
 
apt-add-repository --remove ppa:webup8team/java
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
  
  
 
==ORACLE JVM manual installation (debian)==
 
==ORACLE JVM manual installation (debian)==
 +
  
 
===Download JDK===
 
===Download JDK===
Line 70: Line 94:
 
* Through a graphical interface: http://www.oracle.com/technetwork/java/javase/downloads/index.html
 
* Through a graphical interface: http://www.oracle.com/technetwork/java/javase/downloads/index.html
 
* Using wget:  
 
* Using wget:  
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
export ORACLE_JVM_URL='http://download.oracle.com/otn-pub/java/jdk/7u45-b18/jdk-7u45-linux-x64.tar.gz'
 
export ORACLE_JVM_URL='http://download.oracle.com/otn-pub/java/jdk/7u45-b18/jdk-7u45-linux-x64.tar.gz'
Line 83: Line 108:
  
 
* Get binaries
 
* Get binaries
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
tar -xzvf $ORACLE_JVM_FILENAME
 
tar -xzvf $ORACLE_JVM_FILENAME
Line 90: Line 116:
  
 
* Register new version
 
* Register new version
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_45/bin/java 1065
 
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_45/bin/java 1065
Line 96: Line 123:
 
update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_45/bin/javaws 1065
 
update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_45/bin/javaws 1065
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
You have to adjust the source folder /usr/lib/jvm/'''jdk1.7.0_45'''/bin/.
 
You have to adjust the source folder /usr/lib/jvm/'''jdk1.7.0_45'''/bin/.
  
 
* Adjust symlink
 
* Adjust symlink
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
rm /usr/lib/jvm/default-java
 
rm /usr/lib/jvm/default-java
 
ln -s /usr/lib/jvm/jdk1.7.0_45/ /usr/lib/jvm/default-java
 
ln -s /usr/lib/jvm/jdk1.7.0_45/ /usr/lib/jvm/default-java
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 
You have to adjust the source folder in LS command.
 
You have to adjust the source folder in LS command.
  
 
* Set system default version
 
* Set system default version
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
update-alternatives --config java
 
update-alternatives --config java
Line 114: Line 145:
  
 
* Check result
 
* Check result
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
java -version
 
java -version
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
  
 
===Environment variable JAVA_HOME===
 
===Environment variable JAVA_HOME===
 +
 +
 
You need to add an environment variable JAVA_HOME, if it does not already exist:
 
You need to add an environment variable JAVA_HOME, if it does not already exist:
 
* Debian
 
* Debian
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vim /etc/profile
 
vim /etc/profile
Line 127: Line 163:
  
 
* Ubuntu
 
* Ubuntu
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vim /etc/environment
 
vim /etc/environment
Line 132: Line 169:
  
 
Add the following line:
 
Add the following line:
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
export JAVA_HOME="/usr/lib/jvm/default-java"
 
export JAVA_HOME="/usr/lib/jvm/default-java"
Line 137: Line 175:
  
 
Update PATH:
 
Update PATH:
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
if [ "`id -u`" -eq 0 ]; then
 
if [ "`id -u`" -eq 0 ]; then
Line 148: Line 187:
  
 
==OpenJdk==
 
==OpenJdk==
 +
 
It’s always good to setup OpenJDK as well, especially for the old versions as there is more updates than the ORACLE version.
 
It’s always good to setup OpenJDK as well, especially for the old versions as there is more updates than the ORACLE version.
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
apt-get install openjdk-6-jdk openjdk-7-jdk
 
apt-get install openjdk-6-jdk openjdk-7-jdk
Line 155: Line 196:
  
 
==Choose default java version==
 
==Choose default java version==
 +
 +
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
update-alternatives --config java
 
update-alternatives --config java
Line 164: Line 207:
  
 
==Reminders==
 
==Reminders==
 +
 +
 
Don't forget to adjust the JAVA_HOME in all JAVA related services:
 
Don't forget to adjust the JAVA_HOME in all JAVA related services:
 
* Tomcat: /etc/default/tomcat7
 
* Tomcat: /etc/default/tomcat7

Revision as of 17:13, 10 June 2014


“Java” is one of the most popular and common language in this world.

It's everywhere, and you need some library to execute these programs.


Get Java

2 versions of Java are available:

  • JRE as Java Runtime Environment. For end-users. Download JRE: https://java.com/getjava‎
  • JDK as Java Devolpment Kit. For developers and application servers.

The JDK includes the corresponding JRE. Download JDK: http://www.oracle.com/technetwork/java/javase/downloads/index.html

You need to install the JDK on your development station & servers.



Windows installation

1. Create a new environment variable

  • Name: JAVA_HOME
  • Value: path to your JDK installation (C:\Program Files\java\jdk1.7.0_45)


2. Update the PATH

  • Add following sequence to the end of the PATH variable:
      > %JAVA_HOME%\bin


3. Check installation

  • Open a new terminal console [Run -> "cmd"]
  • Run: java -version


Linux installation

On a server, I advise you to put the JDK instead of the JRE. Many services requires the JDK such as JMX.

While on Windows ORACLE java is almost the only JVM available, on Linux you can choose your JVM vendor: OpenJDK, ORACLE, .... I advise you to use the official ORACLE library, instead of the OpenSource OpenJDK due to stability issues and JavaFX2 compability.


ORACLE JVM automatic installation (ubuntu)

This is for Ubuntu based distributions only.


Add a new repository:

add-apt-repository ppa:webupd8team/java
apt-get update


Install ORACLE JDK packages:

apt-get install oracle-java7-installer oracle-jdk7-installer

This will download the latest JDK, install it automatically and set the new JVM as the default one.


To remove the repository:

apt-add-repository --remove ppa:webup8team/java



ORACLE JVM manual installation (debian)

Download JDK

You've to retrieve the JDK / JRE from the ORACLE website

export ORACLE_JVM_URL='http://download.oracle.com/otn-pub/java/jdk/7u45-b18/jdk-7u45-linux-x64.tar.gz'
export ORACLE_JVM_FILENAME='jdk-linux-x64.tar.gz'
wget -c --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F" \
$ORACLE_JVM_URL --output-document=$ORACLE_JVM_FILENAME

You have to adjust the ORACLE_JVM_URL to the exact JVM you want to use.


Installation

  • Get binaries
tar -xzvf $ORACLE_JVM_FILENAME
mv jdk1.7.* /usr/lib/jvm
cd /usr/lib/jvm
  • Register new version
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_45/bin/java 1065
update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.7.0_45/bin/javac 1065
update-alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.7.0_45/bin/jar 1065
update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.7.0_45/bin/javaws 1065

You have to adjust the source folder /usr/lib/jvm/jdk1.7.0_45/bin/.

  • Adjust symlink
rm /usr/lib/jvm/default-java
ln -s /usr/lib/jvm/jdk1.7.0_45/ /usr/lib/jvm/default-java

You have to adjust the source folder in LS command.

  • Set system default version
update-alternatives --config java
update-alternatives --config javac
update-alternatives --config javaws
update-alternatives --config jar
  • Check result
java -version


Environment variable JAVA_HOME

You need to add an environment variable JAVA_HOME, if it does not already exist:

  • Debian
vim /etc/profile
  • Ubuntu
vim /etc/environment

Add the following line:

export JAVA_HOME="/usr/lib/jvm/default-java"

Update PATH:

if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$JAVA_HOME/bin"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:$JAVA_HOME/bin"
fi
export PATH


OpenJdk

It’s always good to setup OpenJDK as well, especially for the old versions as there is more updates than the ORACLE version.

apt-get install openjdk-6-jdk openjdk-7-jdk


Choose default java version

update-alternatives --config java
update-alternatives --config javac
update-alternatives --config javaws
update-alternatives --config jar


Reminders

Don't forget to adjust the JAVA_HOME in all JAVA related services:

  • Tomcat: /etc/default/tomcat7
  • Eclipse IDE: eclipse.ini
  • Jenkins
  • etc.