// 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.
c:\ | | ------ jdk5.0 | | -------- bin | | | | | ------ javac.exe | | | | | ------- java.exe | -------- programs | | | | --------- Hello.javaIn 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.
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\system32The 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
c:\jdk5.0\programs>java Hello Hello World! I am learning JavaThe 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: HelloThe 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 JavaCongratulations!, If you have successfully compiled and run your first Java program.
A thousand miles journey starts with one step forward!!!
Good Luck,