Wednesday, 26 June 2013

First Java Program Execution

Before going to run our first Java program we need some settings on our computer to execute our program. Let us see them.


In Windows XP or 7 we need path setting in onrder to execute our Java program. I all ready provided the download link to download Java software.

If you have download JDK1.7 update 2 then in your system you get one folder like this. I am assuming that Java is installed in C drive.

"C:\Program Files\Java\jdk1.7.0_02\bin" This folder contains Java compiler and Java interpretor which are necessary to compile and run our program. Hence these tools are necessary to be in our Windows PATH so that we can use them. To do this we need to include the above directory in our Windows Path so that OS can recognize our commands.

Just copy the above path with out quotes and include it in the path by following the steps below.

Right Click on My Computer Icon present on the desktop and choose properties. Click on Advanced tab and in this tab click on Environment Variables. You will find two types of panes User Varaibles and System Variables. Under System variables search for the variable named PATH. Now click on Edit. If it is not there click on New Button to create it.

If the PATH already exists then the current path is shown in varaiable value. Don't delete the existing value. Just add our JDK path to the existing path. To do this just add ; at the end of current path and paste the above path. Just press OK. Be careful will editing these settings. It will look like this.

Variable Name: Path
Variable value: .......; C:\Program Files\Java\jdk1.7.0_02\bin

How to verify PATH setting is successful or not?
Just open command prompt and type javac and press enter. If it says javac is not recognized as an internal or external command then it means there is something wrong with your path settings.

If the PATH setting is correct you will get its usage syntax and a list of options supported by the command.

This is all regarding the PATH setting.

Since we are going to execute a lot of programs let us make one personal directory where all our Java related programs can be stored. Assume that in my system I am going to store all my programs under D:\Naresh>.

Goto start and open Run or just press the shortcut Window Key + R. Type cmd and you will taken to command prompt. By default it points to current directory. Just shift to D drive by typing D: and create one directory named Naresh (give any name of your choice) by the command md naresh then move to my directory Naresh by change directory command cd Naresh. Now the path appears like above D:\Naresh>.

Now let us start our first Java program. Create a file named First.java where we are going to write our source code. Here is the shortest way to do this!

D:\>Naresh>notepad First.java

The above command will open a Notpad window and it will ask Whether you want to create a file named First.java . Just press yes. If the file already exists it will just open the file with out displaying any dailog box. Now type the following code in this file.


/*  

First Java program by Naresh Bharat.
Visit www.nareshbharat.com for free Java Classes Online.

*/

import java.lang.*;
class First
{

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

}

After typing the code just save the program by pressing Control and S key simultaneously. Otherwise Click on Close button and it will ask whether you want to save the changes. Press yes and you are done!

To compile the Java program we need java compiler. This can be done with the help of javac command. Just type the following command at prompt.

D:\Naresh>javac First.java

If you get the prompt immediately after giving the above command like below

D:\Naresh>

This means you successfully compiled your Java program. If there is any error then error will be displayed. If it contains any error again open Notepad window edit your program and come back to the command prompt and once again try to compile the program. Repeat this procedure until your program is error free.

If every thinng is OK, you will get First.class file in your current directory. This is the byte code. Just for confirmation type dir command and verify the files list. You will find that java compiler has created one .class file related to your program

Now to run our Java program we need Java Interpretor. For this use the following command.

D:\Naresh>java First
Hello World!


Just type the class name i.e. First. Don't give .java or .class extension here. The command java just takes class name with out extension.

If you get the above result that means you are successful in executing our first program. Every Java program execution is like this only.


Actually I just exectued the program with out explaining any thing about what really the purpose of every line written. Let us discuss in more detail.

/* */ This is know as comment section. Every thing written inside /* and */ is just for our understanding. Java compiler just ignores all the content present inside comments. You can assume it just like you write the date in your notebook so that it can be referred later easily. You write date in your notebook just for your sake. The content in your notebook is not related to date at all. Like the same way comments are just for our understanding. Do remember that comments can be defined any where in a Java program.

If the comment contains a single line Java provided one more style as a shortcut. Just type // and write your comment. Any thing written after // will be ignored by compiler. But your comment should not conatin more than one line.

Java even provided one more comment style known as documentation comment. It starts with /** and ends with */. I will discuss this later when the need occurs.


The import statement is used to import the packages in our program. Here I am include java.lang package into my program. By default even if you don't write this, the packace is imported automatically. Just for knowledge sake I mentioned it.

Every language will have set of rules to be followed while programming. Java states that every thing should be written inside a class. And the calss syntax is very simple!

class
{

}

Note that here class is a keyword used to tell the compiler that class declaration is to be defined. The content in the class is to be included with in { }.

Every standalone Java program execution starts from main method. Unfortunately main method syntax in Java is somehow lengthy as compared with the syntax of C.

I will try to explain what is the purpose of every word. Don't worry even though if you are not getting into this concept. As a starter it is difficult to understand perfectly the meaning of every keyword. Gradually when the specific concept comes you will get better idea.

public keyword is used to access the main method by outside world.

static keyword is used to access main method directly with out creating object. This is enough just for now. Later you will come to know what is meant by object creation and all the things related to it.

void is used to indicate that main method is not going to return any value. If any method does not return any value then it return type should be mentioned as void. This will be more clearer to you when methods are covered. If you know C then you know this already.

main is the method name. According to Java naming convention and specifications the main method should be named only to main. That's why it is defined like that. () indicates that it is a method.

If you want to pass any values to main method at command prompt then the main method should be capable of reading and strong them (the values or data given by user) so that it can be processed. For this purpose String args[] is mentioned. String is the class name and args is the variable of type String and [] indicates that it is going to store an array of String values. Again please don't worry if you are not getting this. This will be more clearer to you when we discuss array declaration syntax.

The main method code should be mentioned within the braces only. Thats why {} are used.

In order to display some thing on screen we can use println method. But printing is not that simple as compared to C. It is called via System class. out is an object. The System class exists in java.lang package. It is imported by default if you don't import it. Please remember the syntax for now, because you are new. All the details will become more clearer to you when we cover more concepts related to them.

In order to understand this Hello World! program we need to know lot of concepts. I had just given some brief idea to make aware of the program. Of course I know that this is not enough to you to understand perfectly.

Those who already know Java may find this session not much useful. Sorry but I had started this tutorial keeping a beginner in mind who just knows that Java is just a programming language and nothing else!

Hope the session is useful to you. Don't worry nothing is going to happen to you if you don't understand it. If possible read this post once again. Also follow the tutorial order accordingly. Don't go for next tutorial before covering the previous tutorial.

Let me know your feedback via comments!  

No comments:

Post a Comment