08 Aug Run Java First Program on Windows
In the previous lesson, we learned how to set up the environment for Java. We successfully verified the installation of Java in the command prompt (cmd). In this lesson, we will learn how to write and run our first Java program on Windows.
Open Notepad and enter the following code:
class Example1 {
public static void main(String []args) {
System.out.println("Studyopedia Java Tutorial");
System.out.println("Our First Java Program");
}
}
Now, save the above Notepad as Example1.java. Here, you can see the extension is . java to run a Java program.
The class above is a main class, since it has the main() function in it. Also, a program having main should have the program name similar to the class name. In this case, it’s Example1.
Now, we will open the Command Prompt as we did in the previous lesson to verify JDK installation. Here, we will add a new project folder in the D: drive, i.e.
D:\Studyopedia
We will save all our Java examples in the above folder. You can add any folder.
But to tell the compiler that you have saved your Java programs here, you need to set the path in Environment Variables, i.e.
Go to Control Panel > System > Advanced System Settings > Advanced tab > Environment Variables.
Under System Variable, click Path, then New, and add the following path and save it,
D:\Studyopedia
After adding the path above, now again go to Command Prompt and reach the above path as shown below,

Now, type the following command to compile the Java program and press Enter:
>javac Example1.java
Now, type the following command to run the Java program, which we compiled successfully, and press Enter:
>java Example1
Now, if the program is correct, without any bugs, then it will be successful, and the following will be printed,

After successfully running the program, the output will be visible as shown above.
Now, go to the same path, and you can see a class file generated for the same program.

In this lesson, we learned how to run our first Java program on Windows.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More
No Comments