How to run a Java program from the command line?

How to run a Java program from the command line

To run a Java program, Java must be installed (JDK) and JDK install directory should be
set to “Environment Variable”.
How to Set JDK Path : Set JDK Path

 

Now type your java code on “notepad” and save it with dot(.) java extension:

Code:

ExampleClass.java

class ExampleClass{
public static void main(String args[]){
ย  System.out.println("Hello World");
ย ย  }
}

 


Let’s run the program on “command line”:

๏ƒ˜ First see whether Java is installed and the version installed
Command: java -version
๏ƒ˜ Now we will go to the directory of our computer’s folder that has saved our Java program.
Command : “Cd C: \ Users \ Sourav \ Desktop \ java_code”
๏ƒ˜ Now compile the java program:
Command: “javac exampleclass.java”
๏ƒ˜ Lastly the Java program was run with the command “java ExampleClass” and found our results were “Hello World”
Command:

java ExampleClass