Pages

Saturday, December 24, 2011

Your First Java Program

First Java Program - Using an Editor 

So you are ready to start with your first Java program. Here is a step by step procedure to create your first Java program in an editor.
1.    Verify the correct path of Java installation to see whether it is in C:, D: or any other drive. If you didn't play with the installation then by default Java will be installed at "C:\Program Files\Java"
2.   Once you are done with the location of Java, open - Command Prompt by clicking "Start-> Command Prompt" or by typing “CMD” at the “Run” option.
3.    You will see a black window known as the "Command Prompt" or DOS window with something like "C:\Users\Username" written and a blinking cursor. The “Username” will be replaced by the name of the current logged in user. This is where you will write commands to create and compile your Java applications.
4.     At the cursor type the command,"CD\". The command CD (Change Directory) will take you out of the current directory or folder to the "C:\" prompt.
5.    When you see the "C:\" prompt type - "CD Program Files\Java\JDK1.6.0\Bin". This command will take you to the "Bin" (Binary) folder inside the "JDK1.6.0" folder. If your Java installation is any other folder or drive replace the path with the original location.
6.    Don't worry about the case in which you are typing your commands. All the DOS commands can be written in capital or small letters. DOS commands are case insensitive but pay extra care to the spaces. There is a space between "Program" and "Files".
7.  The command prompt should now read something like this -- "C:\Program Files\Java\JDK1.6.0\Bin"
8.    At the command prompt, type "Notepad prog1.java" and hit the enter key. You can change the name of the program from "prog1" to any other valid name but don't forget to add the ".java" extension.
9.     The "Notepad" command will start the "Notepad" editor. You will see a message "Cannot find the prog1.java file. Do you want to create a new file" and three command buttons. Select "Yes" to create a program. Don't close the Command Prompt.
10. Inside the notepad window type the following code
public class prog1
{
          public static void main(String args[])
          {
                   System.out.println(“Hello World”);
          }
}
11. Save the file by either pressing "Ctrl+S" or by selecting "File->Save" and return to the "Command Prompt". On the prompt - "C:\Program Files\Java\JDK1.6.0\Bin", type
          javac prog1.java 
This command will compile the java program. If there are some compilation errors, they will be flashed here.
12. If there are no errors, the command prompt will return. Now type the following command
      java prog1
          this command will run the program and the output
Hello World 
will be shown.
Things to remember
·       If you are a Windows 7 user, don’t install the JDK inside the “Program Files” folder. A user does not have the permission to write (create/edit files) in the “Program Files” folder. Therefore you won’t be able to save your programs inside the “Bin” folder. If you try to save the file in the “Bin” folder, Windows will ask you to save the file in the “Documents” folder instead. To install JDK in any other folder just change the install location to any other drive or folder while installing java using the “Browse” button.
·      It is not compulsory that you invoke “Notepad” from the command prompt only. You can open “Notepad” directly from the “Start Menu” also. Just make sure that the program is saved in the “Bin” folder.
·       If you have installed JDK in any drive other than “C:”, switch to that drive by just typing the drive letter followed by colon (“:”). For example “D:” (without the quotes of course!)
;·     Make sure that the program name and the name of the class is exactly be the same. For example, in the above code the name of the program and the class is “prog1”.
·        The “S” in both “System.out.println” and “String args[]” should be in caps.

0 comments:

Post a Comment