Home
| | Sitemap
||Page number :2
Starting with Java
Before you start writing programs in Java, make sure that you have jdk (Java Development Kit) installed on your P.C. JDK is collection of tools that allow us to compile, run and debug programs. The latest version is jdk1.5 which you can download from Sun Microsystems’s website provided you have a broadband internet connection. Else look for computer magazines for this software. Java development kit is available free of cost so you can get it easily. All the tools which help us to develop are in the bin directory of the installed folder.
The main tools are---
- java ---- Java interpreter, it runs application.
- javac --- Java compiler, converts source code to byte code.
- jdb ----Java debugger helps us to fix errors in our program.
- javah --- Produces header files
- javap --- Java disassebler.
- Javadoc - It creates HTML documents from our java code.
Some other tools ---
- appletviewer — Helps us to view and run applets. (Applets are small programs that are embedded in web pages)
- Jar --- A tool to make zipped files that are also known as jar files (Java Archive Files) and can be distributed by the developer after finishing his software. Jar files are platform independent and can be executed in any OS. They contain the bytecode which is executed by JVM.
Your First Java Program
There are two ways by which you can write, compile, run and debug your applications.
- For writing the code you need a text editor like notepad. You can directly compile and execute programs from the DOS prompt.
- Use software like BlueJ or JCreator. We will discuss these in detail after we are familiar with the first method.
I assume that you are using Windows OS as it is the most widely used operating system. If you have a machine running a flavour of LINUX than the steps will differ.
Writing a your first program in Notepad --- Go to start and click on Run. In the dialog box specify notepad. It will open notepad.
You can write your text in it or use any other suitable editor. I personally use ‘notepad 2’ as it is really light on computer’s resources and has many more features to offer than the original notepad bundled with windows. If you have Linux you can use “Kate”, a nice text editor. Lets make a simple program that prints the statement — “This is a Java Program”
The code is to be written exactly in the same way. As Java is case sensitive, a small mistake can lead to failure in compilation.
/*This the first java program hence named first.java*/ class first { public static void main (String args[]) { System.out.println (“This is a java program”); //Printing a statement } }
Save this program in the bin folder of the jdk directory. Remember to save it with a name that is similar to the class name i.e. first.java. Specify “All Files” in the save as type choice. This screenshot is self explanatory.
Now you need to compile the program. Reach the Dos prompt by clicking start and then run. Type “cmd”. The DOS (Disk Operating System) will open. Now follow the steps as you see in the picture. However, the directory specification may differ but the general steps will always be the same.
You can easily figure out the use of javac and java commands.
Popular Error Encountered: Some times you may encounter an error during the execution of program.
Exception in thread “main” java.lang.NoClassDefFoundError: filename
Solution
Go to control panelÆ SystemÆ Click on Advanced Tab then on “Environments Variables” Double click on classpath and specify this path — .;c:\Progra~1\Java\jdk1.5.0\jre\lib\rt.jar;c:\progra~1\Java\jdk1 .5.0\lib\dt.jar
page 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29