Thursday, June 27, 2013

Hello World program in JAVA

 

Today on 6Tags i am going to write about starting  journey as Java programmer.Java is one of the most popular programming language in the world.Check out wikipedia for its history.I'm going to write about how to write a simple hello world program in java.Lets start our journey as a java programmer.


requirements:

1. Windows 7/8

2. Latest versions of JDK DOWNLOAD

3.A working human brain


Download and install latest version of JDK, i working with JDK1.7_03.
Now you have to set the path for cmd to know where the java resides.For this got to:
c:/program files/java/["your java version"]/bin 



 copy the address ,

right click on my computer

choose properties

go to Advance system setting

in Advanced tab choose  Environment Variables

Click on path

variable name=path

variable value= [paste the location you copied]






You are on the Half way now,
now choose a editor NOTEPAD,ECLIPSE,NETBEANS ,many options are out there, i'll go with NOTEPAD.
open NOTEPAD
copy the program and save it as helloworld.java. Here it is

class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}
Program is quite simple we created a class helloworld, then a function to print the hello world.
Now for some eay work, make a folder in c drive , my folder is dr and save the helloworld.java there.
Make sure your file is not helloworld.java.txt.

Now open CMD
go the directory where you saved your  java program.for me it cd c:\dr
type JAVAC helloworld.java,this will create a byte code that will be system independent and a helloworld class file will be created in folder.
If it shows no error then type
JAVA HelloWorld  (this is the class name , it is case sensitive,and there is no extension).
And EUREKA you are now a java programmer.


Now one thing very important,I am not a java programmer i just wrote my helloworld yesterday faced a small problem, that can be huge for a newcomer . My source code was like this
class manish
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}
And i saved it as mac.java.
Now when i run the program javac mac.java run successfully but java mac showed a error like this 
COULD NOT FIND OR LOAD MAIN CLASS MAC, i goolged stack overflow and other discussion forum suggested other commands ,nothing happend then suddenly it strike it is talking about class so i changed my command from java mac to java manish, and bang !!..:) Beaware of these silly mistakes.


2 comments:

  1. Nice post.. introduce IDE for java in ur next post...

    ReplyDelete
    Replies
    1. am not a java professional, i am learning btw i will try to make u happy..:)

      Delete