java programming tutorial 1: how to start “hello world” program in java

18
Habibur Rahman (OpenSourceBangladesh.org) TigerHATS Team [email protected] Dhaka, Bangladesh OpenSourceBangladesh Java programming tutorial 1: How to start “Hello World” program in Java In this tutorial, you will get some fundamental idea how to begin your programming in java. However, this tutorial is written for newbie’s. They need to know how to start object oriented programming in Windows Platform. Java is not complex to learn, but there is an enormous deal to it. You can run Java programs unchanged in any machine and operating system combination that supports Java. Java programs are intrinsically object-oriented. Java programs are executed by the Java Interpreter, which analyses the bytecodes and carries out the operations they specify. The Java Development Kit (JDK) supports the compilation and execution of Java applications. Step: 1 Download JDK: Search on Google using this keyword "JDK 6 download or JDK download" and Click the first search item named "Java SE Downloads" or simply type this URL on browser: http://www.oracle.com/technetwork/java/javase/downloads/ index.html © 2011 OpenSourceBangladesh

Upload: habibur-rahman

Post on 02-Apr-2015

653 views

Category:

Documents


4 download

DESCRIPTION

In this tutorial, you will get some fundamental idea how to begin your programming in java. However, this tutorial is written for newbie’s. They need to know how to start object oriented programming in Windows Platform.Java is not complex to learn, but there is an enormous deal to it. You can run Java programs unchanged in any machine and operating system combination that supports Java. Java programs are intrinsically object-oriented. Java programs are executed by the Java Interpreter, which analyses the bytecodes and carries out the operations they specify. The Java Development Kit (JDK) supports the compilation and execution of Java applications.

TRANSCRIPT

Page 1: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

OpenSourceBangladesh

Java programming tutorial 1: How to start “Hello World” program in Java

In this tutorial, you will get some fundamental idea how to begin your programming in java. However, this tutorial is written for newbie’s. They need to know how to start object oriented programming in Windows Platform.

Java is not complex to learn, but there is an enormous deal to it. You can run Java programs unchanged in any machine and operating system combination that supports Java. Java programs are intrinsically object-oriented. Java programs are executed by the Java Interpreter, which analyses the bytecodes and carries out the operations they specify. The Java Development Kit (JDK) supports the compilation and execution of Java applications.

Step: 1

Download JDK:

Search on Google using this keyword "JDK 6 download or JDK download" and Click the first search item named "Java SE Downloads" or simply type this URL on browser:

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Download latest JDK 6 version (Java SE 6 Update 23) for Windows. Click on “Download” button which will redirect another page.

Please Select Platform “Windows” and Language “English or Multi-language or your preferred language” for your download:

Click check box for JDK License Agreement. For example (I agree to the Java SE Development Kit 6u23 License Agreement) Then click on "Continue" which will redirect you in another page.

Now you will see Available Files for Download JDK Click the Latest Java SE Development Kit (JDK) file name to start download JDK. For example (jdk-6u23-windows-i586.exe)

© 2011 OpenSourceBangladesh

Page 2: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Additional Resources (Optional): Java SE 6 (JDK 6) Documentation

Java SE documentation is regularly updated to provide developers with in-depth information about new features in the Java platform. You can download Java SE 6 (JDK 6) Documentation and this documentation available at the JDK download page (Step 1). .Click "Download Zip" button

Please Select Language (English) and check License Agreement button then click on "Continue"

After that Download JDK Documentation

Now you need to extract JDK Documentation. For example in C drive (C:\)

Step: 2

Setup JDK on your machine:

Now you need to double click on executable JDK, depending on your system configuration it might take little time to initiate. Now you will see an installation wizard and follow the instruction to install JDK.

Now click on "Next"

Figure 1: JDK installation

© 2011 OpenSourceBangladesh

Page 3: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

You will see that Default selected "Development Tools"

You can select others feature likes "Demos and Samples", "Source Code", "Java DB" simply by clicking the down arrow button which will show you below option "This feature will installed on local hard drive"

Figure 2 shows the default installation directory (C drive) click "Next" Then click "Finish" button

Figure 2: JDK installation

Step: 3

Setup Environment Variable:

Right Click on “My Computer” on Desktop icon and select and click on "Properties" button

Click on "Advanced" button

Click on “Environment Variables"

© 2011 OpenSourceBangladesh

Page 4: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Figure 3: Environment Variable

Figure 4: Environment Variable

© 2011 OpenSourceBangladesh

Page 5: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Go to JDK installation directory like the below path and copy this path to fix environment variable.

C:\Program Files\Java\jdk1.6.0_23\bin

Now you need to edit System Variables. Select “Path” then Click on “edit” Path then put a semicolon before “paste” the path in “Variable value”

Click on "OK" button

Figure 5: Environment Variable

Step: 4

How to test JDK being installed properly in your machine:

Click on "start” then click on "Run"

Figure 6: Run Command Prompt

Run dialog box will be appeared then write "cmd" then press “OK” after that cmd.exe will be executed.

Command prompt will be help for you to understand that you installed JDK properly or not.

Write "java" in your command prompt which will give you some information about java.

© 2011 OpenSourceBangladesh

Page 6: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Figure 7: Java running on your machine

Step: 5

How to compiling a simple “Hello World” java program in your Command Prompt:

Java source code is stored in files with extension .java as well as Java object code is stored in files with the extension .class.

Simply click on "start” after that click "Run" then a new box will appeared titled “Run” and place your cursor on it then you need to type “Notepad” after Open: Untitled Notepad window will be appeared. Write this Java code in your notepad. public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello world"); } } Please Save As " HelloWorld.java”, you have to be careful when you save the documents. Save as type: "All Files" and click "Save" save it where you want to save. For example D drive (D:\)

The program consists of just one class, HelloWorld, and one method, main(). Execution of an application always starts at the first executable statement in the method main().

© 2011 OpenSourceBangladesh

Page 7: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Go to the Windows command (cmd) file as described in Step 4.

The figure 8 provided detail instructions. Type "D: or d:" in your command prompt. Now you are in D drive, type "javac HelloWorld.java" in your cmd then it will create a class named “HelloWorld.class”

You just need to write Java class name in command prompt. Now you can execute java class just type in command prompt "java -ea HelloWorld " After that you will see “Hello World" in your command prompt.

Figure 8: Command Line Instruction to run Hello Word

Alternative Way:

You can use JCreator which is a powerful Free IDE for Java. After installing JDK, You can install JCreator LE version to compile and run java application.

Download URL: http://www.jcreator.org/download.htm

After download executable JCreator LE or Classic version then double click on it. A setup wizard will be appeared.

© 2011 OpenSourceBangladesh

Page 8: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Click on “Next” button

Figure 9: JCreator Setup

Click on “I accept the agreement” and click on “Next”

Figure 10: JCreator License Agreement

© 2011 OpenSourceBangladesh

Page 9: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Select destination location as by default C drive then click on “Next”

Figure 11: Select destination location

Press “Yes” button then Press “Next” button

Figure 12: Folder Does Not Exist

© 2011 OpenSourceBangladesh

Page 10: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Select Additional Tasks if you want additional icons on your desktop then Press “Next” button

Figure 13: Select Additional Tasks

Press “Install” button.

Figure 14: Ready to Install

© 2011 OpenSourceBangladesh

Page 11: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Press “Finish” button to complete JCreator Setup.

Figure 15: JCreator Setup Complete

JCreator Configuration:Press “Next” button.

Figure 16: User SettingsPress “Next” button

© 2011 OpenSourceBangladesh

Page 12: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Figure 17: File Associations

Press Browse and select JDK Home Directory.

Figure 18: JDK Home Directory

C:\Program Files\Java\jdk1.6.0_23 Press “Next”

© 2011 OpenSourceBangladesh

Page 13: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

Press Browse and select JDK JavaDocs home directory then press “Finish”

Figure 19 : JavaDocs home directory

How to run Hello World program in JCreator:

At the first you need to run JCreator.

Click File > New > File, Select the file type to “Java Class” then press “Next”.

Figure 20: Java Class

© 2011 OpenSourceBangladesh

Page 14: Java programming tutorial 1: How to start “Hello World” program in Java

Habibur Rahman (OpenSourceBangladesh.org)TigerHATS Team

[email protected], Bangladesh

File name will be HelloWorld.java. Select location by browsing (..) button to select location. Click on “Finish” button.

Figure 21: HelloWorld class name

Step 5 already provided HelloWorld.java code or write the below code inside HelloWorld class.

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

Click Build > Build File. While you may not see anything happening during this process, much is going on behind the scenes.

Click Build > Run File. Notice the General Output pane displays the words "Hello World."

Hopefully, it will help you to start java programming as a newbie's. Even though, I have tried to my best to share my very little knowledge about this with you. Thank you very much for your staying power to read my article. If I made any mistakes in my article, please correct me.

If you have any questions, you can ask me freely. I will be happy to answer your question if I can.

© 2011 OpenSourceBangladesh