How to Install Java in Windows 10?
Java is one of the most widely used programming languages. To execute code in any programming language, we need to set up required environments like editors, software, and IDEs. JDK (Java Development Kit) is the complete environment required to run java codes on any operating system. IDE(Integrated Development Environment) like Eclipse and IntelliJ, makes coding and developing complex projects easier with their user interfaces, automation tools, and debugger.
Java is a high-level, robust programming language and a platform. It was developed by James Gosling at Sun Microsystems that was released in 1995. Any hardware or software environment in which a program runs is known as a platform. Since Java has a runtime environment (JRE) and API, it is called a platform.
To run any code and develop applications in java language, we need to install JDK(Java Development Kit). JDK contains tools required for development, and JRE(Java Runtime Environment), which contains all libraries, and JVM(Java Virtual Machine) is required to execute the programs.
JDK can be installed on different operating systems including Linux, Windows, and macOS. Here, we will see detailed steps of installations of JDK SE 16 on the Windows 10 operating system.
Which One Do We Need: JVM or a JRE or a JDK?
While installing Java, we come across different terms like JVM, JRE, JDK. These are different software bundles available for running java programs with different functionalities. To understand what we need to install JVM, JRE or JDK let's understand these terms.
1. Java Virtual Machine (JVM)
- JVM(Java Virtual Machine) is an abstract machine. It is a specification that provides a runtime environment in which java bytecode can be executed.
- When we run any Java program, the online Java compiler first compiles your Java code into bytecode. After compilation JVM translates bytecode into machine code, i.e. machine-level instructions.
- Java is known as a platform-independent language because programs written in Java are executed by JVM and not by the physical machine.
- To execute any Java program user has to first compile the code to convert Java code into byte code and then run it. The compiler creates a .class file after successful compilation.
- After compilation, the byte code given in the .class file by the compiler is converted into machine-level instructions by JVM, and output is given to the user.
Syntax to compile Java program:
Syntax to run Java program:
Here java command activates JVM.
- Flow of program execution is done as follows:
2.Java Runtime Environment(JRE)
- Java Runtime Environment (JRE) is an open-source software distribution with a Java class library, specific tools, and a JVM.
- It is the most common environment for devices running Java applications. JRE can be used when a user has to use Java applications but does not do development.
- Java source code is compiled and translated into Java bytecode. If you want to get output from this bytecode, you need JRE.
- It loads classes, checks memory access, and finds the system resources needed to run applications.
- JRE acts as a software layer over the operating system. It is one of the related components of Java Development Kit(JDK) and superset of JVM.
3.Java Development Kit(JDK)
- JDK is a software development kit required to develop applications in Java.
- JDK consists of development tools like a compiler and debugger in addition to JRE.
- It contains JVM, JRE, and additional development tools like compiler, interpreter, debugger, and many more things required for application development.
- JDK makes it easy for developers to develop applications.
Points to Remember while Downloading:
If you're a user looking to run a Java application, you only need the JRE(which includes a JVM.)
If you're a developer looking to program in Java and use any IDE for the same, you need the JDK(which includes JRE libraries, which in turn includes a JVM.)
From the above explanations, we know that JDK contains all the required components(JVM, JRE, compiler, and debugger) to execute the java program. So, we will download and install JDK 16.
NOTE: JDK 17 is the latest version available, but we will go with JDK 16 so that we don't face any challenges during installation.
Installing JDK on Windows
Step 1: To download Java, you should have an account on oracle.com. You can sign in if you have one, or else you can sign up and create an account.
Step 2: Go to the site of Java Oracle using this link and download the latest version of JDK 16 from oracle.com for windows.
NOTE: Before downloading check whether your system is 32-bit or 64-bit and download accordingly. In my case, it's 64-bit.
Step 3: Clicking on the installation link will take you to a popup window as shown below. Click on I reviewed and accept the Oracle Technology Network License Agreement for Oracle Java SE development kit and click on the download button. It will take you to the login page of oracle.com, log in and start downloading the software.
Step 4: Once the JDK 16 download is complete, run the .exe file on windows to start installations. First allow windows to make necessary changes during installation.
Begin the installation by clicking on next.
Step 5: Select the PATH where you want to install Java in Windows. Leave it default and click on Next.
Step 6: After installations are done, click on Close.
Step 7: After installation, we need to set environment variables PATH and CLASSPATH to run a program. Path and Classpath both are operating system level environment variables. Path is used to define where the system can find the executables(.exe) files and classpath is used to `specify the location of .class files.
- The PATH variable gives the location of executables like javac and java that are stored in the bin folder of Java.
- When you try to execute a program from the command line, the operating system searches for the specified program in the current direction and executes if it's available.
- In case the programs are not available in the current directory, the operating system verifies in the set of directories specified in the ‘PATH ’ environment variable.
- The CLASSPATH variable gives the location of library files that are stored in the lib folder of Java.
- This environment variable is used to specify the location of the classes and packages.
- When we try to import classes and packages that are not available in Java Standard Library.
- JVM verifies the current direction for them, if not available it verifies the set of directories specified in the CLASSPATH environment variable.
To set these environment variables right-click on this PC and go to properties.
Step 8: Click on Advanced System Settings
Step 9: Go to environment variables and click on New.
Step 10: Create a new PATH variable and set its path to the bin folder, which is in the installed JDK folder.
Copy the address of the bin folder in JDK.
Paste it in path variable value.
Step 11: Follow the same steps to create the CLASSPATH variable. Its variable value will be set to the lib folder in JDK.
Step 12: The PATH and CLASSPATH variables are set. Click on OK.
Here the installation of JDK 16 is complete and environment variables are set.
Verifying the Setup
After installation, verify if it is working properly by checking the java version on cmd.
Syntax: java -version
If it shows the java version we have installed, then the installations are successful.
Installing the IDE
- Integrated development environment(IDE) is a software suite that comprises basic tools like editor, compiler, debugger to develop applications.
- Java has various IDE but mostly used is Eclipse IDE.
- It has all the necessary tools required for application development and testing.
- One of the best perks of using Eclipse IDE is its user-friendly user interface(UI).
Here are the Steps to Install One of the Famous IDE Eclipse :
Step 1: Download Eclipse Installer from the link and run .exe file.
Step 2: Click on install.
Step 3: Accept the license by clicking on Accept Now.
It will start the installation.
Step 4: Click on the Eclipse IDE shortcut to open the IDE.
Step 5: Select the workspace to run the project. You can create a new workspace(folder) and give its location.
Now, the Eclipse IDE is ready to create your first Java Program.
Write Your First Java Program on Windows
Let's write our first Java Program that prints "Hello World!"`
Step 1: Write a simple program using Java that prints "Hello World!" in any editor. (Here, we have used notepad.)
Note: Java is an Object-Oriented Programming language which works using classes and objects. And a basic requirement to run any java program is that it must have at least one class.
Step 2: Save the file with any name using the .java extension.
Note: While saving files, remember that the class name in the program and file name should be the same.
Step 3: Open the command prompt from the folder where your program file is saved.
Step 4: Run command
to compile the java program. After successful compilation of the program command prompt will be ready for another command.
Step 5: To execute the program and get output on the command prompt screen run the command
Step 6: And after execution, you will see your output on the screen.
- Java needs JDK which is a superset of JRE and JVM to execute programs.
- javac command is used to compile java code and java command is used to execute it.
- java command activates JVM, which is responsible for executing java programs.
- After successful installation, we need to set environment variables to execute java commands.
- IDE consists of the smart editor and other applications which are helpful in developing and debugging Java applications easily.