Getting Started With Java

Java is the language of choice of many. It is very important for every PC programmer to get hands-on Java. But many students who learn Java on their own and try to compile a simple java program face some common problems. In this article, i am providing some common scenarios and the solutions for those problems.

Writing A Small Java Program

Writing a small java program takes a little more effort than writing your first C program. It is because of Java structure. Everything in Java is a class. So even to write a small Hello World program, one has to create a class and main function within it. You may have no idea about a class, but you still have to do it.

// a simple java program with the name Hello.java

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

Follow the steps to create and save the above program.
  1. Start any editor. It may be Notepad or Edit or any other editor like TextPad or Editplus etc.
  2. Type the above program as-it-is. Pay extra attention to case. Java is case sensitive .
  3. Save the file under the name - Hello.java . Remember the file name must be Hello (H is capital) and the extension must be .java.
    If you are using Notepad, ensure you give file name in double quotes ( "Hello.java" ) as Notepad saves the file with the extension .txt otherwise.

Directory Structure

The following directory structure may be followed while you are saving the file.

c:\
  |
  |
  ------ jdk5.0
   			|
   			|
    		-------- bin
 			|  			|
   			|			|
   			|			------   javac.exe
   			|			|
   			|			|
   			|			-------   java.exe
   			|
 			-------- programs
   						|
   						|
   						|
   						|
   						--------- Hello.java

In the above directory structure, i have installed JDK ( J2SE 5.0) in JDK5.0 directory under C drive.

Here I created folder programs under JDK5.0 directory. Directory bin is a standard directory and it contains two important files - javac.exe and java.exe

File Hello.java is saved under programs directory.

Compiling Java Program

Having saved the file with the name Hello.java, now we have to compile the program. Take the following steps to compile the program.
  1. From windows, select All Programs -> Accessories ->Command prompt to go to command prompt (also called as console).
  2. Go to jdk5.0 directory and then to programs directory. In case if your system have different names from the names i am using, use those names.
  3. Type the following command shown in bold and computer response is shown in italic.
    c:\jdk5.0\programs> javac  Hello.java 
    'javac' is not recognized as an internal or external command,
    operable program or batch file.
    
    
    The reason for error is; operating system did not find javac in the current directory. It is obvious (according to above directory structure) that javac.exe is not found in the current directory as we are in programs directory.

    The remedy is to inform operating system to invoke javac.exe that is in bin directory even though we are in programs directory.

    PATH command of Windows can be used to inform Windows where to find Javac.exe and Java.exe.
    At the command prompt, enter the following command to set the path where OS should search for JAVAC and JAVA

    
    C:\jdk5.0\programs>path c:\jdk5.0\bin;c:\windows\system32
    
    
    The above command informs OS that it should go to c:\jdk5.0\bin to locate JAVAC.EXE and other .EXE files if the file is not found in the current directory. The second directory is important only if you are using EDIT editor, otherwise you can ignore it.

    Now, we are ready to compile java programs as follows:

    c:\jdk5.0\programs>javac Hello.java

    You should see command prompt again without any messages. Then you can see a file - Hello.class in the current directory. Hello.class is the bytecode of Java program.

    If you are getting error as follows, it means JAVAC could not locate Hello.java. It is mainly because of saving the file without the extension .java. It is a common problem with Notepad users as Notepad adds extension .TXT if you do not enclose filename in double quotes at the time of saving the file.

    So, check whether you have file Hello.java in programs directory and also check whether the extension is .java alone by using DIR command of OS.

    
    c:\jdk5.0\programs>javac Hello.java 
    error: cannot read: Hello.java
    1 error
    
    

Running Java Program

Once Java program is compiled successfully, you have to run it using JAVA as follows. Make sure you are in programs directory and then give the following command.

c:\jdk5.0\programs>java Hello 
Hello World! I am learning Java

The only possible error at this stage is missing .class file. If the .class file is not found in the current directory (may be you didn't compile in the first place) or JAVA is not able to locate then it displays the following message.
c:\jdk5.0\programs>java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
The remedy for the above problem is to compile in case you didn't compile. However, in some cases the problem may be because of CLASSPATH. Well it is too early to explain the details of CLASSPATH. But it will be sufficient to know CLASSPATH is used by Java to locate .class files.

If you are getting above error in spite of having Hello.class in the current directory, then set the classpath and then run the java program.


c:\jdk5.0\programs>set classpath=.;

c:\jdk5.0\programs>java Hello  
Hello World! I am learning Java

Congratulations!, If you have successfully compiled and run your first Java program.

A thousand miles journey starts with one step forward!!!

Good Luck,

P.Srikanth.