Friday, February 6, 2009

J2SE 5.0 Features


Features of J2SE 5.0

    Generics
    Enhanced for Loop
    Autoboxing/Unboxing
    Typesafe Enums
    Varargs
    Static Import
    Metadata (Annotations)


Generics
       
        When you read an element out of a Collection, you must cast it to the type of element that is stored in the collection. Besides being inconvenient, this is unsafe. The compiler does not check that your cast is the same as the collection's type, so the cast can fail at run time.

        Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.
           

Enhanced for Loop

        It eliminates the drudgery and error-proneness of iterators and index variables when iterating over collections and arrays.

Autoboxing/Unboxing
       
        It allows you to put/retrieve primitive data types in the collection. e.g. You can't put an int (or other primitive value) into a collection.
        Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is Integer in the case of int).
        When you take the object out of the collection, you get the Integer that you put in; if you need an int, you must unbox the Integer using the intValue method.
        All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.
               

Typesafe Enums       
       
        Enumerated type facility allows you to create enumerated types with arbitrary methods and fields.
        It provides all the benefits of the Typesafe Enum pattern without the verbosity and the error-proneness.

   
Varags
       
        This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.

Static Import
       
        The static import construct allows unqualified access to static members without inheriting from the type containing the static members.

Metadata (Annotations)

        Metadata language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code.

Use of Abstract Class and Interface

When should I use an interface instead of an abstract class?

Use abstract Java classes when you want to provide some standard base code but want/need to force the user's
of your class to complete the implementation

(i.e., you create a skeleton implementation and the sub-classes must flesh it out)

Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.
For example, let bird be a class with a method fly(). It will be ridiculous for an aeroplane to inherit from bird class just
because it has the fly() method.
Rather the fly() method should be defined as an interface and both bird and aeroplane should implement that interface.



Tuesday, July 1, 2008

How to setup HyperTerminal To access device connected to Port?

Following Steps will show you how to setup Hyper Terminal to the Serial Port

1. Start --> Programs --> Accessories --> Communiations --> HyperTerminal



















2. Give any name so that this will be available for next access. Select Connect using proper port which shown in BlueSoleil Software.

















3. Select the properties in the the picture. This will configure the communication properties between HyperTerminal and Mobile.

















4. Type AT command in the Window, you will see the response as follows.



















5. You will get a complete list of AT-Commands to test various options of mobile using AT-Commands.

Sunday, June 29, 2008

How to install Java Communication Package on windows?

You can download Communication Package from following URL. This is a old package of Communication API.

http://people.inf.ethz.ch/hvogt/proj/pp/files/win/javacomm20-win32.zip

1. Unzip the file javacomm20-win32.zip to javacomm20-win32 directory.
2. Copy win32com.dll to your \bin directory.
3. Copy comm.jar to your \jre\lib\ext\ directory.
4. Copy javax.comm.properties to your \jre\lib directory.

You can check this installation with the Samples provided in the Java Communication Package.

Wednesday, December 19, 2007

How to install GlashFish Server on linux?

Installing GlassFish Server on Linux

1)    Install JDK on a system and set JAVA_HOME environmental variable.

       If you don't have GlassFish server, U can download GlassFish Server from following location.


       http://java.net/download/javaee5/v2_branch/promoted/Linux/glassfish-installer-v2-b58g.jar

2)    Run:

       java -Xmx256m -jar filename.jar

      This will extract jar file and create a new directory structure rooted under a directory named 'glassfish'.   

3)    cd glassfish

       Change Directory to GlassFish.

4)    If you are using a machine with an operating system that is a derivative of UNIX(tm), set the execute permission for the Ant binaries that are included with the GlassFish bundle.

      chmod -R +x lib/ant/bin
 
       lib/ant/bin/ant -f setup.xml


      OR for Windows:

      lib\ant\bin\ant -f setup.xml

5)     Start the server using the asadmin command. For that be maybe you first have to change the current directory to /glassfish/bin.

 
6)    next start the database

        
      asadmin> start-database

7)   and finally you can start the server...
       asadmin> start-domain domain1
      You can access your default page at

http://localhost:8080/


8) The server is now up and running. You can log into the web admin interface by the following url:

http://localhost:4848

Login as Admin - userid="admin", password ="adminadmin"

   

Friday, December 14, 2007

Enable Directory Listings in Tomcat

Enable Directory Listings in Tomcat

We can enable Directory listings for applications when welcome files are mentioned.

For that we have to use web.xml in conf folder. The lines which are make bold with blue color do the tricks.

    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


Virtual Hosting in Tomcat

Virtual Hosting in Tomcat

We can configure Virtual Hosting in Tomcat by simply adding Hosts in the server.xml file of Tomcat.

Here is the entry in the server.xml that configures Hosts in the Tomat.

 <Host name="www.nitinroman.com" appBase="webapps" unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
    </Host>
   
    <Host name="www.sachinroman.com" appBase="webapps" unpackWARs="true" autoDeploy="true"
       xmlValidation="false" xmlNamespaceAware="false">
    </Host>


After making an entry in the server.xml , restart  the Tomcat.

Finally add an entry in C:\WINDOWS\system32\drivers\etc\hosts file as follows

127.0.0.1    localhost
127.0.0.1    www.nitinroman.com
127.0.0.1    www.sachinroman.com




How to Execute Linux Machine Command using Java from Windows Operating System?

Here, In this example, we are execute Linux machine command using Java from windows operating machine.

In Java, This can be achieved using Command Prompt. To access any runtime (say DOS) we need an access to that Runtime. We can access runtime using Runtime class.

Runtime class has a static method called getRuntime() which returns Runtime object.
Then with exec() method we can have access to that runtime command.

To access Linux machine, we need plink - ( a command-line interface to the PuTTY back ends ) which can be downloaded from the following URL,

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

Here, Is the code snippet that will do the work.


class RuntimeTest
{
public static void main(String[] args)
{
try
{
Runtime runtime= Runtime.getRuntime ();

// Here , we have to access command line to run plink utility as well provide username , password and comman to execute on linux machine.

Process pro = runtime.exec("cmd /c plink.exe root@192.168.0.247 -pw nki.6300 mkdir test13456");

// Wait for the result of process.

int i = pro.waitFor();

if(i==0)
{
System.out.println("Exceuted");
}

}
catch(Exception ex)
{
System.out.println(ex);
}
}
}