Friday, March 22, 2013

Creating Debian Installer


Creating Debian installer(.deb) for linux based system makes the installation process very simple because we do not need to worry about the required files and dependencies because everything will be there in the package. Here I am going some basic steps on how can we create the debian installer:

( I am using Ubuntu 12.04 LTS)

1. Prepare Development Environment


$ sudo apt-get install packaging-dev

This installs everything you need.
(For detail: http://developer.ubuntu.com/packaging/html/getting-set-up.html)

2. Packaging

Install build-essential if it is not already installed in the system.

For detail here: http://developer.ubuntu.com/packaging/html/packaging-new-software.html#





Wednesday, March 20, 2013

Java Confusions

1) Why static block?

Java static blocks are used to initialize the static variables. The static block code is loaded when Java Virtual Machine(JVM) loads the class.  Since we know, static variables can not be included in constructor, only member variables can be initialized in constructor. So, for static variables, the static blocks work like a constructor. We can have any number of static blocks that are executed at the time class is loaded. See the example below:

class MyClass { 
 static Map labels = new HashMap(); 
 static { labels.put(5.5, "five and a half"); 
 labels.put(7.1, "seven point 1"); } 
 //... 
}

2) Why final keyword for classes, variables or arguments

Please have a look at my previous article which clarified in somehow more detail:
Final keyword always confusing! 


(TO be continued...)

Monday, March 18, 2013

XML serialization and de-serialization in Java

It is sometimes a great issue when one wants to serialize their objects into XML files. We have JAXB(Java Architecture for XML Binding) API which can serialize and also de-serialize the java object. I will introduce frequently used annotation used for XML serialization with example:

1. Annotating XML variables


@XmlRootElement(namespace="className" or name="className")
@XmlType(propOrder={"property", "list"})
public class className{
   private int attribute;
   private String property;
   private String value;
   private List list;

  @XmlAttribute(value="attribute")
  public int getAttribute(){
   return this.attribute;
  }

@XmlElement(value="property")
  public String getProperty(){
   return this.property;
  }
/*
   @XmlValue
   public String getValue(){
   return this.value;
   }
*/

@XmlElementWrapper(name="lists")
@XmlElement(name="list")
public List getList(){
  return this.list;
 }
}

We can define the annotations for extended objects as follows:

@XmlElementRefs({@XmlElementRef(type=extendedClass1.class),@XmlElementRef(type=extendedClass2.class),@XmlElementRef(type=extendedClass3.class)})
public baseClassObject getObject(){
       return this.object;
}
 

2. Serializing Object


JAXBContext context = JAXBContext.newInstance(className.class);
Marshaller m = context.createMarshaller();
//Format the output
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(classNameObject, fileObject); 
// or to just print to the console
m.marshal(classNameObject, System.out);


3. DeSerialising Object

JAXBContext context = JAXBContext.newInstance(className.class);
Unmarshaller m = context.createUnmarshaller();
classNameObject = (className)m.unmarshal(fileObject);