Java is a widely-used programming language that has been around for decades. With its versatility, it is used to develop applications for different platforms such as desktop, web, and mobile. If you’re new to Java programming and you want to run a Java program on your Windows 11 computer, this article is for you. In this guide, we will walk you through the steps on how to run a Java program on Windows 11, starting with the installation of the Java Development Kit (JDK). If you haven’t installed Java yet, you can check out our previous article on “How to Install Java on Windows 11” for detailed instructions. Once you have installed the JDK, you can proceed with this guide to learn how to set up the Java environment variables, compile your Java program, and run it using the Java Virtual Machine (JVM). By the end of this article, you will have the knowledge to run your Java programs with confidence on Windows 11.
Let’s start to write and run your first program in Java.
We must follow the steps given below to run a Java program :
Step 1: Open a text editor
To write a Java program, you can use any text editor such as Notepad, Sublime Text, or Visual Studio Code. Open your preferred text editor and create a new file. Here, we use notepad, so, open the notepad.
Step 2: Write the Java program
In the text editor, type the following code:
public class MyProgram { public static void main(String[] args) { System.out.println("Welcome to Study Trigger!"); } }
This program simply prints “Welcome to Study Trigger!” to the console. You can replace the message with anything you want.
Here is an explanation of the code:
public class MyProgram
: This line declares a class named “MyProgram”. In Java, all code must be contained within a class. The “public” keyword makes the class accessible to other classes.public static void main(String[] args)
: This line declares a method named “main”. The “public” keyword makes the method accessible to other classes. The “static” keyword means that the method can be called without creating an instance of the class. The “void” keyword means that the method does not return a value. The “String[] args” parameter is an array of strings that can be passed to the method.System.out.println("Welcome to Study Trigger!");
: This line prints the message “Welcome to Study Trigger!” to the console. “System.out” is an object that represents the console output. “println” is a method of the System.out object that prints a string to the console and adds a newline character at the end.
Step 3: Save the file
Save the file with the name “MyProgram.java”. The file name must match the name of the class that contains the main method.
Step 4: Compile the program
Open the command prompt and navigate to the directory where the file is saved. Type the following command to compile the program: “javac MyProgram.java“.
This command will create a file called “MyProgram.class” in the same directory.
Step 5: Run the program
Type the following command to run the program: “java MyProgram“. This will execute the program and output “Welcome to Study Trigger!” to the console.
Congratulations, you have successfully written and run your first Java program! You can modify the program and add more functionality as you learn more about Java programming.
In conclusion, running a Java program on Windows 11 is a straightforward process. By following the steps outlined in this guide, you can easily set up your Java environment, write and compile your Java program, and run it using the Java Virtual Machine. Remember to first install the Java Development Kit (JDK) and set up the environment variables before compiling your Java program. In our next article, we will cover the execution of a Java program by the compiler in more detail. With the knowledge gained from this guide and our upcoming article, you will have a solid foundation in Java programming and be able to write and run more complex programs in Windows 11.