To Run a Java program you need to have installed Java on your system, if you don't know how you can check my post on Setting Up Java.
Step 01:Open Command prompt
Open your command prompt, for those who do not know here is a way to do it.
1. Press the Windows key + R key you will get the run window,type cmd in the window and run it.
2. Or else you open the run window from your start menu and then type cmd in the window and
run it.
3. Another way is search cmd in your search bar and you will get something called command prompt click it to open.
Now that You have opened command prompt it should look something like this.
Step 02:Set the Path
If you haven't used Java before you will have to set the path to the Java directories so the computer knows where to look when running Java programs.
To set the path you will have to type the bold text in the command prompt and hit Enter,(this is assuming that you have installed java with the default setting otherwise you have to enter the address where you have installed Java)
set "PATH=%PATH%;C:\Program Files\Java\jdk1.8.0_20\bin".
Step 03:Compile a Java Program
Now you have to change the directory of the command prompt to where your .java file is, for an example let's assume the .java file is in your D drive in Folder named java ex.
First type D: in the command prompt and hit enter now you will see that instead of C in the corner of the screen you will see D.
Then type cd D:\java ex and hit Enter.
Now the Command prompt window should look like this.
Now type javac fileName.java and hit Enter.
If you get the below result it means that your file has most probably compiled.The result being nothing displayed and the next line of the command prompt being displayed(file name assumed to be HelloWorld)
If you get a text saying,
'javac' is not recognized as an internal or external command,
operable program or batch file.
This means you haven't set the path correctly,go back and check it at the top of the post.
Step 04:Run A Java Program
Once you compile the .java file a .class file is formed in the same directory,
Run the program by typing the following,
java fileName
Then you should get the result below if the program is a HelloWorld program.
That's How you Compile and Run a Java Program.