Wednesday, December 31, 2014

How to Install Oracle Java and Android Studio on Mint 17.1

This article is a 'how-to' for installing Oracle's Java JDK 8 (or SE 8) and Google's Android Studio on Linux Mint 17.1 (Rebecca). If you wish to develop software on the Android platform on your Mint 17.1 PC, then this 'how-to' is for you. Note that most necessary steps are performed at the command line in a terminal - if you are not familiar with this, I suggest reading about it first before trying out this 'how-to'.

On a newly-installed and updated Linux Mint 17.1, only OpenJDK JRE (Java Runtime Environment) 7 is installed by default. This can be verified by executing the following in a terminal:

  java -version

The output is as follows:

  java version "1.7.0_65"
  OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-0ubuntu0.14.04.1)
  OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)


Although whether the JRE or JDK version of OpenJDK 7 is installed by default is not clearly indicated in the output, it can be easily verified by executing the following:

  update-alternatives --list java

The output is as follows:

  /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java

This clearly indicate that only the JRE is installed by default. Google's Android Studio requires the full Java JDK, and the recommended version is Oracle's Java (JDK or SE 7 and above). So, you can either delete the OpenJDK 7 JRE and install Oracle's Java JDK 8 in it's place, or install Oracle's Java JDK 8 and keep the OpenJDK 7 JRE. Either way you have to tell Linux Mint which Java is the default by using the 'update-alternatives' command. This 'how-to' implements the second option - install Oracle's Java JDK 8 and keep the OpenJDK 7 JRE.

Point your web browser to the Oracle SE download page here and download the latest version of the  32-bit or 64-bit JDK (not Server JRE or the JRE) - currently at version 8u31 (or 1.8.0_31). It is also recommended that you download the API docs and demos (+samples) for the JDK - although this is not required for Android development on Android Studio. Once the packages have been downloaded, execute the following commands:

  cd Downloads
  tar xzvf jdk-8u31-linux-x64.tar.gz
  tar xzvf jdk-8u31-linux-x64-demos.tar.gz
  unzip jdk-8u31-docs-all.zip -d jdk1.8.0_31


The last 2 commands in the above is only applicable if you have downloaded the demos and the documentation. The 2nd command above will create a 'jdk1.8.0_31' directory in the Downloads directory while the last 2 commands above (if applicable) will create the 'sample', 'demo' and 'docs' directories under the 'jdk1.8.0_31' directory.

Now execute the following commands:

  sudo mv jdk1.8.0_31/ /usr/lib/jvm/
  cd ~


This will move the whole jdk1.8.0_31 directory to the /usr/lib/jvm directory - where the root of the current OpenJDK 7 JRE resides. If you are curious (like me), you can do a 'ls -la /usr/lib/jvm' command to find out what resides in this directory. Don't be surprised to find both the OpenJDK 6 and OpenJDK 7 directories listed there - why I don't know. Notice also that there is also a 'default-java' link which currently points to java-1.7.0-openjdk-amd64. This link is actually set by the 'default-jre' and the 'default-jre-headless' package which are installed by default. To remove this link, execute the following command in a terminal:

  sudo apt-get remove default-jre default-jre-headless

Notice that the owner and group of the jdk1.8.0_31 directory is still in your name and group. You can keep it as it is if you are the only user of the PC or you can set this to root by executing:

  sudo chown -R root:root /usr/lib/jvm/jdk1.8.0_31

The next thing to do is to tell Linux Mint where Oracle Java JDK resides. For this we use the 'update-alternatives' command. Execute the following commands one by one from the terminal (note that all commands are on a single line):

  sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_31/bin/java" 1500 --slave "/usr/share/man/man1/java.1" "java.1" "/usr/lib/jvm/jdk1.8.0_31/man/man1/java.1"
  sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_31/bin/javac" 1500 --slave "/usr/share/man/man1/javac.1" "javac.1" "/usr/lib/jvm/jdk1.8.0_31/man/man1/javac.1"


These will install all the necessary links to Oracle's Java. Note that the 2nd command is strictly not essential to Android program development - it only sets up the link for the Java compiler.

Next we have to tell Linux Mint the default Java to use. Just remember that Open JDK 7 Java is the default Java for a newly installed Linux Mint 17 system - however, since we installed the above links with a priority of 1500 (higher than OpenJDK's 1075), the default Java was automatically set to Oracle's Java.

To verify that all have been set correctly, you can use the following commands:

  update-alternatives --display java
  update-alternatives --display javac


If the output to the two queries above contains the following lines:

  link currently points to /usr/lib/jvm/jdk1.8.0_31/bin/java
  link currently points to /usr/lib/jvm/jdk1.8.0_31/bin/javac


then everything have been set correctly. Just another quick check can be performed by executing the following command:

  java -version

If the output for the command is:

  java version "1.8.0_31"
  Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
  Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)


then Oracle JDK 8 has been set up properly.

Now that Linux Mint has been informed about Oracle's Java, you still have to perform one last task before Java is set up and ready for use. This is to set the environment variables and paths correctly for the whole system. Execute the following command:

  sudo nano /etc/profile

This will execute the nano text editor and open the /etc/profile file for editing. Now add the following lines at the end of the file.

  export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_31
  export PATH=$PATH:$JAVA_HOME/bin


Save the file and close it. Note that, upon system startup, these will set the JAVA_HOME and PATH environment variables for the whole system. The JAVA_HOME environment variable points to the JDK root while the PATH environment variable will add the /bin directory of the JDK to whatever path that has already been specified. This will enable the 'javac' command to be executed from any directory.

Now log out and then log in again to the Linux Mint system in order to make sure that both the environment variables are set. Check by opening a terminal and executing one by one:

  echo $JAVA_HOME
  echo $PATH


You can then delete the tar.gz and zip files which were downloaded from the web in order to free up disk space.

Now that you have installed Oracle JDK 8 on your system, it's time to download and install Google's Android Studio. Point your browser to here and click on the big green button and then on android-studio for Linux package (NOT the SDK tools only). Wait for the download to finish and then start a terminal and execute the following command:

  unzip Downloads/android-studio-ide-135.1641136-linux.zip

This will decompress the contents of the zip file downloaded into a new directory called 'android-studio'. To start Android Studio setup, execute the following commands, one by one:

  cd android-studio/bin
  ./studio.sh


A window similar to the one shown below will appear asking whether you would like to import any settings. If this is the first time you are using Android Studio, it will be likely that you don't have anything to import. However, if you are moving from an older version of Android Studio, you may want to keep any settings made previously - in this case specify the location of the settings and let the setup wizard do the job.


Click on the OK button to proceed. Android Studio will begin loading as shown in the figure below.


After a while, the Setup Wizard screen similar to the one shown below, will appear. Click on the Next button to continue.


The next screen similar to the one shown below will appear.


The Standard type of setup is already selected - this is recommended unless you want to customize the setup. Click on the Next button and the the next screen will appear.


If you are using Linux Mint 17.1 and you have Virtualization Technology (vt-d) enabled in your PC BIOS and Linux kernel, then it will be detected as shown on the wizard's screen. You can read up more on this technology at the URL shown on the screen - just remember that the web page there is for Windows only - to read more on Linux and Virtualization Technology, click here. Then click on the Next button.

The next screen that appears as shown below is where you agree to accept the license for installing Google's Android SDK. Note that this is a must and cannot be skipped if you want to develop software for the Android platform. Click on Accept and the the Finish button.


The SDK components will begin downloading as shown in the screen below.



Once downloading and installation is completed, a log window will appear as shown. Click on the Finish button.



The next screen shown is the Android Studio IDE itself (shown below). You have now successfully installed Android Studio on your Linux Mint 17.1 PC.



Notice the 'Check for updates now.' on the IDE window - click on Check and if there are any updates, a window similar to the one shown below will pop up.



Click on the Update and Restart button. Android Studio will close and the updates will begin downloading as shown.



Once the updates have been installed, Android Studio will start again.

Congratulations! You have successfully installed Oracle's Java and Google's Android Studio on your Linux Mint 17.1 PC. Remember that, at the moment, you have to start a terminal, change to the correct bin directory and then launch studio.sh in order to start Android Studio. Surely there must be an easier way and there is! To place a launcher icon on your desktop, follow the steps outlined below:
  1. Right-click on an empty portion of your desktop and select 'Create a new launcher here...'
  2. A window similar to the one shown below, will appear. You an now enter a name for the item - 'Android Studio' seems appropriate here. Then for the command, click on the Browse button and select the 'studio.sh' file in your home directory. In the comment field, enter any text you like - 'Google's Programming IDE for Android' seems appropriate here. Leave the 'Launch in Terminal?' field unchecked.
  3. For the icon, click on the rocket icon, and navigate to your home directory, double-click on android-studio and bin directories and then select the idea.png file. Then click OK, followed by another OK.



A window may pop up asking you whether you want to create a menu item for Android Studio also. Should you say 'Yes', that item will be placed in the 'Other' category but will not be visible until you log off and log in again. You can test the launcher by double-clicking on it.




Where to go from here? You must realize that Android Studio is relatively new (Google was using Eclipse IDE previously) so the number of good tutorials on the web is limited. However, a quick search on Google revealed some good sites - I recommend that you visit some of the sites and see whether they meet your needs.

Happy computing!



EDIT: This blog was edited to use the latest Java from Oracle - Java SE update 31. In addition, the 'update-alternatives' command were changed to use 1500 as the 'priority' option, in order to set the system to use Oracle's Java automatically. Because of this change, the 'update-alternatives --set' commands were removed. Also, it was found that it was unnecessary to remove the links to the OpenJDK 7 Java - the 'update-alternatives' command will take care of which Java to use by default.

Some of the 'update-alternatives --list' commands were replaced by the 'update-alternatives --display' commands as these were more appropriate in these situations.

Strictly speaking, it is not necessary to set the PATH environment variable to include the Java bin directory in order to execute the 'javac' command, but I leave this to user to decide. No harm will come to the system if you do this. The rest of the blog remains unchanged.

Take note that there are more Oracle Java commands that can be set by using 'update-alternatives', but the minimum required by Android Studio is given in this blog. For those who wants to set up a Oracle Java programming environment, read my blog here.



9 comments:

  1. Exactly what i need. You just saved me a lot of -googling for these information separately-.

    ReplyDelete
  2. thank you very much! Android studio works now!

    ReplyDelete
  3. Could you please advice what to do?
    I installed latest jdk1.8.0-60
    after update linux mint 17.1 via update manager there is message "the following package will be removed: jdk1.8.0-51", (it is previously installed JDK) I click "OK" and get following error
    "E: jdk1.8.0-51: subprocess installed post-removal script returned error exit status 127"

    ReplyDelete
  4. solved by removing
    jdk1.8.0-51.conffiles
    jdk1.8.0-51.md5sums
    jdk1.8.0-51.postrm
    jdk1.8.0-51.list
    jdk1.8.0-51.postinst
    jdk1.8.0-51.prerm

    from /var/lib/dpkg/info

    ReplyDelete
  5. Thanks for the detailed instructions

    ReplyDelete
  6. Thanks a lot.. a Question.. wht if I purge openjdk rather than just remove ? do I lost something with libreOffice or something ? thanks

    ReplyDelete
    Replies
    1. That depends on whether LibreOffice uses Java and which Java. I suggest that you find out by launching LibreOffice and checking. As far as I know, it can automatically detect which Java you are using and uses that as well - but I could be wrong.

      Delete
    2. That depends on whether LibreOffice uses Java and which Java. I suggest that you find out by launching LibreOffice and checking. As far as I know, it can automatically detect which Java you are using and uses that as well - but I could be wrong.

      Delete
  7. Hello thank you for this tutorial , but I have a problem with update-alternatives when I type the command it does not. it tells me to type update-alternative

    ReplyDelete