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