How to Run Java Program?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

To learn how to run Java program, first, ensure Java is installed, and the path is correctly set on your system. After installing the JDK, use the javac command to compile the Java file into platform-independent bytecode. This bytecode can be executed on any machine with a Java Virtual Machine (JVM) using the java command, producing the program's output.

Steps to Run a Java Program

Here, we will see how to run Java program using Notepad and Command Prompt. Let's see the process step-by-step:

Step 1: I have used the code given below to perform all the operations below. Open the Notepad, copy this code in the Notepad, and save that file with the extension .java.

You can see in the below images that I have written the Java code and saved it as test.java. Now, a new file will be created with the name test.java.

Create a Notepad named folder

Open a Notepad file and copy the Java Code

Save the file with name test.java

Step 2: You can see that inside the Art folder, there is a Java file named test.java.

Java file is there in the folder

Step 3: Now press Alt+D, and you will enter the above search bar.

type CMD

Step 4: Type CMD and press enter key.

press enter key

Step 5: You can see that the CMD is open. Now type javac test.java, as seen in the image below.

Type javac test.java command

Step 6: You can see that a new file has been created with the .class extension. The file's name would be the name of the class you have used inside your program. In my case, I have used the Scaler as a class name.

new file has been created

Step 7: Now again, open the cmd and type the command java Scaler as you can see in the below image.

Open the cmd and type the command java Scaler

Step 8: At last, you will see that the content of the file has been displayed.

Content of the file has been displayed

Note:

1: Java programs are compiled using the javac command. It takes .java files as input and generates bytecode.

2: The java command executes Java bytecodes. It processes bytecode as input, executes it, and produces the output.

Conclusion

  • This guide highlights how to run Java program, starting from creating the Java file in Notepad to compiling and executing it.
  • The javac command plays a crucial role in compiling Java programs, converting them into executable bytecode.
  • To run a Java program, the java command is employed to execute the compiled bytecode, demonstrating the output of your program.
  • Following these steps ensures a smooth process on how to run Java program, illustrating the seamless transition from code compilation with javac to execution with java.