Thursday, February 21, 2013

Mastering Java – Install Java n execute first program


Mastering Java – Install Java n execute first program
What is Java?
Java is a programming language from Sun MicroSoft later acquired by Oracle Corporation in 2009-2010, Java is an open source technology that is used for developing application they very easy to use and more fun in browser. It is absolutely free and very easy to lean.
How do we know Java is already installed in our computer?
Go to Start Menu, click on Computer, click on local Disk that is C:, click on Program Files, there should be Java folder. Click on Java, check for jdk. If it is not present then Java is not yet installed in your system.

How to install Java?
Installing Java is nothing but installing library files which supports to develop the application. It is like say we want to prepare a delicious meal. Here think that meal is nothing but an application. To prepare a meal what all the material or ingredients we need.
1.       A kitchen having cooker and vessels
2.       Rice, dal, pepper powder, salt etc.
3.       Plates to serve the meal
Here the rice, dal, pepper powder salt etc. are nothing but library files and entire kitchen room is nothing but a SDK- Software Development Kit. Installing SDK is nothing but getting new kitchen having all the material, equipment and ingredient ready to use in your system.
This kitchen room is constructed /developed by Sun Microsoft in 1995. Remember that if don’t have kitchen room (SDK) you can’t be able to develop the Java application.
Following is the URL from where you can download the sdk.
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Java SE is nothing but Java Standard Edition.
Click on Download button present under JDK

Select Accept License Agreement button

Click on  jdk-7u2-windows-i586.exe for 32-bit operating system and download the .exe
Click on Run

You need to click on Next for few times











Now go to C à Program Files àJava folder and check for jdk folder

What is Java Compiler?
Java Compiler is a set of code which transforms the Java code what you written to computer understandable code. Once you download jdk, the compiler will be installed in your computer.
If you consider our old example, say you have written a Java code, means you have prepared rice and dal, but how a person(computer) can eat it, so we need something to put in proper way like a plate, so that computer can eat it. The plate is nothing but a compiler, a compiler transform code you understand to computer understands.
How do know Java Compiler is installed in your computer?
Open cmd, type JavaC and hit enter, you may get following message. This is because your computer is not able to recognize the Compiler where is installed.

Let’s make computer understand where Compiler is present.
To do so go to C, Program Files, click on Java then Click on jdk. There you find a bin folder. Click on bin.

In bin you find few library files. Right click on any library file and copy the complete path till end of \bin

Now right click on ‘Computer’, click on Properties

Click on ‘Advanced System Setting’

Click on ‘Environment Variables’

On Environment Variables windows click on New



Enter Variable Name : Path
Variable Values: copy paste the path copied previously
Click on OK

Now again open cmd, type javac and hit enter.
This time it should not throw error as previously but should display few useful messages as shown below.

How to write simple Java Program and execute?
To write Java Program we need to know basic stuff like syntax, methods, class etc. These will be taught later but let’s learn how to execute a simple Java Program.

1.       Open Notepad/Notepad++ edit.
2.       Save as .Java

In Java everything begins with class, by class keyword compiler starts reading the code inside it.
Inside the every class there will be set of methods. The methods are nothing but actions or task what we would like to do.
Whenever computer reads class, it looks for methods. From the previous example a class is nothing but a bowl of rice. When a computer start eating a bowl of rice, it looks for the taste, it may be salt, sweet or spicy.
A method always having keyword called main and starts from keyword public, the syntax and other things will be taught later. But as of now let’s ignore them and start writing a program.

3.       Now compile the above code, means convert the above code from you are understanding to computer understanding. When it compiles the Java file, it changes to class file. Your computer cannot read .java but reads only .class
4.       Open cmd, type cd \

This will redirect to local disk.
5.       Change the directory to the file where you have saved the .java file. Say I have saved .java file in D: drive. So type ‘D:’ hit enter.

6.       Type Dir, you can see all the files present in D drive

7.       Type cd ExecuteFiles and Dir, it will list all the files and folders present in ExecuteFiles folder

8.       Now compile the .Java present in this folder
9.       Type javaC and java file name, ie, javac MyFirstJavaProgram.java
10.   When you compile any java, it creates the file name with .class extention

11.   Type java and file name without extension.
Ex: java MyFirstJavaProgram


If you define any other class having similar name in the same file and compile, it creates related .class files and if you want to execute the class you have to execute separately
Here I have created one more function ‘MyFirstJavaProgram1’ in the same file

When you compile it creates MyFirstJavaProgram1.class


We have can save the file in any name if the class is not defined as public. If the class in public and name of the file is not as class then it throws error

 One file should have only one public class else it throws error

But one java file can have one public class and any number of classes

-Rajashekara Gouda Hotur
Mail ID: myqtp2010@gmail.com