Running The First JAVA Program

I plan to share my computer science knowledge on this blog whenever I get some free time on my hands. So this is the first post where I will explain how to get started with JAVA.

Note – This post is for someone who is a complete beginner and want to learn JAVA from the ground up.

Check if JDK is Installed

First check if JDK is installed on your computer. Open terminal and type javac.

 javac 

If the terminal gets flooded with javac usage information then JDK is installed. If it’s not installed, read the next section below. Else skip to the third section of this post.

Installing JDK

You can get JDK from this site – JDK Download. You have to click on JDK download and then select the right file based on your operating system.

For example, I have a Macbook Air running Mac OS X 64 bit. So I will select Mac OS X 64 bit file. If you want to check whether your operating system is 32bit or 64 bit, read this post.

After downloading JDK, install it on your computer. And then you can once again check if JDK is installed.

Printing Hello World

1) Open the text editor (like notepad or textedit) and type in (or copy paste) the following code –

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

2) Save this file as hello.java on your desktop
3) Open the terminal, navigate to desktop using cd (change directory command).
4) Type in the following two lines one by one (the first one is to compile, the second one is to run the program).

javac hello.java
java hello

This will run the program and you will see Hello World in the terminal window. That’s all that you need to run a simple java program.

Here are few points to note –

  1. The file name (hello.java) should always be the same as the class name (hello) containing main method.
  2. println means print line and it will print the text and move the cursor to next line. Try using print instead of println and see the difference.

Update –

You can also run JAVA code on this awesome compiler app developed by my friends at Programming Hub.

Running The First JAVA Program

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s