One thing I have realized after delivering a couple of sessions on Java that not everyone is very clear about installing the JDK and start programming in Java on their computers (specially the first years). Although there are many tutorials and a lot of help on the internet but its all scattered about making the life tough for a beginner. So my aim is to enable all those who are new to the Java programming environment to get started quickly.
Before you write your first program, you'll need:
- The Java SE Development Kit 6 (JDK 6)
- A text editor (I'll be using a simple editor included with the Windows platforms, the Notepad)
The first program, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will:
- Create a source file containing the code, written in the Java programming language, that you and other programmers can understand. You can use any other text editor to create and edit source files.
- Compile the source file into a .class file. The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
- Run the program. The Java application launcher tool (java) uses the Java virtual machine to run your application.
1. Create a Source FileFirst, start your editor. You can launch the Notepad editor from the Start menu by selecting
Programs > Accessories > Notepad.
In a new document, type in the following code:
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
Take care when you type. Type all code, commands, and file names exactly as shown. Both the compiler (javac) and launcher tool (java) are case-sensitive, so you must capitalize consistently.
Save the code in a file with the name HelloWorldApp.java. To do this in Notepad, first choose the File > Save As menu item. Then, in the Save As dialog box, specify the folder (directory) where you'll save your file. In the File name text field, type "HelloWorldApp.java", including the quotation marks. From the Save as type combo box, choose Text Documents (*.txt). In the Encoding combo box, leave the encoding as ANSI. When you're finished, the dialog box should look somewhat like this.
Now click Save, and exit Notepad.
2. Compile the Source File into a .class FileBring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt, or by choosing Run and then entering cmd.
A shell window should look something like this.
The prompt shows your current directory (as shown in the preceding figure).
To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is uiet on the C drive, type the following command at the prompt and press Enter:
cd C:\uiet
Now the prompt should change to C:\uiet>.
If you enter dir at the prompt, you should see your source file, as the following figure shows.
Now you are ready to compile. At the prompt, type the following command and press Enter.
javac HelloWorldApp.java
The compiler will generate a bytecode file, HelloWorldApp.class. At the prompt, type dir to see the new file that was generated, as shown in the following figure.
Now that you have a .class file, you can run your program.
3. Run the ProgramIn the same directory, enter the following command at the prompt:
java HelloWorldApp
The next figure shows what you should now see:
The program prints "Hello World!" to the screen. Congratulations! Your program works!
Hopefully this post will help you to shed all your inhibitions and you will realize how simple it is realy to start programming in java. Best of Luck!