Write your first simple java program.


 

First of all we need is a placewhere we can write your Java code. All Java programs are written using plain text files such as a notepad.Therefore there is no need of a fancy software.Lets come to write your first program.For this you open a notepad and start writing a code.The program code looks like this:

class HelloWorld

{

  public static void main(String[] args)

{  

 System.out.println("Hello World!");

}

}


For your first program we are going to understand step by step process to compile and run a program.


1.The first step is to save the file written on the notepad.

 

 

A dialog box is open as show above. Now save your file as "HelloWorld.java". Now I have created an directory in My Documents called "Java" to store the all the Java programs in one place for my convience. You are free to create a another folder on your computer as you prefered.

Note:-

It is very Sincere while saving a file you must save a file as "HelloWorld.java".

class HelloWorld

{

The above code is an instruction to call the class "HelloWorld".


2. Then we need to open a Terminal Window as show below.

 

 

Most of the program are window application you are use a cursor to open a file but the above program are a example of console application it does not run on its own window its needs a terminal window to run a program. A terminal window is a source to run a programs that is stored on your hard drive.

Now to open a terminal window, press the "Windows key" and the letter “R” at one time.

Now a dialog box is open and type a cmd and press ok.

3. The Java Compiler

 

 

Now what is a java compiler .It is one of the console program is the Java compiler called "javac". Its function is to read the code in the HelloWorld.java file, and then translate it into a language that your computer understand. This process is known as called compiling.In Java every program you write in a notepad they have to be compiled before it can be run.

To run javac from the terminal window,First of all you need to tell your computer where The file is located on your computer.Now you give the directory just like I give here my location is "C:\Program Files\Java\jdk\1.6.0_06\bin". If you have the same directory then use it otherwise search your directory from your computer.

When you found your directory location, type the following command into the terminal window:

set path= *the directory where javac located*

Example:-

set path=C:\Program Files\Java\jdk\1.6.0_06\bin

Now press Enter. The terminal window return you something in a flash, It will be the command prompt. At last the path to the compiler has now been set and ready to use.

 

 4. Change the Directory as you preferred


 

Now,you think where your HelloWorld.java file is saved in your computer. My file has been saved in a directory called "Java1" in My Documents. The location is as "C:\Documents and Settings\Chandan\My Documents\Java1".

Follow the below code to change the directory in the terminal window, type in the command:

cd *directory where HelloWorld.java file is saved*

Example:-

cd C:\Documents and Settings\paul\My Documents\Java1

 

5. Compile Your Program


 

Now you are ready to compile Your program. For that, enter the command:

javac HelloWorld.java

When you hit Enter buttom, the compiler work start and look at the code contained within the HelloWorld.java file, and try to attempt to compile it if yor program does not contain any error. If it contain error, it will display a series of errors on the command prompt which help you fix the code.After fixing the error again try to compile your program in the same manner as discuss above.

Tip: After your HelloWorld program has been successfully compiled, and you will see a new file in the same directory. It will be the “HelloWorld.class”.

 

6. Run the Program

 

And at last the only thing is to remain is to run the program. In your terminal window type the command:

java HelloWorld

When you press Enter, the program will run and you will see a output as "Hello World!" written to the terminal window.

This all about your first java program keep going I will discuss the core java in a brief in my next article . Thanks for reading my article.

 


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments