<?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>Nils Preusker &#187; java</title>
	<atom:link href="http://www.nilspreusker.de/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nilspreusker.de</link>
	<description>Pragmatic Technologist</description>
	<lastBuildDate>Tue, 23 Mar 2010 10:44:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Serializable and serialVersionUID</title>
		<link>http://www.nilspreusker.de/2010/01/14/java-serializable-and-serialversionuid/</link>
		<comments>http://www.nilspreusker.de/2010/01/14/java-serializable-and-serialversionuid/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 12:32:13 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=317</guid>
		<description><![CDATA[Here&#8217;s a question:
Is a serialVerionUID required on abstract classes?
My first thought: No way! If the serialVersionUID is used by the serialization mechanism to check whether some serialized data is compatible with a serializable class, abstract classes (that will never be instantiated, and thus, never be serialized directly) don&#8217;t need one! The problem is that the [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a question:</p>
<blockquote><p>Is a serialVerionUID required on abstract classes?</p></blockquote>
<p>My first thought: No way! If the serialVersionUID is used by the serialization mechanism to check whether some serialized data is compatible with a serializable class, abstract classes (that will never be instantiated, and thus, never be serialized directly) don&#8217;t need one! The problem is that the entire inheritance tree is checked when a class is de-serialized — and that includes abstract base classes. A simple example illustrates the problem:</p>
<p>If we have an abstract base class &#8230;</p>
<pre>import java.io.Serializable;

public abstract class Base
implements Serializable {}</pre>
<p>&#8230; and a sub-class that declares a serialVersionUID &#8230;</p>
<pre>public class Sub extends Base {
    private static final long serialVersionUID =
      -7913545780181427623L;
}</pre>
<p>&#8230; and we create an instance of the sub-class and serialize it to a file &#8230;</p>
<pre>...
Sub sub1 = new Sub();
try {
    FileOutputStream fout = new FileOutputStream("sub.dat");
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(sub1);
    oos.close();
} catch (Exception e) {
    e.printStackTrace();
}</pre>
<p>&#8230; and then we change the base class by e.g. adding an int field &#8230;</p>
<pre>import java.io.Serializable;

public abstract class Base
implements Serializable {
    int a = 0;
}</pre>
<p>&#8230; and try to de-serialize the sub-class &#8230;</p>
<pre>Sub sub2 = null;
// Load the file and de-serialize the object
try {
    FileInputStream fin = new FileInputStream("sub.dat");
    ObjectInputStream ois = new ObjectInputStream(fin);
    sub2 = (Sub) ois.readObject();
    ois.close();
} catch (Exception e) {
    e.printStackTrace();
}</pre>
<p>&#8230; we&#8217;ll get a &#8220;java.io.InvalidClassException&#8221;.</p>
<p>If we do the same thing, but define a serialVersionUID in the abstract base class, the exception would not occur. The reason for this is, that if a serialVersionUID is not explicitly declared, the JVM will calculate a default one, &#8220;based on various aspects of the class&#8221; [1]. This is a great mechanism, at first glance it even seems to be better than creating the id manually — if the class changes, these changes are automatically reflected in the generated id. But, this mechanism can differ based on the VM If you would run this example on one VM and then try to de-serialize the same &#8220;sub.dat&#8221; file with a different VM, without modifying the base class, it is possible that you would get an &#8220;InvalidClassException&#8221;, simply because the VM uses a different mechanism to calculate the default serialVersionUID. This is why it is recommended to manually declare the serialVersionUID.</p>
<p>However, this also means that, if we change a serializable class and thereby make it incompatible with previously serialized instances, we have to update the serialVersionUID!</p>
<p>Now if we think about software maintenance and data integrity, there should be a way to migrate existing serialized data to new versions of the serializable class. I&#8217;d be interested to know if there is an official recommendation for this, but didn&#8217;t investigate further yet.</p>
<p>It would also be interesting to see how OR-mapping frameworks fit into the picture. There is an interesting thread on using Serializable and what to do with the serialVersionUID in the hibernate JIRA: <a title="Hibernate JIRA issue" href="http://opensource.atlassian.com/projects/hibernate/browse/HBX-964" target="_blank">http://opensource.atlassian.com/projects/hibernate/browse/HBX-964</a></p>
<p>I&#8217;d be interested to know what others think about this, so please feel free to comment!</p>
<p>[1] <a title="Java 6 API Docs - Serializable" href="http://java.sun.com/javase/6/docs/api/java/io/Serializable.html" target="_blank">http://java.sun.com/javase/6/docs/api/java/io/Serializable.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2010/01/14/java-serializable-and-serialversionuid/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Eclipse on Snow Leopard (10.6.2)</title>
		<link>http://www.nilspreusker.de/2009/11/24/eclipse-on-snow-leopard-10-6-2/</link>
		<comments>http://www.nilspreusker.de/2009/11/24/eclipse-on-snow-leopard-10-6-2/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 08:22:57 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[eclipse]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=300</guid>
		<description><![CDATA[When I downloaded and installed Eclipse (eclipse-SDK-3.5.1-macosx-carbon.tar.gz) yesterday, I started to get the following error message:
To fix this, you need to go to &#8220;Preferences &#62; General &#62; Capabilities&#8221; and check &#8220;Classic Update&#8221;. Afterwards you should be able to select &#8220;Help &#62; Software Updates &#62; Find and Install&#8230;&#8221;.
I wasn&#8217;t able to find much on the internet, [...]]]></description>
			<content:encoded><![CDATA[<p>When I downloaded and installed Eclipse (eclipse-SDK-3.5.1-macosx-carbon.tar.gz) yesterday, I started to get the following error message:</p>
<div id="attachment_302" class="wp-caption alignnone" style="width: 423px"><img class="size-full wp-image-302" title="eclipse-update-error" src="http://www.nilspreusker.de/wp-content/uploads/2009/11/eclipse-update-error.png" alt="eclipse-update-error" width="413" height="150" /><p class="wp-caption-text">Eclipse error message: &quot;Cannot complete the request. This installation has not been configured properly for software updates!&quot;</p></div>
<p>To fix this, you need to go to &#8220;Preferences &gt; General &gt; Capabilities&#8221; and check &#8220;Classic Update&#8221;. Afterwards you should be able to select &#8220;Help &gt; Software Updates &gt; Find and Install&#8230;&#8221;.</p>
<p>I wasn&#8217;t able to find much on the internet, but <a href="https://bugs.launchpad.net/ubuntu/+source/eclipse/+bug/438414/comments/18" target="_blank">this post on an Ubuntu forum</a> finally brought the solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2009/11/24/eclipse-on-snow-leopard-10-6-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Maven Dependency Conflicts</title>
		<link>http://www.nilspreusker.de/2009/09/24/maven-dependency-conflicts/</link>
		<comments>http://www.nilspreusker.de/2009/09/24/maven-dependency-conflicts/#comments</comments>
		<pubDate>Thu, 24 Sep 2009 14:05:50 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[mvn]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=270</guid>
		<description><![CDATA[Have you ever seen an exception like this:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'XY' defined in class path resource
[applicationContext.xml]: Instantiation of bean failed; nested
exception is org.springframework.beans.
BeanInstantiationException: Could not instantiate bean class
[XY]: Constructor threw exception; nested exception is
java.lang.LinkageError: You are trying to run JAXB 2.0 runtime
(from jar:file:/.../WEB-INF/lib/jaxb-impl-2.1.8.jar!/com/sun/
xml/bind/v2/model/impl/ModelBuilder.class)but you have old
JAXB 1.0 runtime earlier in the classpath (at [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever seen an exception like this:</p>
<pre>org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'XY' defined in class path resource
[applicationContext.xml]: Instantiation of bean failed; nested
exception is org.springframework.beans.
BeanInstantiationException: Could not instantiate bean class
[XY]: Constructor threw exception; nested exception is
java.lang.LinkageError: You are trying to run JAXB 2.0 runtime
(from jar:file:/.../WEB-INF/lib/jaxb-impl-2.1.8.jar!/com/sun/
xml/bind/v2/model/impl/ModelBuilder.class)but you have old
JAXB 1.0 runtime earlier in the classpath (at jar:file:
/.../WEB-INF/lib/jaxb-impl-1.0.4.jar!/com/sun/xml/bind/
WhiteSpaceProcessor.class) Please remove the JAXB 1.0
runtime for 2.0 runtime to work correctly.</pre>
<p>Well, I have, several times&#8230; and the task of having to figure out which library causes this dependency conflict seemed <span style="text-decoration: line-through;">unresolvable</span> pretty scary at first! Luckily, there is the m2eclipse plug-in with its excellent dependency graph. So if you are using Eclipse, whether you are actually using m2eclipse to manage your project or not, just the dependency graph makes it worth having a look at it. I still run my maven tasks on the command line, but have m2eclipse installed, just to be able to use the graph.</p>
<p><img title="Dependency Graph" src="http://www.nilspreusker.de/wp-content/uploads/2009/09/dep-graph.png" alt="Dependency Graph" /></p>
<p>After identifying which library is causing the dependency conflict, all you have to do is to add an &#8220;exclude&#8221; node to that dependency in your pom.xml. In the above case, this snipped did the trick:</p>
<pre>&lt;dependency&gt;
    &lt;groupId&gt;...&lt;/groupId&gt;
    &lt;artifactId&gt;...&lt;/artifactId&gt;
    &lt;version&gt;...&lt;/version&gt;
    <strong>&lt;exclusions&gt;
        &lt;exclusion&gt;
</strong><strong>            &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
            &lt;groupId&gt;javax.xml&lt;/groupId&gt;
        &lt;/exclusion&gt;
        ...
    &lt;/exclusions&gt;</strong>
&lt;/dependency&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2009/09/24/maven-dependency-conflicts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java and Snow Leopard</title>
		<link>http://www.nilspreusker.de/2009/09/22/java-and-snow-leopard/</link>
		<comments>http://www.nilspreusker.de/2009/09/22/java-and-snow-leopard/#comments</comments>
		<pubDate>Tue, 22 Sep 2009 11:38:46 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=259</guid>
		<description><![CDATA[I noticed that there isn&#8217;t a whole lot of useful information about Java and Snow Leopard (OS X 10.6) out there on the web. Maybe this is because there isn&#8217;t a whole lot to say about it. Snow Leopard comes with Java 6 (1.6.0_15 that is) only, which means that the links that still exist [...]]]></description>
			<content:encoded><![CDATA[<p>I noticed that there isn&#8217;t a whole lot of useful information about Java and Snow Leopard (OS X 10.6) out there on the web. Maybe this is because there isn&#8217;t a whole lot to say about it. Snow Leopard comes with Java 6 (1.6.0_15 that is) <span style="text-decoration: underline;"><strong>only</strong></span>, which means that the links that still exist in the</p>
<pre>/System/Library/Frameworks/JavaVM.framework/Versions/</pre>
<p>directory all point to &#8220;CurrentJDK&#8221;, which points to &#8220;1.6&#8243;. The thing is that Snow Leopard ships with a 32 bit version and a 64 bit version of the VM (Virtual Machine), 64 bit being the default one. The Java Preferences app shows this nicely:</p>
<p><img src="http://www.nilspreusker.de/wp-content/uploads/2009/09/java-preferences.png" alt="Java Preferences Application" /></p>
<p>And that is the big news about it, since a 32 bit version of java 6 didn&#8217;t previously exist for OS X.</p>
<p>Now I&#8217;ve previously had to tweak Eclipse to use the 1.5 VM (see &#8220;<a title="Java Versions on Mac OS X" href="http://www.nilspreusker.de/2009/03/03/java-versions-on-mac-os-x/" target="_self">Java Versions on Mac OS X</a>&#8220;), and now it just works. I&#8217;m assuming that this is because of the &#8220;mixed mode&#8221;:</p>
<pre>$ java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03-219)
Java HotSpot(TM) 64-Bit Server VM (build 14.1-b02-90, mixed mode)</pre>
<p>&#8230;so the VM would automatically detect if an application should be executed in 64 or 32 bit mode and then start the correct VM. I still need to investigate how this actually works. Anyway, the good news is that java and eclipse seems to be working better with Snow Leopard. I&#8217;ll write more on this as I continue working with it, for now here is an interesting post on the topic:</p>
<p><a title="Eclipse, Java and Snow Leopard" href="http://blog.zvikico.com/2009/09/eclipse-java-and-snow-leopard.html" target="_blank">http://blog.zvikico.com/2009/09/eclipse-java-and-snow-leopard.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2009/09/22/java-and-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Different Maven Versions on OS X</title>
		<link>http://www.nilspreusker.de/2009/04/24/different-maven-versions-on-os-x/</link>
		<comments>http://www.nilspreusker.de/2009/04/24/different-maven-versions-on-os-x/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 14:06:08 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=246</guid>
		<description><![CDATA[I keep running into the problem that different projects I&#8217;m working on require different versions of maven. I have two versions installed, maven 2.0.6 in /usr/share/maven-2.0.6 and maven 2.0.9 in /usr/share/maven. To find out which version of maven you are running, open a terminal and type the following
$ mvn --version
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: [...]]]></description>
			<content:encoded><![CDATA[<p>I keep running into the problem that different projects I&#8217;m working on require different versions of maven. I have two versions installed, maven 2.0.6 in /usr/share/maven-2.0.6 and maven 2.0.9 in /usr/share/maven. To find out which version of maven you are running, open a terminal and type the following</p>
<pre>$ mvn --version
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: "mac os x" version: "10.5.6" arch: "x86_64" Family: "mac"</pre>
<p>In this case, maven 2.0.9 is running, so we&#8217;ll see how to switch to maven 2.0.6. First we need to know where the maven executable, or in this case, the symbolic link to the executable is located:</p>
<pre>$ which mvn
/usr/bin/mvn</pre>
<p>Next, we&#8217;ll use ls to check where the symbolic link is pointing:</p>
<pre>$ ls -l /usr/bin/mvn
lrwxr-xr-x  1 root  wheel  24 Apr 24 14:26 /usr/bin/mvn -&gt; /usr/share/maven/bin/mvn</pre>
<p>Finally, if you have different maven versions installed, you can switch between them by overwriting the symbolic link:</p>
<pre>$ sudo ln -fhsv /usr/share/maven-2.0.6/bin/mvn /usr/bin/mvn</pre>
<p>After providing your password, the symbolic link should now be pointing to the maven 2.0.6 executable. To doublecheck, type</p>
<pre>$ mvn --version</pre>
<p>The output should be something like this</p>
<pre>Maven version: 2.0.6</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2009/04/24/different-maven-versions-on-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Versions on Mac OS X</title>
		<link>http://www.nilspreusker.de/2009/03/03/java-versions-on-mac-os-x/</link>
		<comments>http://www.nilspreusker.de/2009/03/03/java-versions-on-mac-os-x/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 15:20:56 +0000</pubDate>
		<dc:creator>nils</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.nilspreusker.de/?p=206</guid>
		<description><![CDATA[Update: This post refers to OS X 10.5 &#8220;Leopard&#8221;, if you are using 10.6 &#8220;Snow Leopard&#8221;, you might want to consider reading my post &#8220;Java and Snow Leopard&#8221; instead.

I just started using my Mac as a development machine and thought I&#8217;d share some of the issues and solutions I came across. I&#8217;m using Java 6, [...]]]></description>
			<content:encoded><![CDATA[<p><em>Update: This post refers to OS X 10.5 &#8220;Leopard&#8221;, if you are using 10.6 &#8220;Snow Leopard&#8221;, you might want to consider reading my post &#8220;<a title="Java and Snow Leopard" href="http://www.nilspreusker.de/2009/09/22/java-and-snow-leopard/" target="_self">Java and Snow Leopard</a>&#8221; instead.<br />
</em></p>
<p>I just started using my Mac as a development machine and thought I&#8217;d share some of the issues and solutions I came across. I&#8217;m using Java 6, Tomcat 6, Eclipse 3.3.2 (Europa), hibernate 3, Spring 2.0.3 and tapestry 1.4.5.</p>
<p>The first issue I came across was an</p>
<pre>UnsupportedClassVersionError: Bad version number in .class file</pre>
<p>error when I tried to start tomcat. (I&#8217;m using the sysdeo plug-in to control tomcat, even though I&#8217;m aware that WTP is much better&#8230; I have my reasons, but that&#8217;s beyond the scope of this post.)</p>
<p>Now the title of the console in Eclipse revealed the problem, the Sysdeo plug-in was using version 1.4 of the Java RE. The thing is, I didn&#8217;t notice this until I tried a whole bunch of other stuff&#8230; which cost me quite a bit of time, but also helped understand a few things better.</p>
<p>OS X keeps the different Java versions neatly separated in the</p>
<pre>/System/Library/Frameworks/JavaVM.framework/Versions</pre>
<p>directory. By creating or overwriting the symbolic link &#8220;CurrentJDK&#8221;, you can change the default JDK version of your system. To do this, you have to change to the Versions directory and set a new symlink with ln:</p>
<pre>$cd /System/Library/Frameworks/JavaVM.framework/Versions
$sudo ln -fhsv 1.6 CurrentJDK</pre>
<p>After providing the administrator password, the default JDK will be set to version 1.6. Unfortunately, after doing this, I couldn&#8217;t start Eclipse anymore. The fix for this is to edit the following file:</p>
<pre>/Applications/eclipse/Eclipse.app/Contents/Info.plist</pre>
<p>and un-comment the following line:</p>
<pre>&lt;string&gt;-vm&lt;/string&gt;&lt;string&gt;/System/Library/Frameworks/
JavaVM.framework/Versions/1.5.0/Commands/java&lt;/string&gt;</pre>
<p>This tells Eclipse to use a specific Java version, instead of the platforms default one. Now I was able to start Eclipse again and tomcat started without exceptions when I manually deployed my war files. My next step was to try Sysdeo to start Tomcat, which is where I realized that all I would have had to do was to set the correct JVM in Preferences &gt; Tomcat &gt; JVM Settings. Anyway, learned something on the way <img src='http://www.nilspreusker.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here are some links that helped me:</p>
<p><a href="http://blog.kischuk.com/2008/05/08/running-eclipse-on-macbooks-with-java-6/" target="_blank">http://blog.kischuk.com/2008/05/08/running-eclipse-on-macbooks-with-java-6/</a></p>
<p><a href="http://www.insanelymac.com/forum/index.php?showtopic=58817&amp;st=0&amp;p=435204&amp;#entry435204" target="_blank">http://www.insanelymac.com/forum/index.php?showtopic=58817&amp;st=0&amp;p=435204&amp;#entry435204</a></p>
<p><a href="http://johnnywey.wordpress.com/2008/06/21/os-x-and-java-unsupportedclassversionerror/" target="_blank">http://johnnywey.wordpress.com/2008/06/21/os-x-and-java-unsupportedclassversionerror/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nilspreusker.de/2009/03/03/java-versions-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
