Saturday, May 22, 2010

A Quick Guide for installing Tomcat on Windows


  • After unzipping the file, navigate to the "bin" directory and start Tomcat by running the startup.bat file.

  • You must have the JDK installed before you can start Tomcat. The JDK contains the Java runtime engine.


  • Another point to consider is that JSP pages must be compiled the very first time someone accesses the JSP page. This compilation process converts the JSP page into a Servlet. This requires access to the Java compiler. Tomcat will not be able to call the Java compiler if the directory path to the Java compiler includes spaces. To avoid the problem, install the JDK in a directory with a name that contains no spaces, such as JDK1.3.

  • After starting Tomcat, open your browser and type the following URL: http://localhost:8080. Tomcat uses port 8080 by default, so make sure that port is not being used by another server, such as Internet Information Server (IIS). If there is a port conflict, you can change Tomcat's default port by modifying the file server.xml in the conf directory. Search for the number 8080 in the file and change it to an unused port number. When Tomcat starts successfully, you will see an introduction page for Apache Tomcat.
REFERENCES:

Setting the CLASSPATH of JAVA in Windows Vista

To setup the CLASSPATH of JAVA in Windows Vista
Right Click on My Computer > Properties >Advanced System setting > Environment Variables > System Variables

Add a new System Variable

Variable Name : JAVA_HOME
Variable value : C:\Program Files\Java\jdk1.6.0_16

The value will be wherever you have your JDK installation.

There is a simple test to see if the installation you have done is correct or not.
Go to the command line prompt and type
C:\> java -version
the output will be
java version"1.6.0_17"
Java SE Runtime Environment
Java HotSpot Client VM

The version will be which ever version you have installed. This simple test shows that you have installed the JDK or JRE correctly and have set the class path also correctly.

Java Command Line Arguments in Eclipse

To execute a class having the main method with arguments follow the steps below.

  1. Click from menu bar.
  2. “ Run ” - > “ Run ” and now select “ Arguments tab ”. Text area against “ Program Arguments ” is available to enter arguments. You can add any number of arguments but each parameter should be separated by a space. Space is the delimiter here.


Many people think that comma (,) or semi colon (;) is delimiter but it just a misconception. Delimiter is “ space ”.

Reference:

http://www.eclipse-blog.org/java-se/java-command-line-arguments-in-eclipse-ide.html

Javadoc Basic Tutorial

1. Use following comment style to generate a Javadoc.

/**  
 * Returns .............  
 * 
 * @param  url  an absolute URL   
 * @param  name the location of the  
 * @return      the image   
 * @see         Image  
 */
The first line contains the begin-comment delimiter (/**).
The last line contains the end-comment delimiter (*/) Note that unlike the begin-comment delimiter, the end-comment contains only a single asterisk.

Order of Tags
Include tags in the following order:

* @author      (classes and interfaces only, required) 
* @version     (classes and interfaces only, required. See footnote 1) 
* @param       (methods and constructors only) 
* @return      (methods only)
* @exception   (@throws is a synonym added in Javadoc 1.2) 
* @see          
* @since       
* @serial      (or @serialField or @serialData)
2.Click on projects link and choose ” Generate Javadoc ” option.

3.Now a window will be opened where you can select Java Projects or their underlying resources for which JavaDoc needs to be generated. Several other options are also there where user can select any of them as per the need.Here user can select whether to generate JavaDoc for public/private API’s etc.

To Generate the Javadoc you need to have JDK installed. The JRE won't work since it's a compact version so they do not provide the Javadoc functionality.
The Javadoc command will be the Javadoc.exe which is located in the bin folder of the JDK installation.

4.Now IDE asks for other features as well for the generation of Javadoc.

5.You can also save settings as ant script so that you can use the script to generate javadoc in future.

6.Click “Finish” Javadoc will be generated. If you select option of opening index file in browser then after generation of Javadoc you will find ” index.htm ” of Javadoc in your default Web Browser. On console you can see progress of JavaDoc Generation.

Wednesday, May 19, 2010

Changing the JRE being used in Eclipse

To change the JRE you are currently working on
  • Window > Preferences > Java >Installed JREs
  • Add the new installed JRE. Check the one you want to use.
  • Now go to Window > Preferences > Java > Compiler
  • change the "Compiler compliance level" to the one you are using
To fix the JRE not compatible with workspace .class file compatibility: 1.6 error
  • Window > Preferences > Java > Compiler and change the "Compiler compliance level" to 5.0

Adding External Library (.jar) in Eclipse

Adding external library (.jar) to the java classpath
  • From the menu select File -> Import -> File system. Select your jar and select the folder lib as target.
  • You can either create complete folder structure or selected folder structure only.Select the selected folder structure only option.
  • Select your project, right mouse click and select properties. Under libraries select "Add JARs".
  • If you find some errors in the import then find the jar in eclipse/plugin folder for the missing jar files.

Difference Between JRE and JDK

JRE and JDK
JRE
(Java Runtime environment)
JDK
(Java Development Toolkit)
It is an implementation of the Java Virtual Machine* which actually executes Java programs.It is a bundle of software that you can use to develop Java based applications.
Java Run Time Environment is a plug-in needed for running java programs.Java Development Kit is needed for developing java applications.
JRE is smaller than JDK so it needs less Disk space.JDK needs more Disk space as it contains JRE along with various development tools.
JRE can be downloaded/supported freely from
java.com
JDK can be downloaded/supported freely from
java.sun.com
It includes JVM , Core libraries and other additional components to run applications and applets written in Java.It includes JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java applets and applications.
Reference :

Opening a Existing Project in Eclispe

Opening a Existing Project in Eclipse
  • New> Java Project
  • Project name should be the same as the folder name of the existing project
  • Content == Create project from the existing source
  • Projects located in the workspace folder must be direct sub folders of the workspace folder
  • Select the working set as the folder name as the name of the project folder.

Useful Shorts for Eclipse

Shortcuts for Eclipse
To comment Multiple lines :
  • Select all the lines to be commented
  • Press Ctrl + /
  • To uncomment do the same thing
To see the Declaration or corresponding code for a function :
  • Select the function you want to see the code for.
  • Press F3 or right click and do Open Deceleration
To Automatically indent the code use the following keys
  • Select the Code to be indented properly
  • Press Ctrl + i
To Search a function and declaration within a code
  • Press Ctrl + o


Tuesday, May 11, 2010

JUnit to test Java Programs

JUnit is a framework for developing unit tests for Java classes. JUnit provides a base class called TestCase that can be extended to create a series of tests for the class you are creating, an assertion library used for evaluating the results of individual tests, and several applications that run the tests you create.

You need to create a new separate class XText if our original programs class name is X.

There are three things you must do as you set up this class in order to run your tests:

  1. Import junit.framework.*.
  2. Extend TestCase.
  3. Provide a constructor with a single String parameter. This constructor should call the TestCase constructor with the parameter.

Here is a minimal class that demonstrates the structure of classes derived from TestCase:

import junit.framework.*;  
public class XTest extends TestCase {  
 public XTest(String name)  { 
    super(name); 
  }    
 public void Y()  {   .........  } 
} 

Monday, May 10, 2010

Eclipse Basic Help

To add a package to the project in eclipse

Create a new Java project
  • File>New>Java Project
After the Java project is created
  • Right click on the new created folder by the Java
  • New>Package
  • Now add the new Java Package name.
  • You can use dots (.) to specify multiple folders.
  • After the package is added you will see a Tree Hierarchy of the folders which you can see when you go to the folder or when you add the class file inside it.
To add the class file
  • Right click on the last folder and click NEW> Class
  • This will create the new class for the project.
Running you Java program outside Eclipse(create a jar File)
  • To run your Java program outside of Eclipse you need to export it as a jar file. Select your project, right click on it and select "Export".
  • Select JAR file, select next. Select your project and maintain the export destination and a name for the jar file. I named it "myprogram.jar".
Adding external library (.jar) to the java classpath
  • From the menu select File > Import > File system. Select your jar and select the folder lib as target.
  • Select your project, right mouse click and select properties. Under libraries select "Add JARs".
  • If you find some errors in the import then find the jar in eclipse/plugin folder for the missing jar files.
To change the JRE you are currently working on
  • Window > Preferences > Java >Installed JREs
  • Add the new installed JRE. Check the one you want to use.
  • Now go to
  • Window > Preferences > Java > Compiler
  • change the "Compiler compliance level" to the one you are using
To fix the JRE not compatible with workspace .class file compatibility: 1.6 error
  • Window > Preferences > Java > Compiler and change the "Compiler compliance level" to 5.0
REFERENCE: