Install Java on Windows, Mac and Linux/Ubuntu platforms

We have already studied in our previous article about Java and its versions. In this guide we’ll study how to install java on Windows, Mac OS, Linux/Ubuntu, and execute example using command prompt(CMD).

Before installing you should know that Java contain three main component JDK, JRE and JVM.

Java Virtual Machine: JVM is an important part of both JDK and JVM as it is inbuilt in both which execute program/code line by line.

Java Runtime Environment: JRE is minimum requirement to run Java programs. It contains set of tools, libraries, core classes, and supporting files.

Java Development Kit: JDK is application development environment means you’ll be able to write programs and execute it. It includes JRE, Java(interpreter), Javac(compiler), JVM and other tools required to develop applications.

Which one should you install java JDK or JRE?

Answer is simple, if you want to run Java program/application JRE is enough and if you are developing and running a program install JDK.

Let’s start.

Install Java on Windows

1. Visit the link Java version 8 update 191 or Java 11 (available only for 64 bit windows).

JDK download page
JDK download page

2. Accept license agreement and click on required application

Install java jdk-select jdk
Select JDK

3. Double click on downloaded file to install Java.

4. Change directory if you want or leave for default(recommended) and click Next.

install java on windows 1
installation wizard

5. Wait till installation process get completed  and than click OK.

install java on windows 2
installation wizard 2

6. Now again a JRE installation starts, you only need to click NEXT (if you changed directory for JDK set this to same path or else leave it).

install java on windows 3
JRE installation wizard

7. Wait for installation and Click Close. Installation of java JDK completed.

install java on windows 4
Closing Wizard

8. Now we have to set path variable to run programs.

9. Open directory where JDK got installed. Open bin folder and copy path (Ex. C:\Program Files\Java\jdk1.8.0_191\bin).

install java on windows 5
Java installed path

10. Right click on My computer or This PC for windows 10 and select Properties to open system properties.

11. Click on Environment variable and select PATH in system variable then click EDIT.

install java on windows 6
set path variable

12. Click NEW and paste path here. Click three times OK to exit.

install java on windows 7
set path

13. JDK is installed and path is set. Let’s verify it and execute an example.

14. Open Command Prompt (CMD) and type Java –version, it shows

C:\Users\xxx>java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) Client VM (build 25.191-b12, mixed mode)
C:\Users\xxx>

Everything installed and set correctly.

Executing Simple Java program

1. Now open a notepad and copy below Java program to it and save it with .java extension (externcode.java) in newfolder.

class classname {
 public static void main(String args[]){
     System.out.println("externcode.com");
 }
}

2. Go back to command prompt and change the directory to where the file is saved I saved it on desktop in new folder.

C:\Users\XXXXX>cd desktop/newfolder 

C:\Users\XXXXX\Desktop\newfolder>

3. To compile the code enter the following commands

C:\Users\XXXXX\Desktop\newfolder>javac externcode.java 

4. Here nothing will appear and you can see a new file with classname.class got created.

5. To see output type on new line

C:\Users\XXXXX\Desktop\newfolder>java classname
externcode.com

if you get the same output as above than Java JDK is successfully installed your windows.

Install Java JDK on Mac OS

1. To install Java on mac click (JDK version 8 update 191).

2. Visit the link choose your version and click on (shown GREEN mark). On new page accept license agreement and click on jdk-11.0.1_osx-x64_bin.dmg or on jdk-8u191-macosx-x64.dmg it to download.

install Java on mac 1

3. Open file and Double click on application to start installation and follow instructions to install Java JDK. On popup you see instruction Click Continue.

open Java JDK

4. Change directory if you want or leave for default(recommended) and then click Continue. This will install JDK for all users click Next.

install Java on mac 2

5. In next popup it shows space required. Click Install and enter the password your mac password, click Install Software.

install Java on mac 3
install Java on mac 4

6. Wait for the package to get installed. Click close.

install Java on mac 5
Install Java on mac 6

7. Java JDK is installed on your Mac let’s verify it and perform an example program.

8. Open Terminal and enter Java -version command.

yourmacxxx: ~username$ java -version 
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) Client VM (build 25.191-b12, mixed mode)

9. Above output show JDK installed correctly.

Let’s try simple Java program

1. Now open TextEdit, before start writing we need to set editor for java codes.

2. Open preference in TextEdit and in New Document tab, change the format to Plain Text under the Format section. Uncheck the Smart quotes box in Options section.

3. Now Switch to Open and Save tab change the Opening files and Saving files to Unicode (UTF-8) than close the TextEdit application and re-open it.

set textedit

4. Open a new document and copy below code save to desktop as with .java extension (ex. externcode.java).

class externcode {
 public static void main(String args[]){
     System.out.println("externcode.com");
 }
}

5. Now again open Terminal enter the following commands

yourmacxxx:~ username$ cd Desktop
yourmacxxx: Desktop username$ javac externcode.java

6. You can notice on desktop a file externcode.class got created. To get output enter below command and hit Enter.

yourmacxxx: Desktop username$ java externcode
Externcode.com

If you get the output as above, then Java JDK is successfully installed java on your mac.

Install Java on Linux/Ubuntu

1. To download and Install Java JDK by clicking the link jdk-8u191-linux-x64.tar.gz and follow the steps shown above to download.

2. Open the terminal (Ctrl + Alt + T) and enter

User@ubuntuxx:~$ sudo mkdir /usr/lib/jvm

3. If the /jvm folder does not exist, this command will create the folder directory. If you already have this folder, you can leave this step.

4. Change the directory by entering following command.

cd /usr/lib/jvm

5. Extract the jdk-8u191-linux-x64.tar.gz file in that directory with command.

sudo tar -xvzf ~/Downloads/jdk-8u191-linux-x64.tar.gz

6. Open the environment variables file with command and add the following bin folders to the existing PATH variable separated by semicolon.

sudo gedit /etc/environment
/usr/lib/jvm/jdk1.8.0_191/bin
/usr/lib/jvm/jdk1.8.0_191/db/bin
/usr/lib/jvm/jdk1.8.0_191/jre/bin

7. Add environment variables given below at the end of the file.

J2SDKDIR="/usr/lib/jvm/jdk1.8.0_191"
J2REDIR="/usr/lib/jvm/jdk1.8.0_191/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_191"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_191/db"

8. Environment file after the paths.

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_191/bin:/usr/lib/jvm/jdk1.8.0_191/db/bin:/usr/lib/jvm/jdk1.8.0_191/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_191"
J2REDIR="/usr/lib/jvm/jdk1.8.0_191/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_191"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_191/db"

9. Save and close the gedit.

10. Inform the Ubuntu about the Java’s location with following commands.

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk1.8.0_191/bin/java" 0

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk1.8.0_191/bin/javac" 0

sudo update-alternatives --set java /usr/lib/jvm/jdk1.8.0_191/bin/java

sudo update-alternatives --set javac /usr/lib/jvm/jdk1.8.0_191/bin/javac

11. Now let’s verify it with commands and try an example of Java program.

update-alternatives --list java

update-alternatives --list javac

12. Restart the computer and open the terminal again. Enter the following command.

user@ubuntuxx: ~ $ java –version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) Client VM (build 25.191-b12, mixed mode) 

If you get the installed Java version as the output, you have successfully installed the JDK.

Let’s try simple Java program.

1. Open a new document and copy below code save to desktop with .java extension (ex. externcode.java).

class externcode {
 public static void main(String args[]){
     System.out.println("Externcode.com");
 }
}

2. Now again open Terminal enter

user@ubuntuxx: ~$ cd Desktop
user@ubuntuxx: ~/Desktop$ javac externcode.java

3. A new file with externcode.class extension created. Now enter following for output and hit enter.

user@ubuntuxx: ~/Desktop$ java externcode
Externcode.com

We have successfully learned how to install Java JDK on windows, Linux, Mac and also executed a simple example. Next guide will be installing required Java IDE’s and their configuration for larger programs.

Ok guys, thats enough for now I am exhausted. Don’t forget to share and subscribe to the newsletter.

Leave your feedback and questions in comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: