Monday 22 November 2010

Creating and running your very First Java Program - Hello World!

1. Create a folder, say "Java Programs" in C:\ drive


2. Open a simple text editor like notepad and save it as, say HelloWorld.java, in your created folder (Java Programs in my case)  


3. Write the following program in it (and save)



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


4Open the Start menu and click the Run command (or press Windows key + R).  In the Open field type cmd. Click the OK button



5. Get to  C:\  drive by typing cd\ at the command prompt. Next type cd Java Programs to get to the folder you've created. Now type dir at the command prompt and you'll see the HelloWorld.java file on display


6. Next compile the HelloWorld.java file using the javac tool (see JDK tools from the post below). Type javac HelloWorld.java at the command prompt. This compiler turns the HelloWorld.java source code into Java bytecode - HelloWorld.class



7. Now if you type dir again at the command prompt, you'll see a class file called HelloWorld.class already created (a Java bytecode)


8. Next we use the java interpreter, one of the main JDK tools (see the post below), to interpret the HelloWorld.class. Type java HelloWorld at the command prompt 



9. There runs your first Java program

Installing the Java Development Kit (JDK)



Nota Bene: This write-up is about installing the JDK 6 in Windows Vista. JDK 7 is scheduled to be released on 28 July 2011. 


1. Go to http://java.sun.com

2. Click Downloads > Java SE



3. Select the latest version of Java Platform, Standard Edition (say, JDK 6 Update 19) and download the JDK


4. Install the downloaded binary file (it could be something like jdk-6u19-windows-i586.exe) in the Program Files directory. Post installation, the Java folder should show in the C:\Program Files directory


5. Now click the Start button and open the Start menu. In the right pane, right click on Computer > Properties > Advanced system settings 





6. In the Advanced tab, click the Environment Variables... button

7. Create a new user variable by clicking the New... button



8. In the Variable name field write Path


9. In the Variable value field write C:\Program Files\Java\jdk 1.6.0_19\bin


Now if you click Program Files > Java > jdk1.6.0_21 > bin, you'll find the following programming tools:



     appletviewer: This tool lets you debug and run Java applets without a web browser
     java: This tool is the interpreter, the loader for Java applications
     javac: The compiler which converts source code into intermediate Java bytecode
     javadoc: The documentation generator in HTML format
     javah: The C header and stub generator for writing native methods
     javap: This tool is the disassembler which converts bytecode files into a program description


     ...
     ...


10. Open the Start menu and click the Run command (or press Windows key + R)


11. In the Open field type cmd


12. Click the OK button


13. At the command prompt type javac and press Enter



The following will appear in the command window



You're ready to run your first Java program