Java TUTORIAL for beginners

Once you have installed JDK ( Java Developer Kit), it is time to run your first Java program. Before you write your program you need to be aware of the directory, where the JDK has been installed. A structure of the directory is by default something like C:\Program Files\Java\jdk1.7.0_17\bin but it can, in general be C:\Program Files\Java\jdk1.x.x_xx\bin depending upon the version of the JDK you have installed.

To get started quickly, open your favorite text editor ( We recommend Notepad++ , a free editor with lots of features).

Copy the following code in the notepad and save it as in THE SAME DIRECTORY where the JDK has been installed. We will come to the path thing shortly, but let us get started real quick. If your JDK is installed in C:\Program Files\Java\jdk1.7.0\bin then save this file in the directory C:\Program Files\Java\jdk1.7.0\bin

  1. class HelloWorld {
  3. public static void main (String args[]) {
  5. System.out.println("Hello World!");
  6. System.out.println("Learning Java is easy and fun with tutorial!");
  7. }
  9. }
If you are familar with DOS, the follwing steps should be easy. Click the Windows in the Bottom left and type command and press enter. It will open a DOS windows with the current directory something like C:\Users\James. You now need to change your director to the one in which you have installed Java.

The following command takes you to the directory where you have installed java

cd C:\Program Files\Java\jdk1.7.0_17\bin

If you give dir program, you should see javax.exe, java.exe and files in addition to other files.

At this point we are ready to compile the program. To compile the program, give the following command

C:\Program Files\Java\jdk1.7.0_17\bin>javac

If you have not made any typing mistak or any other mistake, it should just return the prompt without any message. Something like.

We have not yet run the program. So you do not see any kind of Hello World message yet. To run it you need to give the java command as

C:\Program Files\Java\jdk1.7.0_17\bin>java HelloWorld

It produce the output something like

If you are wondering why we gave the "java HelloWorld" command in place of "java hello", then you need to look at the code. When java runs, it calls the class. And the first class we have inside the program is the HelloWorld. We will learn more about classes and other thing later.

If you have missed any step here is the youtube that covers the sequence

It is not a good idea to write a program source code in the bin directory typically reserved for binaries. You should actually keep your java source code in a separate directory. This is done with path command in Windows 7. In the next chapter we will look at how to set your path.