Note: You cannot compile and execute a Java program if you haven't installed a Java Development Kit on your system.
If you have a JDK installed in your system, then the above information will show up. If it doesn't, you can use this resource to install one:
Running a Java program in CMD:
Step 1: You have to enter the folder where your Java program is placed. If I am executing a program using CMD, I always place them in the desktop folder for convenience, hence my specified folder is the desktop folder.
I am now in the desktop folder where my java program is placed.
Step 2: You have to first compile your Java program using this command.
javac <filename.java>
This command will compile your program and create a byte code file in the folder you are currently in.
A .class file has been created in my desktop folder once I compiled the Timer.java program.
Step 3: Now you have a byte code file and all you have to do is interpret and run it. For this, the command is simple.
java <filename>
Here, you just mention the name of the file without the .java extension. This command will interpret your byte code and run your program on cmd.
The entire flow of the CMD with the compilation, interpretation & execution will look like this:
Note: If you already have the byte code file in the folder, you can directly run the program using the java <filename> command.