Friday, February 29, 2008

Example programs - Set I

Download the first set of simple example programs from the below link.
All are simple, console based applications. Use javac for compilation and java for execution. If you have any difficulty in setting up the development environment, check the section of this blog that describes about that topic.

Thursday, February 28, 2008

Setting up the development environment

Let me explain the basics of how to setup a good development environment for Java. You need an idea about this and the pattern is in contrast to the Microsoft products, which are really developer friendly.
To start with; let me try to solve an issue that most beginners face. The question is simple - to learn Java, what software package to download? If you search in the Net or go to the home page of Sun, you will be really confused. You can see so many terms - JDK, J2SE, Java SE, J2EE, Java EE, J2ME etc. Let me explain these terms (packages) in simple terms. (I presume that you know something about JRE - the runtime environment and also about SDK - Software Developer Kit - a term which is commonly used).
JDK - JDK is the acronym for Java Development Kit. This is the right package if you want to develop or just compile simple Java programs [ie .java to .class and if necessary, package as .jar]. In simple terms, if you just want to be able to "run" Java applications, you just need the Java Runtime Environment (JRE). If you want to develop things in Java, you need JDK.
J2SE - J2SE is an acronym for Java 2 Standard Edition. In a very broad sense, we can say that it is JDK + JRE. J2SE is considered to be the foundation edition of Java platform and programming environment in which all other editions are based. J2SE provides the essential compiler, tools, runtimes and APIs for writing, deploying and running applets and applications in the Java programming language. This is what most people need for a Servlet, Beans, JSP, XML, JDBC, Web Application or Swing desktop application.
Java SE - Sun decided to rename J2SE to Java SE. Thats it. It is the acronym of Java Platform, Second Edition. The new versions are given this name.
J2EE - J2EE is the edition of the Java 2 platform targeted at developing multi-tier applications. It is the acronym for Java 2 Enterprise Edition. It consists of a set of specifications, APIs and technologies defining enterprise application development. J2EE implementations enjoy all of the features of J2SE platform with additional frameworks and libraries added to support distributed/Web development. In simple terms, we can say that J2EE is an extension to J2SE, that supplies foremost server-side technologies ie. J2EE = J2SE plus RMI tools, EJB, an EJB server, and other distributed computing tools.
There's a high development overhead in using J2EE features; so don't bother unless you need them. It is complex also. For a beginner, the equation is very simple - stay away from J2EE for the time being.
Java EE - It is the new name given to J2EE. It is the acronym of Java Platform, Enterprise Edition. The name changed after J2EE 1.4. Life of the developer has become more easy now, since some of the things such as Web Services are easy to implement now.
J2ME - J2ME is the acronym for Java 2 Platform, Micro Edition. It is used for the development of software for small, resource-constrained devices such as cell phones, PDAs and set-top boxes. New version of J2ME is renamed to Java Platform, Micro Edition or Java ME.
To conclude; as a beginner, what you really want is the Java SE package. Click here to download the latest version. Install the downloaded package and you are set for Java Development.
Writing simple programs
We can write simple applications using any editor - such as Notepad. Many good editors are available. Some of them are listed below.
1. Notepad++ (Light and simple. One of my personal favorites. Free software).
2. EditPad Lite (Powerful multipurpose editor. Free. Pro version available).
3. JCreator LE (Very powerful. Pro version has features such as code completion).
4. NetBeans (Powerful and big. But resource greedy. Lot of functionality).
If you are using Notepad as the editor, you need to give the filename extension as .java and also select All Files in the Save As Type combo box. The above mentioned editors have features like syntax highlighting, which helps in quick development. But for a beginner, its better to start with Notepad itself, since you can learn many things in the hard way.
Compilation & Execution
To compile a simple Java program, the command is javac. To execute it, the command is java. For the execution of an applet program, we need an external HTML file or applet tag as comment in the source code. Simple applet programs are usually executed using the appletviewer command. On execution, if you get an error such not 'command not recognized' or 'ClassDefNotFoundException', continue reading.
Setting PATH and CLASSPATH
PATH is the environment variable where the system searches for the command (executable file name) specified in the command line. On installing Java, the commands such as javac, java etc. goes to the jdk1.X.x\bin directory. For example, on my laptop, they are in the C:\Program Files\Java\jdk1.5.0_12\bin directory (the OS is Windows XP). The working directory, where the source files are saved, maybe different such as D:\WorkArea\Java>. If we execute the javac or java command from that directory, we will get the 'command not recognized' error if the above mentioned path is not present in the environment variable PATH. We can verify this by executing the following command.
ECHO %PATH%
If the proper path is not displayed in the output of the above command, we need to set the path. We can set the path temporarily or permanently. To set the path temporarily, execute the following command in the command line. Keep in mind that location may be different in your system.
SET PATH=%PATH%;"C:\Program Files\Java\jdk1.5.0_12\bin"
When we say PATH=%PATH%, it preserves the path that is already there in the environment variable. To append the new location, we use the semicolon (;) and then the correct path. Note the double quote (") characters around the new path - it is included for safety - because there is a space in the path, in Program Files. Older operating systems, such as Windows 98 will not support this. If we specify the quotes, everything will be treated as a single string.
To set the path permanently, we need to update the PATH system variable. For that, select System Properties by right clicking My Computer and choosing Properties. Then select Environment Variables from the Advanced tab. Select the PATH variable from the System Variables list and click Edit. Move towards the end of the value given in the Variable Value text field, put a semicolon (;) and add the above mentioned path. Click OK to apply the changes. From now on, whenever a new command window is fired, the path should be set appropriately. Instructions given above are for a Windows XP machine and the same is applicable for other NT family systems such as Windows 2000 or 2003. If you are using Windows 98 or Me, you need to update the Autoexec.bat file for setting the path permanently.
CLASSPATH is specific to the Java environment. It is the location that the JRE searches for classes and other resource files. Sometimes, even if everything is correct in your program, you may get an error such as ClassDefNotFoundException while executing the Java program. This could be because of the CLASSPATH issue. Go through this link to find more about this. The method to solve this issue is mentioned under the section 'Using the CLASSPATH environment variable'. It is very similar to setting PATH. If you are confused, there is a simple thing that can be done to solve this issue. Add the current directory to the existing CLASSPATH by executing the following command.
SET CLASSPATH=%CLASSPATH%;.
Note the dot (.) at the end of the command. It represents the current directory. After the execution of this command, the JRE will start looking into the current directory also for the class file. This will solve the issue of ClassDefNotFoundException.
Just to update you, if you are using a *NX family OS such as Linux, the echo command will be echo $Path. Environment variables are represented using the $ character instead of %. Also, you should note that the commands are case sensitive in these systems.