<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Blog &#187; Core Java</title>
	<atom:link href="http://javapapers.com/category/core-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://javapapers.com</link>
	<description>Blog on core java, servlets, jsp and design patterns.</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:25:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>JVM Shutdown Hook</title>
		<link>http://javapapers.com/core-java/jvm-shutdown-hook/</link>
		<comments>http://javapapers.com/core-java/jvm-shutdown-hook/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 23:27:15 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=680</guid>
		<description><![CDATA[Java JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run. JVM&#8217;s shutdown sequence has two phases. All registered shutdown hooks are started in some unspecified order and allowed to run concurrently until they finish. All un-invoked finalizers are [...]]]></description>
			<content:encoded><![CDATA[<p>Java JVM provides you a hook to register a thread with the shutdown initiation sequence. Once a thread is registered, on every shutdown that thread is run.</p>
<p>JVM&#8217;s shutdown sequence has two phases.</p>
<ol>
<li>All registered shutdown hooks are started in some unspecified order and allowed to run concurrently until they finish.</li>
<li>All un-invoked finalizers are run if finalization-on-exit has been enabled.</li>
</ol>
<p>After the above two phases are complete, <a title="Differentiate JVM JRE JDK JIT" href="http://javapapers.com/core-java/differentiate-jvm-jre-jdk-jit/">java virtual machine</a> halts.</p>
<p>Its not a complicated exercise, but a less known feature of java. You might require this to release critical resources in the event of unexpected JVM shutdown. You use finally in a try block to release resources and that is completely different.</p>
<p><img class="alignnone size-full wp-image-681" title="shutdownhook" src="http://javapapers.com/wp-content/uploads/2011/12/shutdownhook.jpg" alt="" width="277" height="186" /></p>
<p>Just a single line of code will register with the hook of JVM shutdown. You use, Runtime class to register the thread.</p>
<pre class="brush: java;">System.getRuntime().addShutdownHook(&lt;thread instance&gt;);</pre>
<ul>
<li>If you register multiple threads with shutdown hook, all those threads will be run in parallel on shutdown. If you wish to run them in a sequence, then you need to have only one thread registered and have your custom logic controlled within that.</li>
<li>If you are shutting down the VM using <em>Runtime.getRuntime().halt</em>, this will not invoke the shutdown hook. It will abruptly shutdown all running process and close the show.</li>
</ul>
<h2>Example Code for JVM Shutdown Hook</h2>
<pre class="brush: java;">
public class JVMShutdownHookTest {
  public static void main(String[] args) {
    JVMShutdownHook jvmShutdownHook = new JVMShutdownHook();
    Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
    System.out.println(&quot;JVM Shutdown Hook Registered.&quot;);
    System.out.println(&quot;Pre exit.&quot;);
    System.exit(0);
    System.out.println(&quot;Post exit.&quot;);
  }

  private static class JVMShutdownHook extends Thread {
    public void run() {
      System.out.println(&quot;JVM Shutdown Hook: Thread initiated.&quot;);
    }
  }
}

Output:
JVM Shutdown Hook Registered.
Pre exit.
JVM Shutdown Hook: Thread initiated.
</pre>
<p>In the above program, I manualy call the System.exit() and make the JVM to shutdown. Once the System.exit is invoked the phase I of shutdown sequence is initiated and that starts then registered thread &#8216;jvmShutdownHook&#8217; and then halts the JVM.</p>
<p>Runtime.runFinalizersOnExit() method is almost similar to addShutdownHook() but its depreciated in 1.2</p>
<p>In case if you are wondering on how to detect the web context shutdown in application server scenarios, you can use ServletContextListner</p>
<h2>When shutdown hook will NOT be initiated</h2>
<p>There is no guarantee that always the shutdown hooks will run. There might be a devastating crash of the whole system and JVM might crash due to it. In similar circumstances there is no guarantee that shut down hooks will invoke the thread. Similarly if native OS kills the JVM process then this shutdown hook sequence will not be initiated. Therefore in all cases when the JVM is shutdown abnormally the hook will not be initiated.</p>
<p>Note:</p>
<ul>
<li>You cannot register a shutdown hook after a shutdown is intiated. That is, if you try to register a thread to shutdown hook from within another thread that is already registered with the hook you will get IllegalStateException.</li>
<li>You can use <em>Runtime.getRuntime().halt(status);</em> to halt JVM abruptly after the shutdown sequence is initiated.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/jvm-shutdown-hook/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Externalizable vs Serializable</title>
		<link>http://javapapers.com/core-java/externalizable-vs-serializable/</link>
		<comments>http://javapapers.com/core-java/externalizable-vs-serializable/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 23:30:08 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=675</guid>
		<description><![CDATA[Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization. Serializable defines standard protocol and provides out of the box serialization capabilities. Externalizable extends Serializable. Implement writeExternal and readExternal methods of the Externalizable interface and create your own contract / protocol for serialization. Saving the state of the [...]]]></description>
			<content:encoded><![CDATA[<ul>
<li>Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization. <a title="Java Serialization" href="http://javapapers.com/core-java/java-serialization/">Serializable defines standard protocol</a> and provides out of the box serialization capabilities.</li>
<li>Externalizable extends Serializable.</li>
<li>Implement writeExternal and readExternal methods of the Externalizable interface and create your own contract / protocol for serialization.</li>
<li>Saving the state of the supertypes is responsibility of the implementing class.</li>
<li>You might have seen in my previouse article on how to <a title="Customize Default Serialization Protocol" href="http://javapapers.com/core-java/customize-default-serialization-protocol/">customize the default implementation of Serializable</a>. These two methods readExternal and writeExternal (Externalizable) supersedes this customized implementation of readObject and writeObject.</li>
<li>In object de-serialization (reconsturction) the public no-argument constructor is used to reconstruct the object. In case of Serializable, instead of using constructor, <a title="Object Construction in Serialization" href="http://javapapers.com/core-java/object-construction-in-serialization/">the object is re-consturcted using data read from ObjectInputStream</a>.</li>
<li>The above point subsequently mandates that the Externalizable object must have a public no-argument constructor. In the case of Seriablizable it is not mandatory.</li>
</ul>
<p><img class="alignnone size-full wp-image-676" title="serializable_externalizable" src="http://javapapers.com/wp-content/uploads/2011/12/serializable_externalizable.jpg" alt="" width="272" height="185" /></p>
<ul>
<li>Behaviour of writeReplace and readResolve methods are same for both Serializable and Externalizable objects. writeReplace allows to nominate a replacement object to be written to the stream. readResolve method allows to designate a replacement object for the object just read from the stream.</li>
<li>In most real time scenarios, you can use Serializable and write your own custom implementation for serialization by providing readObject and writeObject.</li>
</ul>
<p>You may need Externalizable,</p>
<ol>
<li>If you are not happy with the way java writes/reads objects from stream.</li>
<li>Special handling for supertypes on object construction during serialization.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/externalizable-vs-serializable/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>serialVersionUID in Java Serialization</title>
		<link>http://javapapers.com/core-java/serialversionuid-in-java-serialization/</link>
		<comments>http://javapapers.com/core-java/serialversionuid-in-java-serialization/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 23:30:43 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=665</guid>
		<description><![CDATA[serialVersionUID is used to ensure that during deserialization the same class (that was used during serialize process) is loaded. This is a one line definition to explain why a serialVersionUID is used? Apart from the above definition there are quite  a few things to learn from this serialVersionUID. As per javadocs, following is format of [...]]]></description>
			<content:encoded><![CDATA[<p>serialVersionUID is used to ensure that during deserialization the same class (that was used during serialize process) is loaded. This is a one line definition to explain why a serialVersionUID is used?<br />
<img class="alignnone size-full wp-image-670" title="oops" src="http://javapapers.com/wp-content/uploads/2012/01/oops.jpg" alt="" width="313" height="219" /></p>
<p>Apart from the above definition there are quite  a few things to learn from this serialVersionUID. As per javadocs, following is format of serialVersionUID:</p>
<h2>serialVersionUID Syntax</h2>
<p><code>ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;</code></p>
<ul>
<li>serialVersionUID is a static final field. You can assign any number of your choice to it. Later I will explain the significance of these two statements.</li>
</ul>
<h2>Why serialVersionUID?</h2>
<p>Lets start with annoying warning message you get in your IDE when you declare a class as Serializable.</p>
<blockquote><p>The serializable class Lion does not declare a static final serialVersionUID field of type long</p></blockquote>
<p>Most of us used to ignore this message as we always do for a warning. My general note is, always pay attention to the java warning messages. It will help you to learn a lot of fundamentals.</p>
<p><img class="alignnone size-full wp-image-667" title="serialVersionUID" src="http://javapapers.com/wp-content/uploads/2011/12/serialVersionUID.png" alt="" width="479" height="308" /></p>
<p>serialVersionUID is a must in serialization process. But it is optional for the developer to add it in java source file. If you are not going to add it in java source file, serialization runtime will generate a serialVersionUID and associate it with the class. The serialized object will contain this serialVersionUID along with other data.</p>
<p>Even though serialVersionUID is a static field, it gets serialized along with the object. This is one exception to the general serialization rule that, &#8220;static fields are not serialized&#8221;.</p>
<h2>How serialVersionUID is generated?</h2>
<p>serialVersionUID is a 64-bit hash of the class name, interface class names, methods and fields. Serialization runtime generates a serialVersionUID if you do not add one in source. Refer this <a href="http://docs.oracle.com/javase/6/docs/platform/serialization/spec/class.html#4100">link for the algorithm to generate serialVersionUID</a>.</p>
<p>It is advised to have serialVersionUID as unique as possible. Thats why the java runtime chose to have such a complex algorithm to generate it.</p>
<p>If you want help in generating it, jdk tools provides a tool named <em>serialver</em>. Use <strong><em>serialver -show</em></strong> to start the gui version of the tool as shown below.</p>
<p><img class="alignright size-full wp-image-666" title="serialver" src="http://javapapers.com/wp-content/uploads/2011/12/serialver.png" alt="" width="616" height="272" /></p>
<h2>How serialVersionUID works?</h2>
<p>When an object is serialized, the serialVersionUID is serialized along with the other contents.</p>
<p>Later when that is deserialized, the serialVersionUID from the deserialized object is extracted and compared with the serialVersionUID of the loaded class.</p>
<p>If the numbers do not match then, <code>InvalidClassException</code> is thrown.</p>
<p>If the loaded class is not having a serialVersionUID declared, then it is automatically generated using the same algorithm as before.</p>
<h2>Strongly recommended to declare serialVersionUID</h2>
<p>Javadocs says,</p>
<blockquote><p>&#8220;the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization&#8221;</p></blockquote>
<p>Now you know why we should declare a serialVersionUID.</p>
<p>Not only declaring a serialVersionUID is sufficient. You must do the following two things carefully. Otherwise it defeats the purpose of having the serialVersionUID.</p>
<p>serialVersionUID should be maintained. As and when you change anything in the class, you should upgrade the serailVersionUID.<br />
Try your best to declare a unique serialVersionUID.</p>
<h2>Demonstrate serialVersionUID</h2>
<p>Initial class to be serialized has a serialVersionUID as 1L.</p>
<pre class="brush: java;">
import java.io.Serializable;

public class Lion implements Serializable {

  private static final long serialVersionUID = 1L;
  private String sound;

  public Lion(String sound) {
    this.sound = sound;
  }

  public String getSound() {
    return sound;
  }

}
</pre>
<p>Test serialVersionUID:</p>
<pre class="brush: java;">
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerialVersionUIDTest {

  public static void main(String args[]) throws IOException, ClassNotFoundException {
    Lion leo = new Lion(&quot;roar&quot;);
    // serialize

    System.out.println(&quot;Serialization done.&quot;);
    FileOutputStream fos = new FileOutputStream(&quot;serial.out&quot;);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(leo);

    // deserialize
    FileInputStream fis = new FileInputStream(&quot;serial.out&quot;);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Lion deserializedObj = (Lion) ois.readObject();
    System.out.println(&quot;DeSerialization done. Lion: &quot; + deserializedObj.getSound());
  }
}
</pre>
<h3>Output:</h3>
<pre class="brush: java;">
Serialization done.
DeSerialization done. Lion: roar
</pre>
<p>Now change serialVersionUID to 2L in Lion class.</p>
<pre class="brush: java;">  private static final long serialVersionUID = 2L;</pre>
<p>Comment the &#8220;serialize&#8221; block (4 lines of code) in SerialVersionUIDTest. Now run it and you will get the following exception.</p>
<ol>
<li>Serialized Lion with serialVersionUID with 1L.</li>
<li>Changed serialVersionUID to 2L and compiled and loaded the class.</li>
<li>Deserialize the already serialized object and load it with the latest class.</li>
<li>We get exception as serialVersionUID is not matching.</li>
</ol>
<pre class="brush: java;">
Exception in thread &quot;main&quot; java.io.InvalidClassException: Lion; local class incompatible: &lt;strong&gt;stream classdesc serialVersionUID = 1, local class serialVersionUID = 2&lt;/strong&gt;
	at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
	at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
	at java.io.ObjectInputStream.readClassDesc(Unknown Source)
	at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
	at java.io.ObjectInputStream.readObject0(Unknown Source)
	at java.io.ObjectInputStream.readObject(Unknown Source)
	at SerialVersionUIDTest.main(SerialVersionUIDTest.java:21)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/serialversionuid-in-java-serialization/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>Java Versions, Features and History</title>
		<link>http://javapapers.com/core-java/java-features-and-history/</link>
		<comments>http://javapapers.com/core-java/java-features-and-history/#comments</comments>
		<pubDate>Sun, 01 Jan 2012 23:30:35 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=655</guid>
		<description><![CDATA[A popular interview question in java is &#8220;what is new in Java version X?&#8221;. Is that an intelligent question is debatable. I have summarized below important new features added in each major java release till now. I target to highlight important features added in respective release. Apart from below list of features, every release has [...]]]></description>
			<content:encoded><![CDATA[<p>A popular interview question in java is &#8220;what is new in Java version X?&#8221;. Is that an intelligent question is debatable. I have summarized below important new features added in each major java release till now. I target to highlight important features added in respective release. Apart from below list of features, every release has enhancements and lots of bug fixes.</p>
<h2>Java Version SE 7</h2>
<p>Code named Dolphin and released on July 28, 2011.<br />
<img class="alignright size-full wp-image-656" title="dolphin" src="http://javapapers.com/wp-content/uploads/2011/12/dolphin.jpg" alt="" width="208" height="243" /></p>
<h3>New features in Java SE 7</h3>
<ul>
<li>Strings in switch Statement</li>
<li>Type Inference for Generic Instance Creation</li>
<li>Multiple Exception Handling</li>
<li>Support for Dynamic Languages</li>
<li>Try with Resources</li>
<li>Java nio Package</li>
<li>Binary Literals, underscore in literals</li>
<li>Diamond Syntax</li>
<li>Automatic null Handling</li>
</ul>
<h2>Java Version SE 6</h2>
<p>Code named Mustang and released on December 11, 2006.</p>
<p><img class="alignright size-full wp-image-658" title="mustang" src="http://javapapers.com/wp-content/uploads/2011/12/mustang.jpg" alt="" width="259" height="194" /></p>
<h3>New features in Java SE 6</h3>
<ul>
<li>Scripting Language Support</li>
<li>JDBC 4.0 API</li>
<li>Java Compiler API</li>
<li>Pluggable Annotations</li>
<li>Native PKI, Java GSS, Kerberos and LDAP support.</li>
<li>Integrated Web Services.</li>
<li>Lot more enhancements.</li>
</ul>
<h2>J2SE Version 5.0</h2>
<p>Code named Tiger and released on September 30, 2004.</p>
<p><img class="alignright size-full wp-image-659" title="tiger" src="http://javapapers.com/wp-content/uploads/2011/12/tiger.jpg" alt="" width="259" height="194" /></p>
<h3>New features in J2SE 5.0</h3>
<ul>
<li>Generics</li>
<li>Enhanced for Loop</li>
<li>Autoboxing/Unboxing</li>
<li>Typesafe Enums</li>
<li>Varargs</li>
<li>Static Import</li>
<li>Metadata (Annotations)</li>
<li>Instrumentation</li>
</ul>
<h2>J2SE Version 1.4</h2>
<p>Code named Merlin and released on February 6, 2002 (first release under JCP).</p>
<p><img class="alignright size-full wp-image-660" title="merlin" src="http://javapapers.com/wp-content/uploads/2011/12/merlin.jpg" alt="" width="275" height="183" /></p>
<h3>New features in J2SE 1.4</h3>
<ul>
<li>XML Processing</li>
<li>Java Print Service</li>
<li>Logging API</li>
<li>Java Web Start</li>
<li>JDBC 3.0 API</li>
<li>Assertions</li>
<li>Preferences API</li>
<li>Chained Exception</li>
<li>IPv6 Support</li>
<li>Regular Expressions</li>
<li>Image I/O API</li>
</ul>
<h2>J2SE Version 1.3</h2>
<p>Code named Kestrel and released on May 8, 2000.</p>
<p><img class="alignright size-full wp-image-661" title="Kestrel" src="http://javapapers.com/wp-content/uploads/2011/12/Kestrel.jpg" alt="" width="265" height="190" /></p>
<h3>New features in J2SE 1.3</h3>
<ul>
<li>Java Sound</li>
<li>Jar Indexing</li>
<li>A huge list of enhancements in almost all the java area.</li>
</ul>
<h2>J2SE Version 1.2</h2>
<p>Code named Playground and released on December 8, 1998.</p>
<h3>New features in J2SE 1.2</h3>
<ul>
<li>Collections framework.</li>
<li>Java String memory map for constants.</li>
<li>Just In Time (JIT) compiler.</li>
<li>Jar Signer for signing Java ARchive (JAR) files.</li>
<li>Policy Tool for granting access to system resources.</li>
<li>Java Foundation Classes (JFC) which consists of Swing 1.0, Drag and Drop, and Java 2D class libraries.</li>
<li>Java Plug-in</li>
<li>Scrollable result sets, BLOB, CLOB, batch update, user-defined types in JDBC.</li>
<li>Audio support in Applets.</li>
</ul>
<h2>JDK Version 1.1</h2>
<h2>Released on February 19, 1997</h2>
<h3>New features in JDK 1.1</h3>
<ul>
<li>JDBC (Java Database Connectivity)</li>
<li>Inner Classes</li>
<li>Java Beans</li>
<li>RMI (Remote Method Invocation)</li>
<li>Reflection (introspection only)</li>
</ul>
<h2>JDK Version 1.0</h2>
<p>Codenamed Oak and released on January 23, 1996.</p>
<p>Wishing you a happy new year!</p>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/java-features-and-history/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Install Java in Linux (Ubuntu / Debian)</title>
		<link>http://javapapers.com/core-java/install-java-jdk-in-linux-ubuntu-debian/</link>
		<comments>http://javapapers.com/core-java/install-java-jdk-in-linux-ubuntu-debian/#comments</comments>
		<pubDate>Sun, 25 Dec 2011 23:56:39 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=626</guid>
		<description><![CDATA[This manual is to help install java (jdk) in Ubuntu / Debian Linux. Previously we discussed about how java&#8217;s installation has evolved over a period, then I promised to write an article on installing java on Linux. Yes I love Linux! Before install you should be aware of couple of things. Which package I need [...]]]></description>
			<content:encoded><![CDATA[<p>This manual is to help install java (jdk) in Ubuntu / Debian Linux. Previously we discussed about how java&#8217;s installation has evolved over a period, then I promised to write an article on installing java on Linux. Yes I love Linux!</p>
<p>Before install you should be aware of couple of things.</p>
<ol>
<li>Which package I need to install</li>
<li>Is that package already installed in my linux box</li>
</ol>
<h2><img class="alignright size-full wp-image-631" title="java-ubuntu" src="http://javapapers.com/wp-content/uploads/2011/12/java-ubuntu.jpg" alt="" width="200" height="200" /></h2>
<h2>Which Package to Install?</h2>
<p>&#8216;Generally&#8217; we use sun&#8217;s (oracle) jdk. If you do not have precondition on which package, then use sun&#8217;s package. FOSS equivalent alternative for sun JDK is openjdk.</p>
<p>To know the list of jdk packages available for you to install, use the following command:</p>
<pre class="brush: plain;">$ dpkg-query -l '*jdk*'</pre>
<h2>Is java already installed in my linux?</h2>
<pre class="brush: plain;">$ which java </pre>
<p>or</p>
<pre class="brush: plain;">$ java -version</pre>
<p>Above two commands is the easiest way to identify if java is installed in your linux. But this will work only if you have installed jdk using standard means and the path are setup correctly.</p>
<p>Otherwise,</p>
<ul>
<li>If you know the exact package name, you can use following command:</li>
</ul>
<pre class="brush: plain;">$ dpkg-query -W -f='${Status} ${Version}\n' sun-java6-jdk</pre>
<p>in the above command, sun-java6-jdk is the package name.</p>
<p>If your jdk is not installed you will get the following response:<br />
&#8220;No packages found matching sun-java6-jdk.&#8221;</p>
<p>or you can use the following command to get a detailed output</p>
<pre class="brush: plain;">$ dpkg -s sun-java6-jdk</pre>
<h3>Java Installed but path not set or don&#8217;t know the path</h3>
<p>Use the following commands to know the path of jdk installation</p>
<pre class="brush: plain;"> $ sudo updatedb
$ locate java </pre>
<h2>Install Jdk</h2>
<p>As of the time of writing this article sun&#8217;s package is not available in the partner repositories. Therefore you will not be able to do live installation using defalut repositories.<br />
<img class="alignright size-full wp-image-632" title="java" src="http://javapapers.com/wp-content/uploads/2011/12/java.png" alt="" width="130" height="130" /><br />
Either, you need to add additional repository to your list or download package from available site and install it offline.</p>
<h3>Method 1: Live Install</h3>
<p>You need a live internet connection. Run the following commands sequentially.</p>
<pre class="brush: plain;">
$ sudo apt-get install python-software-properties

$ sudo add-apt-repository ppa:sun-java-community-team/sun-java6

$ sudo apt-get update

$ sudo apt-get install sun-java6-jdk
</pre>
<h3>Method 2: Download and Install</h3>
<p><a href="http://archive.canonical.com/pool/partner/s/sun-java6/">http://archive.canonical.com/pool/partner/s/sun-java6/</a></p>
<p>This url has list of Sun&#8217;s packages available. Download the appropriate package and use the following command:</p>
<pre class="brush: plain;"> $ sudo dpkg -i package.deb </pre>
<p>After this, your JAVA_HOME will be:</p>
<pre>JAVA_HOME="/usr/lib/jvm/java-6-sun-1.6.0.06"</pre>
<p>Wishing you Merry Christmas!</p>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/install-java-jdk-in-linux-ubuntu-debian/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Customize Default Serialization Protocol</title>
		<link>http://javapapers.com/core-java/customize-default-serialization-protocol/</link>
		<comments>http://javapapers.com/core-java/customize-default-serialization-protocol/#comments</comments>
		<pubDate>Sun, 18 Dec 2011 23:46:04 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Core Java]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=649</guid>
		<description><![CDATA[Do you know how to customize the default behaviour of the serialization protocol? Tom Cruise starrer MI4 hits theatres this week in India and we experience unusual media frenzy. In no way this is realted but by impulse I mention about MI4 here. In this article I am going to write on how to customize [...]]]></description>
			<content:encoded><![CDATA[<p>Do you know how to customize the default behaviour of the serialization protocol? Tom Cruise starrer MI4 hits theatres this week in India and we experience unusual media frenzy. In no way this is realted but by impulse I mention about MI4 here. In this article I am going to write on how to customize Ghost protocol, oops serialization protocol.</p>
<p><img class="alignright size-full wp-image-650" title="ghostprotocol" src="http://javapapers.com/wp-content/uploads/2011/12/ghostprotocol.jpg" alt="" width="259" height="194" /></p>
<p>One of my reader was asked a quesion in an interview on how to customize the serialization behaviour and this article serves as an answer for that question.</p>
<p>In general there are three approaches to serialization in java:</p>
<ol>
<li>Implement Serializable and use default protocol.</li>
<li>Implement Serializable and get a chance to modify the default protocol.</li>
<li>Implement Externalizable and write your own protocol to implement serailization.</li>
</ol>
<p>Here protocol means, the way (process or approach) object is serialized and de-serialized.</p>
<p>We have seen enough about using the <a title="Java Serialization" href="http://javapapers.com/core-java/java-serialization/">default protocol to implement serialization</a> and <a title="Object Construction in Serialization" href="http://javapapers.com/core-java/object-construction-in-serialization/">how instances are created during serialization</a>. In this current article we shall see about modifying the default protocol.</p>
<pre class="brush: java;">
import java.io.Serializable;

public class Lion implements Serializable {
  private String sound;
  public Lion() {
    System.out.println(&quot;Lion's constructor invoked.&quot;);
    setSound(&quot;roar&quot;);
  }
  public String getSound(){
    return sound;
  }
  public void setSound(String sound){
    this.sound = sound;
  }
}
</pre>
<p>Above is a simple java class which we are going to use to demonstrate serialization. First let use do simple straight forward serialization and then make the customization in step 2.</p>
<pre class="brush: java;">
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class JavaSerialize {

  public static void main(String args[]) throws IOException, ClassNotFoundException {
    Lion leo = new Lion();
    // serialize
    System.out.println(&quot;Serialization done.&quot;);
    FileOutputStream fos = new FileOutputStream(&quot;serial.out&quot;);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(leo);

    // deserialize
    FileInputStream fis = new FileInputStream(&quot;serial.out&quot;);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Lion deserializedObj = (Lion) ois.readObject();
    System.out.println(&quot;DeSerialization done. Lion: &quot; + deserializedObj.getSound());
  }
}
</pre>
<p>Output:</p>
<pre class="brush: plain;">
Lion's constructor invoked.
Serialization done.
DeSerialization done. Lion: roar
</pre>
<p>Above class just uses the standard mechanism and demonstrates serialization using Lion class.</p>
<h2>Customize Java Serialization</h2>
<p>We know Serializable is a <a title="What is a java marker interface?" href="http://javapapers.com/core-java/abstract-and-interface-core-java-2/what-is-a-java-marker-interface/">java marker interface</a>. When a class implements Serializable interface it gives information to the <a title="Differentiate JVM JRE JDK JIT" href="http://javapapers.com/core-java/differentiate-jvm-jre-jdk-jit/">JVM </a>that the instances of these classes can be serialized. Along with that, there is a special note to the JVM</p>
<blockquote><p>look for following two methods in the class that implements Serializable. If found invoke that and continue with serialization process else directly follow the standard serialization protocol.</p></blockquote>
<p>So this gives us a chance to write these two methods insided the Class that implements Serializable and you get a hook to the serialization process. You can write your custom code inside these two methods and customize the standard behaviour of serialization.</p>
<ul>
<li>private void writeObject(ObjectOutputStream out) throws IOException;</li>
<li>private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;</li>
</ul>
<p>I have modified the Lion class to include these two methods and on the fly I change a property of Lion to demonstrate this. This is not <a title="Overloading vs Overriding" href="http://javapapers.com/core-java/overloading-and-overriding/">overriding or overloading methods</a> and this is a mechanism provided by serialization. These two included methods are declared private but JVM can <a title="Access Modifiers In Java" href="http://javapapers.com/core-java/access-modifiers-in-java-explain/">access the private</a> methods of an object. There is no change to the class that does the serialization and de-serialization.</p>
<pre class="brush: java;">
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Lion implements Serializable {
  private String sound;
  public Lion() {
    System.out.println(&quot;Lion's constructor invoked.&quot;);
    setSound(&quot;roar&quot;);
  }
  public String getSound(){
    return sound;
  }
  public void setSound(String sound){
    this.sound = sound;
  }

  private void writeObject(ObjectOutputStream out) throws IOException {
    setSound(&quot;meow&quot;);
    out.defaultWriteObject();
  }

  private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
  }
}
</pre>
<p>Output:</p>
<pre class="brush: plain;">
Lion's constructor invoked.
Serialization done.
DeSerialization done. Lion: meow
</pre>
]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/core-java/customize-default-serialization-protocol/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

