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! 


Thursday, June 5, 2014

How to Setup JDK (Java Development Kit) on Windows XP \ Vista \ 7 \ 8 \ 8.1



1) Download JDK (Java Development Kit)
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

2) Click "Accept License Agreement" then choose a preferred JDK based on your Operating System. If you are using Windows, check whether your system type is 32-bit or 64-bit.


     * If you didn't know how to check system type before, follow my instructions. Otherwise, ignore this stuff.

       
       Easiest method (but not the best) :

        2.1) Open command prompt. (Win + R > Type "dxdiag" then Enter)



        2.2) Once it is done, it will show you its System Information including system type. In this case, my system type is 64-bit.

3) You can now download which version of Windows that is fit for your operating system. In my case, I downloaded Windows x64 installer since the system type is 64-bit.


NOTE : 32-bit system can only run 32-bit programs ; 64-bit system can run both (32-bit and 64-bit) programs.


4) Install the downloaded file. It will only take a little of your precious time.

5) Find the directory folder of your installed JDK program.
          
     5.1) Open your Local Drive (default is C:) > "Java" > "jdk1.x.x" folder (In my case, my JDK version is 1.8.0_05 ; "jdk1.8.0_05" folder) > "bin" folder.
     5.2) Copy the directory path.

6) For Windows Vista / 7 / 8 Users :



          6.1) Open your Control Panel > System and Security > System > Advanced System Settings (Upper Left) > Environment Variables.



          6.2) Under Environment Variables, find and edit "Path".



          6.3) Paste the directory path (where the JDK folder is located) at the end, after semicolon (If you didn't find a semicolon, just create a new one) > Hit OK and close.


7) Check the JDK if it was installed successfully.


   
     7.1) Open command prompt. (Win + R > Type "cmd")
   
     7.2) Under command prompt, type "java -version" or "javac -version".



     7.3) This command tells you what is the current version you are using. If this shows up on your command prompt, CONGRATULATIONS! Otherwise, go back to Step 5.




.