Run a Java Program using Command Prompt
Running a java program is very easy after JDK(Java Development Kit) installation. Following are the steps −
Step 1 : It is very important to save the written Java program as .java extension(eg : MyProgram.java).
➤ Example : MyProgram.java;
class MyProgram {
public static void main(String args[]) {
System.out.println("Hello Java !");
}
}
Step 2 : Open a command prompt window and go to the directory where you saved the java program, We assume that you have created a file on the desktop.
Step 3 : To move to the desktop, type the command below and press enter.
cd desktop
Step 4 : Type 'javac MyProgram.java' and press enter to compile your code.
javac MyProgram.java
Note : If there are no errors in your code, the command prompt will take you to the next line.
Step 5 : Now finally, type 'java MyProgram' and press enter to run your program.
java MyProgram
Now, you will be able to see the result printed on the window.
Comments