Wednesday 30 November 2011



When one of my colleague asked me What is 'Java Generations'?

I don’t know how many Java techies know about Java Generations. We never think about memory management and garbage collection.
This article is very useful who really face these question:
           1)     How heap and non-heap works in Java?
           2)    What are the Generations in Java? 
           3)    How Garbage collection works with Objects?






At initialization, a maximum address space is virtually reserved but not allocated to physical memory unless it is needed.
The complete address space reserved for object memory can be divided into the young and tenured generations.
The young generation consists of eden plus two survivor spaces. Objects are initially allocated in eden. One survivor space is empty at any time, and serves as a destination of the next, copying collection of any live objects in eden and the other survivor space. Objects are copied between survivor spaces in this way until they are old enough to be tenured, or copied to the tenured generation.
Other virtual machines, including the production virtual machine for the J2SE Platform version 1.2 for the Solaris Operating System, used two equally sized spaces for copying rather than one large eden plus two small spaces. This means the options for sizing the young generation are not directly comparable.
A third generation closely related to the tenured generation is the permanent generation. The permanent generation is special because it holds data needed by the virtual machine to describe objects that do not have equivalence at the Java language level. For example objects describing classes and methods are stored in the permanent generation.

Summary:
  • Eden Space (heap): The pool from which memory is initially allocated for most objects.
  • Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.
  • Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.
  • Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.

Source:
http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html

Tuesday 29 November 2011

How to Change Default Author Name for JavaDocs in Eclipse?


In Eclipse we always try to generate the Java docs at the class level or method level. But when we generate the Java docs it picks the user name from the system user. This could result in weird author names in your code files as in an organization usernames are usually as per organizational naming conventions.For example my user name that I use to login is ORG001239 something like  and you will agree that it wouldn't look good as an author name in a Java file and might not make any sense to most other viewers of the code.

/**
 * Test default author in JavaDocs
 * @author ORG001239
 */
public class TestClass {
}

Here is a quick way to change the default author name in your Eclipse projects. Simply edit your eclipse.ini file found in the root directory where you placed Eclipse. I have Eclipse at C:\eclipse, so my path would be C:\eclipse\eclipse.ini. Once editing this file add the following line and save.

-Duser.name=Saket Saraf

After saving restart Eclipse and when you do a JavaDoc comment and use the author attribute by typing @author and pressing enter on the autocomplete you will see something like this:


/**
 * Test default author in JavaDocs
 * @author Saket Saraf
 */

public class TestClass {
}


Source
http://www.javagyan.com

Thursday 17 November 2011

Why PMD?

For Java developers it is good opportunity to check whether they are following standard programming principles? For most Java development projects going on these days the answer would be to use "PMD". Basically PMD is Programming Mistake Detector.  There are a lot many tools that PMD competes with i.e Checkstyle, FindBugs, Hammurapi, Soot, Squale etc.

PMD  identifies potential problems in the code like:
  1. Possible bugs - empty try/catch/finally/switch statements
  2. Dead code - unused local variables, parameters and private methods
  3. Suboptimal code - wasteful String/StringBuffer usage
  4. Overcomplicated expressions - unnecessary if statements, for loops that could be while loops
  5. Duplicate code - copied/pasted code means copied/pasted bugs
PMD is integrated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, CodeGuide, NetBeans/Sun Java Studio Enterprise/Creator, IntelliJ IDEA, TextPad, Maven, Ant, Gel, JCreator, and Emacs.


Configuring PMD for Eclipse

Quickly configure it with your Eclipse installation.
  1. Start Eclipse.
  2. Select the Help>Software Updates>Find and Install... menu item.
  3. Select "Search for new features to install" option and click Next.
  4. Click New Remote Site...
  5. Enter the URL http://pmd.sourceforge.net/eclipse
  6. Select this new site in the Sites to include in search list and click Next.
  7. Select PMD for Eclipse 3 and Apache Xerces in the "Select the features to install" list and click Next.
  8. Accept the terms of the license agreements and click Next.
  9. Verify that the install location is your Eclipse installation directory, otherwise select the correct one, click Finish.
  10. A warning appear telling the feature is not signed. Ignore and click Install to continue.
  11. Accept to restart the workbench to load PMD into the workbench.
  12. Right click on java file and try to check code using PMD.

Source


http://pmd.sourceforge.net/
http://www.javagyan.com/

Wednesday 16 November 2011

Java 7 Latest Release Features


Java 7 released in July and yes most of the people started to use the same. It has very cool features which are really useful for development. 




1. Strings in switch Statements 



  1.    switch (direction) {  
  2.          case "up":  
  3.              y--;  
  4.          break;  
  5.   
  6.          case "down":  
  7.              y++;  
  8.          break

2. Type Inference for Generic Instance Creation 



  1. List<String> strings = new ArrayList<>();  



3. Multiple Exception Handling Syntax




  1. catch (ClassNotFoundException | IllegalAccessException |  
  2.    InstantiationException ex) {  
  3. }

4. The try-with-resources Statement


try (BufferedReader in=new BufferedReader(new FileReader("test.txt")))  {


}


5. Improved File IO API

6. Support for Non-Java Languages: invokedynamic

7. JLayerPane
Now JRockit is Free!

The JRockit JVM is a high-performance JVM developed to ensure reliability, scalability, manageability, and flexibility for Java applications. The JRockit JVM provides improved performance for Java applications.  The JRockit JVM enables your Java applications to run optimally on Windows and Linux operating systems 

JRockit has nice features as compare to Sun JDK like highest performance and nice garbage collector. 


It's available on Oracle Technology Network.

Still there are some limitations but yes its free and below are summarized points for JRockit
  • JRockit is now free (gratis) for development and internal production use on general purpose computers.  
  • Commercial features continue to require a commercial license. This includes most features currently in JRockit Mission Control, JRockit Real Time and JRockit Virtual Edition.
  • No other major changes. Specifically, redistribution of the JDK is permitted, now also applicable to JRockit.

Friday 11 November 2011

WebSphere Aplication Server 4+ Installation On Linux.

WebSphere Aplication Server:
Installation:
  1. Mount the CD (Disk 1 of 2): mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom 
  2. Create install directory:
cd /tmp
mkdir ws
cd ws
cp /mnt/cdrom/wasi401u/wasi401u.zip .
unzip wasi401u.zip
cd linux
./install.sh
  1. The installation program fails to start the admin server.
  2. Start manually: /opt/IBMHTTPServer/bin/adminctl start
    Use the argument
    stop to stop the server or restart to restart after a configuration change. Note:
  • The admin web server operates on port 8008.
  • The admin web server config file: /opt/IBMHTTPServer/conf/admin.conf
  1. Start the  browser and Enter the URL http://localhost/ and select "Configure server". (The password was assigned during installation - remember it!) Next select from the admin menu on the left hand side: "Basic Settings" (select arrow) + "Core Settings". Set basic items like "Server name", etc. The items which can be configured here can also be configured in the httpd.conf file by a knowledgeable admin. (restart after editing.)
  2. The final step for the installer is to launch the WebSphere Application Server "First Step" panel. This may be launched manually any time later on by using the command: /opt/WebSphere/AppServer/bin/firststeps.sh

DB2 Database installation on Linux.


In this blog I am going to install DB2 V7 and Web sphere application server 4+.  On windows platform obviously it is very easy to install because we get the wizards for the same and it automatically creates the instances. But on linux it is bit tricky specially taking care when we create the instances.

DB2 Database:
We will install the below component on Linux environment.
  • DB2 Database Admin Client
  • UDB (Universal DataBase) Enterprise Edition
  • Application Development Client (for writing your own applications)
In addition to the above we will install the sample database.

Installation:
  1. Check configuration using commands:
    • Default language: echo $LANG
      On my machine it is en_US
    • Check if reguired packages are installed: rpm -e pdksh compat-libs ncurses4
      • The package pdksh ensures shell compatability with commercial ATT based Unix sytems.
      • The package compat-libs is for Red Hat 7.x+ systems to have Red Hat 6.x compatability.
      • Package ncurses4 provides terminal drivers and cursor control mechanism.
  2. Mount the CD (Disk 1 of 2): mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom

  3. Install DB2: (as root)
    • cd /mnt/cdrom/db72pf5u/
    • ./db2setup
       
       
+----------------------------- DB2 Setup Utility ------------------------------+
|                                                                              |
|   Select Install to select products and their components to install, or      |
|   select Create to create the DB2 services.                                  |
|                                                                              |
|                                                                              |
|   To select products and their components, select           [ Install... ]   |
|   Install.                                                                   |
|                                                                              |
|                                                                              |
|   To create a DB2 Instance, an Administration Server,        [ Create... ]   |
|   or a Data Links Manager Administrator select Create.                       |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|   [  Close  ]                                                   [  Help  ]   |
+------------------------------------------------------------------------------+
       
       
       

    • First select the "Install..." option to select the products to install. Next select the "Create..." option to create a DB2 Instance.
    • After selecting the "Install..." option you will get the following screen:
       
       
+------------------------------- Install DB2 V7 -------------------------------+
|                                                                              |
|   Select the products you are licensed to install.  Your Proof of            |
|   Entitlement and License Information booklet identify the products for      |
|   which you are licensed.                                                    |
|                                                                              |
|   To see the preselected components or customize the selection, select       |
|   Customize for the product.                                                 |
|   :*: DB2 Administration Client                           [ Customize... ]   |
|   :*: DB2 UDB Enterprise Edition                          [ Customize... ]   |
|   [ ] DB2 Connect Enterprise Edition                      : Customize... :   |
|   :*: DB2 Application Development Client                  [ Customize... ]   |
|                                                                              |
|   To choose a language for the following components, select Customize for    |
|   the product.                                                               |
|       DB2 Product Messages                                [ Customize... ]   |
|       DB2 Product Library                                 [ Customize... ]   |
|                                                                              |
|                                                                              |
|                                                                              |
|                                                                              |
|   [   OK   ]                     [ Cancel ]                     [  Help  ]   |
+------------------------------------------------------------------------------+
       
       
       


Use the space bar to toggle selection between [ ] and add features.
      • DB2 Administration Client
        • :*: Java Support
        • :*: Control Center
      • DB2 UDB Enterprise Edition
        • :*: Java Support
        • :*: Replication
        • :*: Local Warehouse Control Database
        • :*: Distributed Join for DB2 Data Sources
        • :*: Getting Started
      • DB2 Application Development Client
        • :*: Java Support
        • :*: Control Center
        • :*: DB2 Sample Applications
        • :*: Create Links for DB2 Libraries
      • DB2 Product Library (by default no language is selected. Select one!)
        • Optional: DB2 Product Library (HTML):
          :*: en_US [ ] fr_FR [ ] de_DE
Approve it by selecting OK.
Next create a DB2 instance. The default will be db2inst1. A Linux user and home directory of the same name will be generated. You will be prompted for a password. I would suggest select "[Properties...]" to "Create a sample Database for the DB2 Instance".
       
       
+----------------------------- DB2 Setup Utility ------------------------------+
|                                                                              |
|   +-- Status Report -----------------------------------------------------+   |
|   |                                                                      |   |
|   |                                                                      |   |
|   |  The log file can be found in /tmp/db2setup.log.                     |   |
|   |                                                                      |   |
|   |                                                                      |   |
|   |  Installation                                                        |   |
|   |  ------------                                                        |   |
|   |                                                                      |   |
|   |  DB2 Client                                                 SUCCESS  |   |
...
..
       
       
       

The DB2 installation is complete.
The installation created and started IBM DB2 database. The process will be running at this point. The following changes are made:
  • Three users (and home directories) are created:
    • db2as
    • db2fenc1
    • db2inst1
  • Additions to the file: /etc/services
db2cdb2inst1    50000/tcp   # Connection port for DB2 instance db2inst1
db2idb2inst1    50001/tcp   # Interrupt  port for DB2 instance db2inst1
   
Test the database:
  • Log in as user db2inst1
    (i.e.:
    su - db2inst1)
  • Issue the command: db2
  • This will put you into the db2 environment and at the db2 command prompt.
db2 => connect to sample
db2 => select * from employee
db2 => quit
   
Manage the database:
Commands used to manage the database: (login as user db2as)
  • Start DB2: db2start
    By default the installation will start the database and configure the system to start DB2 each time the system boots.
  • List database instances: db2ilist
  • List database release levels: db2level
  • Stop DB2: db2stop
As user db2inst1:
  • List Database Manager Configuration: db2 get dbm cfg
  • List System Database Directory: db2 list db directory
  • Generate sample database: db2sampl
Points:
  • The DB2 database will use TCP port 523.
  • DB2 License manager (execute as root): /usr/IBMdb2/V7.1/adm/db2licm
    Functionality available in Control Center (
    db2cc).