Friday, October 29, 2010

Static Import

Hi ,

     While learning JUnit 4 , I came across  Static Imports of JAVA quite useful feature. Its syntax looks like

import static org.junit.Assert.*; 

    This allows the importing class to access static public members of Assert package without class name.

fail("Not yet implemented"); 

          The static import declaration is analogous to the normal import declaration. Where the normal import declaration imports classes from packages, allowing them to be used without package qualification, the static import declaration imports static members from classes, allowing them to be used without class qualification.


       The static import can also import individual static member of class .

package com.blogspot.pravingole.demo;

import static java.lang.Math.PI;
import static java.lang.Math.*;

public class TestStaticImport {

 public static void main(String[] args) {
  float r = 5.1f;
  System.out.println("Circle area is : "+ (PI*r*r));
 }
}


 for more details click here

P.S. :  This static import don't  become member of importing class, so subclass can't access them without import.

Friday, October 22, 2010

What is JAR ,WAR and EAR . How to build them using ANT

HI,
In JEE, application modules are packaged as EAR, JAR and WAR based on their functionality.Each type of file (.jar, .war, .ear) is processed uniquely by application servers. I am just collecting the stuff for my reference.  :)

JAR:
       Jar file (file with a .jar extension) is the normal Java Application archive and intended to hold generic libraries of Java classes, resources, auxiliary files, etc.This can be added in classpath for compilation and to run java program. Generally in web applications we keep these files in lib directory of the application.
Ex: ojdbc14.jar – This contains all the classes to connect the oracle database
Servlet-api.jar – contains servlet related classes

Ant Task for JAR

 
for ant task details click here. 

WAR:
           Web modules which contains Servlet class files,JSP Files,supporting files, GIF and HTML files are packaged as JAR file with .war( web archive) extension. War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.

 Ant Task for WAR


   
  
  
  
  
  
 

for ant task details click here.


EAR:
        All above files(.jar and .war) are packaged as JAR file with .ear ( enterprise archive) extension and deployed into Application Server.  Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web application.

Ant Task for EAR



      
    
 
for ant task details click here.

 Thanks.

Thursday, October 14, 2010

My favrite Vim commands

  • search - /
  • ignore case search - /\c
  • search for pattern
 /word1\(.*word2\)\@=
/.*Word1.*word2  
  • replace :   %s/word1/replaceword/gc 
  • to append at end of line  :   %s/$/word/gc
  • to append at begin of line  :  %s/^/word/gc
  • to append newline character at end of line  :  %s/$/\r/gc
  • to remove new line character  :  %s/\r//gc
  • to enable Hex editing :%!xxd
  • to go back in binary mode :%!xxd -r
  • To remove all lines not having getDisplayName

  1. replace othr lines with blank
    :%s#^\(.*getDisplayName.*\)\@!.*$##gc

    2.  remove blank lines 
        :v/\S/d
        3.  to remove all lines having getDisplayName

                  just remove @! FROM SEARCH QUERY

      • :vsp      " Vertically split the window

      • " Changing Case use with
      •  lowercase line guu
      •  uppercase line gUU
      •  lowercase line Vu
      •  uppercase line VU
      •  flip case line g~~
      •  Upper Case Word vEU
      •  Flip Case Word vE~
      •  lowercase entire file ggguG
      • buffer commands
      :bn (next buffer),
      :bp (previous buffer)
      :buffers (list open buffers)
      :b (open buffer n)
      :bd (delete buffer).
      :e will just open into a new buffer
      • Grep Command
        • :vim[grep][!] /{pattern}/[g][j] {file} ... 
        • :lvim /\<\(house\|home\)\>/gj *.txt 
        • :lw list selected buffers in grep

      Apache ANT : Learning path

      Hi folks,

             This is not another blog post about using famous build tool Apache Ant. In this post , I am going to list the resources that i found useful while learning ANT.


      Thanks

      Tuesday, October 12, 2010

      How to setup JAVA_HOME environment variable on Windows 7

      Please follow the instructions to set up JAVA_HOME environment variable in your Windows 7 PC. First find out the installation folder of Java development kit (JDK) in your machine.Let's assume it is installed in the folder "C:/jdk6"

      1. Go to Control Panel 
      2. If Control Panel is viewed by Category then select "System & Security " and here select "System" link,otherwise System link is directly inside Control Panel.
      3. On System screen select "Advanced system setting" link in left panel. This will open "System Properties" dialog box with "Advanced" tab preselcted.
      4. Click the Environment Variables button
      5. Under System Variable, click New
      6. Enter the variable name as JAVA_HOME
      7. Enter the variable value as the install path for the Development Kit.  (Eg. C:/jdk6)
      8. Click OK
      9. Click Apply Changes

      Tuesday, October 5, 2010

      Getting executing directory for Servlet web application

      Hi ,

      Some time we need to know the directory in which our web-app is running.In servlet this can be done using following code.

      String webAppPath = req.getSession().getServletContext().getRealPath("//");
      
      

      This is useful when you need to generate some file for other application.