Despite its popularity, Java has been evolving throughout the years growing more versatile and efficient as the time passes. Keeping in view the importance of the Java language, this guide provides procedural guidelines to get start working with Java:
The following are the learning outcomes of this post:
- Setting up Java environment on your PC
- Building a simple Java Program
How to setup environment for Java
The first and foremost step to start working with Java is setting up the environment. A sequential process is carried out to create a Java environment.
JDK(Java Development Kit): A Java development kit that contains many tools for developing and debugging Java programs.
JRE(Java Runtime Environment): To run Java programs, you must acquire JRE on your system. It comprises libraries and supporting files that provide support for executing the source code. JRE is a subset of JDK and it does not require any dedicated installation as JRE is installed with JDK automatically.
JVM(Java Virtual Machine): The process of converting the byte code to machine language is carried out by using JVM. It cannot be installed separately and depends on JRE (and JRE depends on JDK).
So, we have represented the relationship of JDK, JRE and JVM in the following equation:
Note: The above equation shows that the JVM is subset of JRE and JRE is subset of JDK.
How to install JDK on Windows
As discussed earlier, the working of Java highly depends on the Java Development Kit.
Step 1: Download the JDK installer
To download latest .exe file of JDK, navigate to oracle.com and choose the file to download:
Note: At time of writing this article, JDK 17 is the latest version available.
Step 2: Open the JDK installer
Once the installer is downloaded, open that installer and click on Run to start the installation steps:
– After that a wizard will be displayed as shown below; click on Next to proceed further:
– Choose the installation path for JDK and navigate to Next to start the installation:
– The installation will be started and would take several minutes to complete: Upon successful installation, the following wizard will be popped up: Click on Close to get out of this wizard:
How to set Environment Variables for Java (Optional)
After installation of JDK, it is recommended(not necessary) to set environment variables for Java that eases the working mechanism for Java. We recommend setting the following environment variables:
You have to set two environment variables named PATH(that gives the location of executables like java and javac) and CLASSPATH(provides the location of library files).
To do so, follow the steps provided below:
Step 1: Open the settings by searching it from the taskbar:
Step 2: Search for “Advanced System Settings” and click on “view advanced system settings“:
Step 3: Once it is opened, click on Environment Variables:
The following interface will be displayed; click on New to proceed further:
Step 4: upon clicking on New, you have to assign a variable name and its path: here we have set variable name PATH and the location of bin directory is pasted in location text box as displayed in the image below. Click on OK to complete the creation of PATH environment variable:
Step 5: To add class path variable; repeat the step 4 and change the variable name(as per your choice) and copy the location of lib directory(of JDK):
After doing the above steps, you will observe that both variables are created successfully as can be seen in the image below:
Once done with the above steps, you have created two environment variables that would assist in getting smooth execution in Java.
How to install Eclipse for Java
Eclipse is an Integrated Development Environment for Java and is used to develop applications using several languages such as Java, Python, C++ etc. In Java, Eclipse requires JDK to be installed locally on your machine(we have done it already). We have chosen Eclipse IDE to run Java and this section provides the installation guide of Eclipse for Windows, so follow the steps performed below:
Step 1: Download the Eclipse Installer
Navigate to eclipse.org and download the latest Eclipse version available.
Once the installer is downloaded, open it (where you have saved the downloaded file):
Step 2: Start installing Eclipse
The time you click on Eclipse installer; you will observe the following interface: For Java developers, click on the highlighted option:
Go with the default location of installation and click on Install as displayed below:
Accept the licensing terms and conditions of Eclipse:
The installation may take few minutes and upon successful installation the Launch button will be displayed as shown in the image below:
Now you have installed all necessary tools that are recommended to start working with Java.
How to run Java using Eclipse
After going through the above sections of this guide, you can now write and run Java programs using the Eclipse IDE. This section provides a a few steps to write your first program in Java using Eclipse:
Step 1: Launch the Eclipse IDE
First, it will ask you to select your workspace folder(as can be seen below):
Step 2: After clicking on Launch, it would ask you to select what you are here to do (click on Create New Java Project):
Choose a project name (we have set it to linuxhint) and click on Finish to start working:
Step 3: From the left window pane of the linuxhint project, click on src and create a new package:
We have created a package and named it newpack:
Step 4: Now right-click on that newly created package and create a new class:
The class name is set to linux in this guide:
After successful creation of the class, you will observe the following interface where the structure of the class will automatically be imported:
Step 5: Here we have created a method that prints “Welcome to linuxhint“:
Step 6: To run this program, click on the green run button and the output will be shown in the console of Eclipse:
Here you go!
In the earlier part of the guide, we have successfully set the Java environment and lastly we have created our first Java program as well.
Conclusion
Java is a well-known programming language used to develop various categories of applications on multiple platforms. To start working with Java, you must have the knowledge to set up a Java environment. This guide is compiled to provide a procedural demonstration on getting started with Java. You have learned the environment setup of Java that includes installing JDK(Java Development Kit); setting up the environment variables; installing Eclipse IDE(for Java). The environment variables are not necessary however they are recommended to get smooth execution of Java code.