Monday, October 26, 2009

Bye Bye GeoCities …

Today is the last day for GeoCities in the internet world. From tomorrow the GeoCities Websites are nowhere. 

It really somehow hurts me because, I was started to learn web pages only from geocities. Which had given me inspiration towards web pages development…?


Any Finally thanks GeoCities  for all your help. Hope Yahoo may come up with something new







Where your compiled JSP files stored in WEBLOGIC?

I know where the compiled files are stored in Tomcat (which is under work folder), but when it comes to weblogic I was really searching where the compiled JSP classes are stored.After doing some google, then I found the path.
Work Folder of WEBLOGIC:
\bea\user_projects\domains\<YOUR_DOMAIN_NAME>\servers\<APP_SERVER_NAME>\tmp\_WL_user\<APPLICATION_WAR>

Thursday, October 22, 2009

5 Things you should know about String Constant Pool.

  • When a Class was compiled the String literals are noted in a special way by the compiler.
  • Once the Class get loaded, all the String literals which are inside the class are placed inside the String Constant Pool. So once the Class got initialized all the String literals are ready for use.
  • String Constant Pool is Constant Table which will hold all the String Literal Objects which lives on the heap.
  • Most of people think String Constant Pool is where the String Literal Object are Stored, but that's not true."All the Objects are Stored one and only in HEAP"
  • During Garbage Collections, the values which are String Literals are not Garbage Collected. The reason is GC only collects the objects which are not referred. But String Literals objects (in Heap) are referred by the String Constant Pool (Constant Table).

Wednesday, October 21, 2009

Eclipse Scrapbook Pages: A quick way to test you java code snippets.

Scrapbook Pages was the functionality which most of the eclipse users don't know. This is a simple and quick way to test any small code snippets before implementing it.

For example if you want to test how you can get year, month and date from calendar class. Then we have to write a test class with proper class name and main method in it… But it is an annoying process to just to test this small code. So in Scrap page you can just paste the code in the files as below and you can execute (Ctrl + U).

Sample Code:

int year = 2009;

int month = 0; // January

int date = 1;

java.util.Calendar cal = java.util.Calendar.getInstance();

cal.clear();

cal.set(java.util.Calendar.YEAR, year);

cal.set(java.util.Calendar.MONTH, month);

cal.set(java.util.Calendar.DATE, date);

java.sql.Date sqlDate = new java.sql.Date(cal.getTimeInMillis());

System.out.println(sqlDate);

System.out.println("Bye");

You also select only the lines which you want to test, for example if I want to test only the last line "System.out.println("Bye"); Just select the line and execute …. You only see "Bye" in you console…

This is a Quick way to test your code Snipped and another important thing is you don't have any import statements and class name. So If you want to refer Calendar class you have to refer it with full package structure like this java.util.Calendar. By default it only finds the classes which are in java.util (as normally java does).

Monday, October 19, 2009

ANT “Hello World” Example

I thought sharing very basic ant build.xml file which will compile a single class and create a jar by adding manifest file to it. Let start explaining this step by step.
AntHelloWorld Java Class:

Create a java class with which will print hello world in the console.

package com.passion4java.helloworld.ant;
class AntHelloWorld {
    public static void main(String[] args) {
        System.out.println("This Project was build using Ant");
    }
}



Create a build.xml file which will first clean and compile the java classes and then build the jar file.
Hello world build script:
<?xml version="1.0" encoding="UTF-8"?>

<project name="AntHelloWorld" default="main-target" basedir=".">
<description>This is a simple build file to run this example file </description>
    <property name="source.dir" value="src"/>
    <property name="build.dir" value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="main-class-name" value="com.passion4java.helloworld.ant.AntHelloWorld"/>
<target name="main-target" depends="clean,build" description="--> description">

    <!-- creating a jar file-->

    <jar basedir="${classes.dir}" destfile="${ant.project.name}.jar">

        <!-- adding the main class to the manifest file-->

        <manifest>

            <attribute name="Main-Class" value="${main-class-name}"/>

        </manifest>

    </jar>
    <delete dir="${build.dir}"/>

</target>
<target name="clean"><delete dir="${build.dir}"/>

    <delete dir="${classes.dir}"/>
    <delete file="${ant.project.name}.jar"></delete>

</target>

    <!--target:build java classes    -->

    <target name="build">
    <mkdir dir="${build.dir}"/>

    <mkdir dir="${classes.dir}"/>

        <javac srcdir="${source.dir}" destdir="${classes.dir}"/>
    </target>

</project>


Execution:
Download the zip file which is attached and extract it. You will see the directory structure as below.


Go the folder where the build.xml was there and start execute the file by giving the ant build.xml from the command prompt.All the properties which we defined is relative so it should not have any path related issues



How it Works:

  • We have created a project called AntHelloWorld and we have created 3 targets and 4 properties.
  • The name of the Project can also referred by ${ant.project.name}
  • main-target is the default target which be executed when you are running build.xml file without specifying any targets.
  • Once the main-target get triggered, it will first finish all the depended targets which are defined in depends attribute of the target tags.
  • In our main target they are 2 depended targets clean,build. So both clean and build should be execution first before main-target starts.
  • The order as in depended targets should matters, because as you have the target listed based on that only the ant which will pick which one to execute first. In our case it should be clean target.
  • Clean- target deletes the folder and jar if they are present. If no folder is there this will not throw any exceptions.


  • Build targets just compiles all the java classes in the source directory and it will place all the compiled classes in the destination directory

    <javac srcdir="${source.dir}" destdir="${classes.dir}"/>

  • Then our main-target starts it calls the jar tags which pointing to the classes folder which was created by javac tag.
  • There is also a manifest tag which adds the manifest file to the jar file.
  • Main-Class attribute was added to the manifest file using the below tag <manifest>            <attribute name="Main-Class" value="${main-class-name}"/>        </manifest>
  • Main-Class is nothing but the entry point class for this jar.
    That's it, now we have a jar with a single class file and a manifest file inside in it :)  Download ANT HelloWorld Zip File


    Wednesday, October 14, 2009

    Grouping: A Cool function from Regular Expression

    Most of them are very comfortable in find/replacing text in any Text editor. I will be really helpful if you have the exact phrase which you need to replace. Like In the below text if you want to replace word “passionsoft” to “Microsoft” you can easy do that with single find and replace command.

    How about if you want to add some text say “Yousuff” at the end of all the word which started with the string passion.

    Sample TEXT:

    passionsoft.blogspot.com was moved to passion4java.blogspot.com. passion4java.blogspot.com has lot of post related java language. This is all related to passion. Passion is what matters than success and failure.

    Here we can’t use direct find/replace, so how do that? I was solving this previously by going manually to all the word which has passion and start appending at the end.. Which I really HATE to do  i.e if we have a text called “passion4java,passionsoft,passion!@#”, all has different value but starts with the string “passion”… I should not loose the text which are after the word passion and it is different for every word… So go about this??

    I believing there should be some way where you can overcome this using regular expression, later I found the Grouping command using regular expression which will group the pattern and word which are inside the pattern can be reused

    Example:



    Find text: passion([0-9A-Za-z]*)







    Replace text passion$1#--Yousuff--#





    It reduced most of my manual text changes , and I thought of share same with all..


    Please leave your comments if you find anything interesting :)

    Thursday, October 8, 2009

    Easy way to keep your Blog and Twitter up-to-date.

    I was thinking how can I keep my blog and my twitter up-to-date, if I add any new post in my blog and I have to come back to twitter to do the same.
    This is a redundant work which I have to do. Today when I was searching in the net found a cool web application which will update your twitter accounts if any update is made in your blogs.
    The way it works is it takes your RSS feeds of your blog and understands the updates based on that it updates your twitter account.

    twitterfeed

    Then I added follow me on twitter in blog ;)
    Try if you like it :)

    Wednesday, October 7, 2009

    How can we track from where user started to access your Web Application?

    Most of the time we want to know from where the user started to trigger our URL, most of the time it will via of google.com. It terms of marketing a product its very important to know how the URL was triggered...I was think the only way is to add the reference as a request parameter, but there is also another option is available in HTTP header fields.

    Referer Header Field:

    This field in the header object will give you the web page URL from which you application was triggered.
    Below code in java will give you the reference URL.


    String refUrl = request.getHeader("referer");

    Tuesday, October 6, 2009

    Is there any difference between java.lang.NoClassDefFoundError and java.lang.ClassNotFoundException ?

    When we started learning java most of time we may come across java.lang.ClassNotFoundException when we try to run some simple programs. Later we found that this error was throwing due to the class was not in the jvm class-path.
    java.lang.NoClassDefFoundError will be thrown when the class was loaded and it cannot be initialized properly. As JVM can find the classes in the class-path and it cannot create a Class object of the class which is loaded.
    Below are few scenarios where the class initializing may throw exception.
    ·        When a exception was thrown inside the static block of the class.
    o   The reason is the static block is executed when the Class object is getting loaded, if any error occurs it can't be initialized.
    ·        Any Exception was thrown when initialing a static variable of the class.
    o   The reason remains same as static block. It also getting executed during Class load.

    Then the morel of the story is when the class was not properly initialized then we will get java.lang.NoClassDefFoundError, mainly due the static blocks and static variables.
    Simple Example which will throw java.lang.NoClassDefFoundError..

    public class AnotherClass {
              static String test = "new";
              static AnotherClass staticObject = new AnotherClass();

              public AnotherClass() {
                       String a = null;
                       a.length();
              }
    }

    public class SimpleTest {
              public static void main(String[] args) {
                       AnotherClass a= null;
                       try {
                                 a = AnotherClass.staticObject;
                       } catch (Error e) {
                                 e.printStackTrace();
                       }
                       System.out.println(AnotherClass.test);
              }
    }
    When SimpleTest calles the "AnotherClass.staticObject" the class get loaded and the static variable staticObject calles the constructor where the initialization failes… below is the error trace

    java.lang.ExceptionInInitializerError
              at passionsoft.bloggerspot.com.SimpleTest.main(SimpleTest.java:11)
    Caused by: java.lang.NullPointerException
              at passionsoft.bloggerspot.com.AnotherClass.<init>(AnotherClass.java:12)
              at passionsoft.bloggerspot.com.AnotherClass.<clinit>(AnotherClass.java:7)
              ... 1 more
    Exception in thread "main" java.lang.NoClassDefFoundError
              at passionsoft.bloggerspot.com.SimpleTest.main(SimpleTest.java:19)

    As per my understanding I can think only two scenarios when the java.lang.NoClassDefFoundError was thrown if you guys know some more scenarios please update the comments.:)

    Monday, October 5, 2009

    Great Add-ons for Mobile Testing

    When you want to change your User-Agent name of your Browser (its nothing but your browser name which sent to the server on every request) you can use the "User Agent Switcher" Add-ons of Fire fox to change your user-agent....

    What is the Special about Changing User-Agent?

    The "User-Agent" is the only way (as of I know) the server come to know from which browser the user was triggered this request, based on that server may change its display logic...

    If you try google.com from your mobile, you will see a different version as compared to your google.com from your web browser (IE or Mozilla), that difference. If you are a mobile developer want to know how the data are displayed for a Specific model you can give the user agent string of that Mobile then trigger the request.

    For Example when you change your user-agent as "BlackBerry8800/4.2.1 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/100" and hit from you Fire-fox browser, the content will be displayed as it was seen in Blackberry 8800 Mobile…

    These Very good Add-ons if you need to test with different mobile devices (as I do right now)…J.. Please feel free to add your comments ….

     

    This content comes from a hidden element on this page.

    The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
    Click me, it will be preserved!

    If you try to open a new ColorBox while it is already open, it will update itself with the new content.

    Updating Content Example:
    Click here to load new content