You are here
Home > java > Core Java >

How to run a Java program in Windows 10+ ?

How to run a Java program in Windows 10+? : Java Setup 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.

How to Set JAVA_HOME

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

How to run a Java program in Windows 10+? : Java Setup

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.
UsersJBI>java -version

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.

public class FirstProgram {ย ย 
ย ย public static void main(String args[]) {ย ย ย 
ย ย ย ย ย System.out.println("Hello Java Tech Online!");ย ย ย ย 
ย }
}

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+ ?โ€™.

Leave a Reply


Top