Friday, June 6, 2014

How to Run Java Programs Using a Command Prompt (Windows Only)

In this tutorial, I will teach you how to run simple java program(s) ("Hello World!") using a command prompt. Just bear in mind that you will need a JDK (Java Development Kit) in order to run java programs on command prompt. If you don't have a JDK installed on your computer, feel free to visit my previous tutorial on How to Setup a JDK.


1) Create a temporary folder and name it whatever you want.
Created a temporary folder named "Java Programs" on "Documents"

2) Open Windows Notepad or any other text editors. 
3) Copy and paste simple code that is written below.
          
          public class MyFirstProgram {
               public static void main(String args[]) {
                    System.out.print("Hello World!");
               }
          }

4) Save the file as "MyFirstProgram.java" to temporary folder.
Saved a file (MyFirstProgram.java) to "Java Programs" folder

5) Open command prompt. (Win + R > Type "cmd")
6) Locate where the file is. (In my case, the file is under "Documents" > "Java Programs" folder)
7) Once located, type "javac MyFirstProgram.java" to compile the code. This command will create a class file. If no error occurs, it will leave a new line.
NOTE : Locate first where the file is. (Type "cd documents\ java programs")

Class file ; Compiled Java bytecode

8) After that, type "java MyFirstProgram" to run the code. If no error occurs, it will print out the output which is "Hello World!". Otherwise, it will not print and give you error messages.
Printed out the result "Hello World!"

Voila! You've created your first Java program! Congratulations! Explore and experiment codes. Have fun! 


No comments:

Post a Comment