An essential target as a new learner of a programming language is running your first program. Java is a popular and flexible programming language known for its portability and wide range of applications. To run a Java program, we need to set up our development environment, write the code, and then execute it. In this article ”How to run a Java program in Windows 10+ ?”, we are going to discuss a summary of the complete steps involved in running our very first Java program.
If you are going to learn/work on Java, your first step is to install java on your computer/laptop and check whether you have complete environment to start running your Java Programs or not. Here, we will test our java environment in Windows 10+ platform. So the title of this article is ‘How to run a Java program in Windows 10+ ?’.
How to Download Java?
First step to java Setup is to make sure you have java installed in your system. Latest version of Java as a .exe file can be downloaded from Oracle Java Website. From Java 11 onward, we can just download .zip file.
How to install Java?
Once downloaded, Java can be installed like any other software(.exe) on Windows like clicking on exe file & following the instructions.
Note : Java 11 version onward, It’s not mandatory to do installation process. We just need to download zip file from Oracle site as per your operating system & unzip it like eclipse. We can also install it as before. Java 11 has only JDK to install, no need to install JRE separately. JRE automatically gets installed with JDK.
What is LTS (Long term Support ) version?
For product releases after Java SE 8, Oracle will designate a release, every three years, as a Long-Term-Support (LTS) release. Java SE 11 is an LTS release. Once a new feature release is available, any previous non‑LTS release will be considered superseded. For example, Java SE 9 was a non‑LTS release and immediately superseded by Java SE 10 (also non‑LTS). Java SE 10 in turn is immediately superseded by Java SE 11. However, Java SE 11 is an LTS release, and therefore Oracle Customers will receive Oracle Premier Support and also periodic update releases, even though Java SE 12 was released.
How to run a Java program in Windows 10?
Before running a Java program there are some primary steps to create runnable environment first. Let’s complete them step by step:
How to set Java Environment Variable in your system?
If you want to compile & run your Java Program anywhere from command line, you need to set environment variables. In order to set environment variable in Windows 10+ platform, search for “Computer” and then select “This PC” option and then click on “properties”. Now, from property window, select “ADVANCED SYSTEM SETTINGS”. In System Properties window click on “Environment Variables”. Now you will see Environment Variables window where you will find two sections : ‘User Variables’ and ‘System Variables’.
Further, in ‘User Variables’ section, click on ‘New” button. Fill below information to set JAVA_HOME variable as per your installed Java version. Please remember that the field Variable name must be JAVA_HOME, and the field Variable value must point to JDK’s installation directory on your computer. Finally, click on OK to close this dialog.
Furthermore, come back to the Environment Variables window, look for a variable called ‘Path’ under the System Variables list. Now click on ‘Edit’. Now, In the Edit environment variable dialog click on ‘New” and insert the path of your java installation directory till the bin folder to the end of the field Variable value as following : C:\Program Files\Java\jdk-13.0.2\bin
Click on OK three times to close all the dialogs.
How to Check if Java is Installed ?
To check if Java installed properly or not, open COMMAND prompt in windows OS. To open command prompt write “CMD” in run command and then hit enter. In command prompt window
write “java -version“.
If Java installed properly and all environment variables are ok, it will show version number of Installed JDK with the message as below.
java version "13.0.2" 2020-01-14 Java(TM) SE Runtime Environment (build 13.0.2+8) Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)
If there is any problem while installing or in setting environment variable output could be like below:
'java' is not recognized as an internal or external command, operable program or batch file.
If the above error occurs, it means that the java path is not set in your system.
How to set path in Windows system from command prompt?
In order to set the path, open command prompt (cmd). Now, go to the place where you have installed java on your system and locate the bin directory. Then copy the complete path and write it in the command like below.
set path=C:\Program Files\Java\jdk1.8.0_124\bin
How to write First Java Program ?
Here we will learn to write first Java program. Let’s start our first Java program with “Hello World”. But we will just change it to “Hello Java Tech Online!”. If everything till now is ok, our fist program will show the correct output. For example, Let’s open a text editor and write below program.
Now save the file with name “FirstProgram.java“. Once file saved open the command prompt and change directory where file is saved with cd command. And fire “JAVAC” command that we use to compile the java code as below.
♦ UsersJTO>cd C:JTO
♦ C:JTO>javac FirstProgram.java
If java file compiles properly, compiler will create a class file for the java source file. It will create it either in same location or other depending on package declaration. Since there is no package declaration in the code, therefore compiler will create the .class file in the same folder where .java file exists.
You can also use IDE like Eclipse to run the java program. We have covered that part in other article Eclipse IDE tutorial and it’s keyboard shortcuts. For the sake of simplicity, we have only used text editor and command prompt (or terminal) for this tutorial.
How to Run Java Program after java Setup?
Since java file compiled with “JAVAC” command. Therefore, we can now run the program. In order to run the above program, execute “JAVA” run command as given below.
♦ C:JTO>java FirstProgram
Output : Hello Java Tech Online!
Note*: here we are using only the class file name and not extension(.class). Here we have done with Creating and running our first Java program within our article ‘How to run a Java program in Windows 10+ ?’.