Posts

finalize() method in java

protected void finalize () throws Throwable {} every class inherits the  finalize()  method from java.lang.Object the method is called by the garbage collector when it determines no more references to the object exist the Object finalize method performs no actions but it may be overridden by any class normally it should be overridden to clean-up non-Java resources ie closing a file if overridding  finalize()  it is good programming practice to use a try-catch-finally statement and to always call  super.finalize() . This is a safety measure to ensure you do not inadvertently miss closing a resource used by the objects calling class protected void finalize () throws Throwable { try { close (); // close open files } finally { super . finalize (); } } any exception thrown by  finalize()  during garbage collection halts the finalization but is otherwise ignored finalize()  is ne...

Plain Old Java Objects (POJO)

What is a POJO? "A POJO is a simple ordinary java object that does not extends classes or implements interfaces from any frameworks or APIs" The term "POJO" is mainly used to denote a Java object which does not follow any of the major Java object models, conventions, or frameworks.  A POJO is a Java object that doesn't implement any special interfaces  ,extends classes, defined in frameworks and APIs.  Ideally speaking, a POJO is a Java object not bound by any restriction other than those forced by the Java Language Specification. i.e a  POJO  should not  have to. Extend prespecified classes, as in public class Foo extends javax. servlet . http . HttpServlet { ... Implement prespecified interfaces, as in public class Bar implements javax. ejb . EntityBean { ... Contain prespecified annotations, as in @javax. persistence . Entity public class Baz { ... However, due to technical difficulties and other reasons, many ...

Create a domain in glassfish server

While installing Glassfish, sometimes it does not create domain due to java path error. Edit asenv and point to your JDK installation for set AS_JAVA=C:\Java\jdk. you can create a domain with the asadmin tool Asenv.bat is present at the directory C:\glassfish3\glassfish\config glassfish-install-dir\bin\ asadmin create-domain your-domain-name

Deploying a web module in glassfish server

To Start Glass Fish Server Open command Prompt and type any of below commands asadmin  start-domain asadmin start-domain [domain name] [--verbose] To Stop Glass Fish server asadmin stop-domain [domain name] To view the deployed applications asadmin list-applications To build, deploy, clean, undeploy  a web module using Ant 1.       open command prompt 2.        Go to the application context directory 3.        Type ant 4.        ant deploy for deploy a web module to domain 5.        ant clean to clean all build artifacts (deletes the built and dest folder) 6.        ant undeploy un deploys web module from domain

Java EE 6 Software Development Kit installation

Java EE 6 Software Development Kit installation setup requires JRE at the default installation path. Otherwise it will pop up warning message like "This application needs version 1.6 or higher of the java(TM) Runtime Environment." 1. Double click the java-ee-sdk-windows.exe to start installtion process 2.If the application does note found jre, installation can be done from command prompt. 3.In command prompt , Go to the path where setup is present 4.Type setup name - j followed by jre path in double quotes. ( java_ee_sdk-6u4-windows-ml -j "C:\Program Files\Java\jre7") 5.During the installation of the SDK, do the following. Configure the GlassFish Server administration user name as admin, and specify no password. This is the default setting.  Accept the default port values for the Admin Port (4848) and the HTTP Port (8080).  Allow the installer to download and configure the Update Tool. If you access the Internet through a firewall, provide the proxy ...

How to set current jre version in windows environment

Image
You can set the current version of JRE in windows as given below. Open windows registry. (Run --> regedit) Go to HKEY_LOCAL_MACHINE -->SOFTWARE --> Javasoft --> Java Runtime Enviroment Edit and Change the Current Version in Java Runtime Environment

Java code compilation process

Image
When you compile a java file using javac compiler, it produces a file with .class extension. The class file consists of bytes code. Byte code is platform independent as it can be run on any platform once it is compiled.

Prevent Winmail.dat Attachments from Being Sent in Outlook

Select Tools | Options... from the menu. Go to the Mail Format tab. Under Compose in this message format: , make sure either HTML or Plain Text is selected. Click Internet Format . Make sure either Convert to Plain Text format or Convert to HTML format is selected under When sending Outlook Rich Text messages to Internet recipients, use this format: Click OK . Click OK again. 

Why Email attachments are downloaded as winmail.dat file ?

Transport-Neutral Encapsulation Format (TNEF) is Microsoft's non-standard format for encapsulating mail which has any non-plain-text content or properties (such as rich text, embedded OLE objects, voting buttons, and sometimes just attachments). Whether or not a given message is encoded using TNEF is determined by the Outlook default settings, per-recipient setting, Exchange Server settings, and message type and content. Once a TNEF message is used, the entire message, including all the original attachments and properties, is encapsulated in a single attachment of mime type "application/ms-tnef" added to the message to be sent over the Internet. This attachment is usually named "WINMAIL.DAT", and when sent to any non-MS mail client, is useless, and makes access to the original message attachments impossible. You can parse the winmail.dat file using the tnef.jar file in order to extract the attachments For more information please visit the belo...

Creating a Thread

Image
A Thread can be created in two ways. By Extending Thread class By Implementing Runnable interface Examples 1. Creating a Thread by extending Thread class class  MyThread  extends  Thread{   String s=null;    MyThread(String s1){    s=s1;    start();    }    public void  run(){    System.out.println(s);    } } public class  RunThread{    public static void  main(String args[]){         MyThread m1= new  MyThread( "Thread started...." );   } } 2. Creating a Thread by implementing Runnable interface class  MyThread1  implements  Runnable{    Thread t;   String s=null;    MyThread1(String s1){    s=s1;    t= new  Thread(this);    t.start();    }    public voi...

Scheduler example

Category.java package com.sandbox.scheduler.model; public enum Category {     RED(1),     GREEN(2),     BLUE(3);     private int value;     public int getValue() {         return value;     }     private Category(int value) {         this.value = value;     } } Task.java package com.sandbox.scheduler.model; import java.time.LocalDateTime; import java.util.UUID; import com.sandbox.scheduler.model.Category; public class Task {     public int urgency = -1;     public Category category;     public LocalDateTime timestamp = LocalDateTime.now();     public UUID uuid = UUID.randomUUID();     public Task(int urgency, Category category) {    ...

What is an interface?

Interface is a reference type which consists of only method declarations and constants. All methods in interface by default public abstract and all fields are public static final eventhough it has not been mentioned explicitly. Example: public interface Car { int wheel=4; int paintCar(String color); int driveCar(); }

Why String is immutable in java?

1)Imagine StringPool facility without making string immutable , its not possible at all because in case of string pool one string object/literal e.g. "Test" has referenced by many reference variables , so if any one of them change the value others will be automatically gets affected i.e. lets sayString A = "Test"String B = "Test" Now String B called "Test".toUpperCase() which change the same object into "TEST" , so A will also be "TEST" which is not desirable. 2)String has been widely used as parameter for many java classes e.g. for opening network connection you can pass hostname and port number as stirng , you can pass database URL as string for opening database connection, you can open any file by passing name of file as argument to File I/O classes.In case if String is not immutable , this would lead serious security threat , I mean some one can access to any file for which he has authorization and then can change the file...

Asynchronous Messaging

Asynchronous messaging describes communications that takes place between two applications or systems, where the system places a message in a message queue (called an Event Queue in enterprise messaging systems ) and does not need to wait for a reply to continue processing. Contrast with synchronous messaging .

Asynchronous Messaging

Asynchronous messaging describes communications that takes place between two applications or systems, where the system places a message in a message queue (called an Event Queue in enterprise messaging systems ) and does not need to wait for a reply to continue processing. Contrast with synchronous messaging .

Understanding Java Memory Settings

As part of the Best Practices, we know that we should be setting -Xms & -Xmx Java command line parameters. What are these settings and why is it required to set these. As JAVA starts, it creates within the systems memory a Java Virtual Machine (JVM). JVM is where the complete processing of any Java program takes place. All JAVA applications (including IQv6) by default allocates & reserves up to 64 MB of memory resource pool from the system on which it is running. The Xms is the initial / minimum Java memory (heap) size within the JVM. Setting the initial memory (heap) size higher can help in a couple of ways. First, it will allow garbage collection (GC) to work less which is more efficient. The higher initial memory value will cause the size of the memory (heap) not to have to grow as fast as a lower initial memory (heap) size, thereby saving the overhead of the Java VM asking the OS for more memory. The Xmx is the maximum Java memory (heap) size within the Java Virtual Memory ...

Understanding java Memory settings

As part of the Best Practices, we know that we should be setting -Xms & -Xmx Java command line parameters. What are these settings and why is it required to set these. As JAVA starts, it creates within the systems memory a Java Virtual Machine (JVM). JVM is where the complete processing of any Java program takes place. All JAVA applications (including IQv6) by default allocates & reserves up to 64 MB of memory resource pool from the system on which it is running. The Xms is the initial / minimum Java memory (heap) size within the JVM. Setting the initial memory (heap) size higher can help in a couple of ways. First, it will allow garbage collection (GC) to work less which is more efficient. The higher initial memory value will cause the size of the memory (heap) not to have to grow as fast as a lower initial memory (heap) size, thereby saving the overhead of the Java VM asking the OS for more memory. The Xmx is the maximum Java memory (heap) size within the Java Virtual Memory ...

What are PermSize and MaxPermSize and how they work in Java.

Permanent Generation (which is “Perm” in PermSize) The permanent generation is used to hold reflective of the VM itself such as class objects and method objects. These reflective objects are allocated directly into the permanent generation, and it is sized independently from the other generations. Generally, sizing of this generation can be ignored because the default size is adequate. However, programs that load many classes may need a larger permanent generation. PermSize is additional separate heap space to the -Xmx value set by the user. The section of the heap reserved for the permanent generation holds all of the reflective data for the JVM. You should adjust the size accordingly if your application dynamically load and unload a lot of classes in order to optimize the performance. By default, MaxPermSize will be 32mb for -client and 64mb for -server. However, if you do not set both PermSize and MaxPermSize, the overall heap will not increase unless it is needed. When you set both...

OOPS Concepts with realtime examples

1. Abstraction Abstraction helps to hide the non essential features from the user and makes the application user friendly. Eg. Take your mobile phone. You are having buttons and know the purpose but no need to understand how it prints number on the screen when it is pressed. 2. Encapsulation It bundles the code into a single unit which makes the code very easy to handle. Eg. Take computer keyboard. All the buttons have been enclosed. 3. Inheritance It helps to inherit the superclass properties to subclasses. Eg. Father - Son relationship. 4. Polymorphism A entity which behaves differntly at different places. Eg. A person behaves as a good husband in family. He behaves as a good employee at company. He also behaves as a good citizen in public

Object Oriented Concepts

There are four main object oriented concepts in java. 1. Abstraction 2. Encapsultion 3. Inheritance 4. Polymorphism