<?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>crossedstreams.com &#187; Java</title>
	<atom:link href="http://blog.crossedstreams.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crossedstreams.com</link>
	<description>Total protonic reversal!</description>
	<lastBuildDate>Sat, 21 Jan 2012 20:10:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Studying for SCJP?</title>
		<link>http://blog.crossedstreams.com/development/studying-for-scjp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=studying-for-scjp</link>
		<comments>http://blog.crossedstreams.com/development/studying-for-scjp/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 19:16:45 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=640</guid>
		<description><![CDATA[I was recently quizzed about prep for the Sun Certified Java Programmer exam, so here&#8217;s a quick rundown of the resources I found useful and tips I picked up when I was studying for SCJP 5. All in, it cost me around £200 and took just under four months. I can recommend the following resources: [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently quizzed about prep for the Sun Certified Java Programmer exam, so here&#8217;s a quick rundown of the resources I found useful and tips I picked up when I was studying for SCJP 5. All in, it cost me around £200 and took just under four months. I can recommend the following resources:</p>
<ul>
<li><a href="http://www.amazon.co.uk/Certified-Programmer-Study-310-055-Certification/dp/0072253606">SCJP 5 Study Guide</a> by Kathy Sierra and Bert Bates. Choose the book for the certification version you&#8217;re going for. Work through every chapter and make sure you understand every concept and gain some familiarity with the APIs. I found flash-cards (little cards, questions on one side, answers on the other) invaluable for really burning some of the more difficult to remember stuff. As I didn&#8217;t have a programming or Computer Science background, I learnt more about Object-Oriented concepts and programming in the first three chapters of this book than I did from anywhere else.</li>
<li>javaranch.com <a href="http://faq.javaranch.com/java/ScjpFaq">SCJP FAQ</a> for the latest thoughts info on certification, and the <a href="http://www.coderanch.com/forums/f-24/Programmer-Certification-SCJP">SCJP Certification Forum</a>, in which you&#8217;ll start by asking questions and wind up answering the questions of others as you get lined up for the exam. The FAQ is also a good place to check what the recent exam takers are saying about the exam and the study materials. You can also use the Rules Roundups to get a feel for answering questions, although they&#8217;re pretty easy compared to the exam.</li>
<li>Mock exams. When you think you&#8217;re nearly ready for the exam, grab a pack of mock tests (I used <a href="http://www.whizlabs.com/scjp/scjp.html">whizlabs</a>). I certainly wasn&#8217;t as ready as I thought I was! Do them properly &#8211; set aside at least an hour and try and work as if under exam conditions &#8211; you also get used the format of the exams and there&#8217;s a few tricky corner cases in the mock exams I did that are worth knowing about.</li>
<li><a href="http://www.prometric.com/default.htm">Sign up for your exam at prometric.com</a>. I signed up for the exam as soon as I&#8217;d finished reading the Study Guide, because I&#8217;m fundamentally lazy and needed the impending deadline! If you do the same, you might want to give yourself a month or two between booking and exam day to do those mocks and deal with any gaps in your knowledge they throw up.</li>
</ul>
<p>I&#8217;ve also got a couple of tips on techniques to use for answering some of the tricker exam questions &#8211; that can also come in useful when writing and debugging code.</p>
<p>First up, the exam will often have a code sample and ask you to choose from a number of options. &#8216;Does not Compile&#8217; is almost always an option, and it kept catching me out at first. The way I dealt with it was to run through the sample twice &#8211; first as the compiler, looking for those errors that would prevent the code compiling in the first place. Only if that first check didn&#8217;t throw up any problems do you need to imagine the behaviour of the code as it runs and look for exceptions and results.</p>
<p>Second, come up with some way of sketching the changing references to objects as you work through a code sample. This&#8217;ll help you catch subtle re-assignments you might miss working it through mentally.</p>
<p>It&#8217;ll take up your time. I was probably spending about 5-10 hours a week learning, revising and writing code for those four months &#8211; that counts when you&#8217;re working full time. It&#8217;s worth it though, particularly if you&#8217;re an inexperienced Java programmer. It&#8217;ll make you aware of some of the principles and practises you should be thinking about and it&#8217;ll give you something of a guided tour of the core Java APIs.</p>
<p>If you going to go for this cert, good luck!</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/studying-for-scjp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When Eclipse Plugins Don&#8217;t</title>
		<link>http://blog.crossedstreams.com/development/when-eclipse-plugins-dont/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=when-eclipse-plugins-dont</link>
		<comments>http://blog.crossedstreams.com/development/when-eclipse-plugins-dont/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 21:55:29 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=520</guid>
		<description><![CDATA[Quick Version If, after &#8216;installing&#8217; a plugin for Eclipse 3.4 it&#8217;s not visible in Preferences or in context menus, check that your user account has permissions to write into the appropriate Eclipse directories &#8211; because Eclipse doesn&#8217;t warn you. Preamble For those who don&#8217;t know, Eclipse is an IDE heavily used for Java development. Like [...]]]></description>
			<content:encoded><![CDATA[<h3>Quick Version</h3>
<p>If, after &#8216;installing&#8217; a plugin for Eclipse 3.4 it&#8217;s not visible in Preferences or in context menus, check that your user account has permissions to write into the appropriate Eclipse directories &#8211; because Eclipse doesn&#8217;t warn you.</p>
<h3>Preamble</h3>
<p>For those who don&#8217;t know, <a href="http://www.eclipse.org/">Eclipse</a> is an IDE heavily used for Java development. Like anything else, it&#8217;s a delicate balance of awesome and suckage, and today I got caught out by a pinch of suckage.</p>
<p>It&#8217;s a plugin-based platform, which means that anyone can write a piece of software that &#8216;plugs-in&#8217; to Eclipse to extend or enhance the functionality. It&#8217;s a great approach, allowing me to tailor my own installation to my needs. Having recently re-installed Ubuntu Linux, earlier today I was trying to set up my plugins without success.</p>
<h3>The Problem</h3>
<p>I set up the <a href="http://m2eclipse.sonatype.org/">m2eclipse</a> update site as described in the documentation (pretty standard fare&#8230; Help &gt; Install New Software&#8230;, paste in the update site URL and choose components to install) but after &#8216;installation&#8217; was completed there was no sign of the plugin. There should have been new options appearing all over Eclipse, but no. Tumbleweed.</p>
<p>Tried installing the next plugin on my list&#8230; <a href="http://subclipse.tigris.org/">Subclipse</a>. Same story. So not a problem with a plugin&#8230;</p>
<h3>The Solution</h3>
<p>&#8230;and then it dawned on me. I&#8217;d installed Eclipse into /opt, which is all locked down to root, with administrative privileges. Schoolboy error &#8211; no permissions for the plugin files to actually be installed under the authority of my user account, which is the context that Eclipse normally runs.</p>
<p>A quick chown to root:admin for the /opt/eclipse directory (rather heavy handed &#8211; I should probably have worked out which directories I needed write access to, but I was out of patience), &#8216;uninstalled&#8217; the plugin in the Eclipse, tried the install again and then I was back in business.</p>
<p>I&#8217;m really surprised that Eclipse doesn&#8217;t complain when it&#8217;s &#8216;installing&#8217; a plugin in directories to which it has no access. I may not be the sharpest tool in the box, but I&#8217;m fairly sure that permissions problems happen to other people too. I might go so far as to find out where you feed back to the Eclipse development community for once. Anyways, this post will help remind me next time I get this wrong, it might help someone out, and if nothing else you might be mildly entertained by my ineptitude.</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/when-eclipse-plugins-dont/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Perl is slower and faster than Java</title>
		<link>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl-is-slower-and-faster-than-java</link>
		<comments>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 23:39:52 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PERL]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=104</guid>
		<description><![CDATA[Bit of a random one coming up&#8230; I needed to get an measure of the difference in performance between Perl and Java for a simple client application, so I wrote the traditional &#8216;Hello World&#8217; app in both and ran a bunch of executions averaging over the time from start to end of execution. The net [...]]]></description>
			<content:encoded><![CDATA[<p>Bit of a random one coming up&#8230;</p>
<p>I needed to get an measure of the difference in performance between Perl and Java for a simple client application, so I wrote the traditional &#8216;Hello World&#8217; app in both and ran a bunch of executions averaging over the time from start to end of execution. The net result: </p>
<p><strong>Perl is around 34 times faster than Java.</strong></p>
<p>Really? I thought Java was supposed to be fast? In fact, Dhananjay Nene talked about how comparatively fast a selection of languages &#8211; including Java &#8211; were <a href="http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/">on his blog</a>, and cwilbur &#8216;s comments suggest that in that experiment:</p>
<p><strong>Java is around 100 times faster than Perl.</strong></p>
<p>So how can these two conflicting results both be true? I&#8217;m sure you&#8217;ve already figured it out, but I&#8217;m going to tell you anyway.</p>
<p>Java code takes hundreds of milliseconds to start up, because Java code runs in the Java Virtual Machine which needs a little time to get itself ready before your code can run. Once it&#8217;s up, however, you can get performance close to (or even better than, in reality) that of typical C++.</p>
<p>On the other hand, Perl doesn&#8217;t need that environment and so can start faster but trades off more overhead at runtime, meaning slower running performance.</p>
<p>Just goes to show, application performance is another area where there&#8217;s no one right answer &#8211; it&#8217;s about choosing the right tool for the job &#8211; I don&#8217;t think there&#8217;s any way round these performance characteristics without <a href="http://martiansoftware.com/nailgun/index.html">cheating</a>.</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/perl-is-slower-and-faster-than-java/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Why Java?</title>
		<link>http://blog.crossedstreams.com/development/why-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=why-java</link>
		<comments>http://blog.crossedstreams.com/development/why-java/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 17:59:10 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=222</guid>
		<description><![CDATA[I was recently asked, why Java? It&#8217;s a great question &#8211; exactly why do I choose to learn the Java language? I gave it some thought and I&#8217;ll share what I considered. I&#8217;m certainly not saying that what follows is a justification of the Java language in any general context, nor am I any kind [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently asked, why Java? It&#8217;s a great question &#8211; exactly why do I choose to learn the Java language?</p>
<p>I gave it some thought and I&#8217;ll share what I considered. I&#8217;m certainly not saying that what follows is a justification of the Java language in any general context, nor am I any kind of expert. This is just my view, given my circumstances.</p>
<h3>It&#8217;s Free!</h3>
<p>If you want to learn a language you don&#8217;t want to be laying out pots of cash up front. It&#8217;s likely you&#8217;ve got a Java Virtual Machine on your computer right now, and obtaining a development kit to get you started writing software is <a href="http://java.sun.com/javase/downloads/index.jsp">free and straightforward</a>. Win.</p>
<h3>The Java Virtual Machine</h3>
<p>You never know when you might need to run that little app you wrote on a Windows host, or a different flavour of UNIX, or even <a href="http://www-03.ibm.com/servers/eserver/zseries/software/java/">a big ol&#8217; IBM mainframe system</a> &#8211; it&#8217;s nice to have some confidence that it&#8217;s just going to work.</p>
<p>Now, the JVM runs on any platform I can think of. Perhaps in the early days, there was some divergence in different platform implementations of Java (having felt the pain of the slight non-standard nature of the Micro$oft JVM in IE some time ago!) which somewhat hampered the cross-platform claim, these days there&#8217;s a <a href="http://en.wikipedia.org/wiki/Technology_Compatibility_Kit">suite of compliance tests</a> that a JVM implementation must pass to brand itself Java compatible. That means that I don&#8217;t have to worry too much about cross-platform compatibility.</p>
<p>The JVM supports more than just the Java language. Jython, JRuby, Scala, JavaFX, Groovy, Fortress, Clojure&#8230; the list is getting ever longer, and there&#8217;s even a <a href="http://www.infoq.com/JVMLanguageSummit">JVM Languages Summit</a>. So, if you&#8217;re using the Java language and find a problem that better solved in another language, it&#8217;s perhaps not such a huge leap to get your Java code and your new JRuby code working together. Tim Bray wrote up some nice notes on the language summit <a href="http://www.tbray.org/ongoing/When/200x/2008/09/25/JVM-Summit">here</a> if you&#8217;re looking for a little more.</p>
<p>The other trick about the JVM is that your code gets better without you changing it. As new JVMs are released, they include the latest, hottest optimizations that take the software you write and give it go-faster stripes.</p>
<h3>Tools and Platforms</h3>
<p>Tool support for the Java language is extensive enough that there is generally a choice &#8211; for example, there is a choice of many Integrated Development Environments (<a href="http://www.eclipse.org/">Eclipse</a>, <a href="http://netbeans.org/">NetBeans</a>, <a href="http://www.jetbrains.com/idea/">IntelliJ IDEA</a> to name but three) in which to write your software.</p>
<p>If you&#8217;re building &#8216;Enterprise&#8217; software (whatever that really means!) you have a choice of application servers (<a href="http://www-01.ibm.com/software/webservers/appserv/was/">WebSphere</a>, <a href="http://www.jboss.org/">JBoss</a>, <a href="http://www.mortbay.org/jetty/">Jetty</a>, <a href="http://tomcat.apache.org/">Tomcat</a>, <a href="https://glassfish.dev.java.net/">GlassFish</a>&#8230;) that all implement the Java Enterprise Edition specification in whole or in part (for example, the open source Tomcat application server supports a stripped-down subset of the spec) you know that any of the application servers should run your software.</p>
<p>With all these things the choices mean that I can choose the implementation with the right strengths and at the right price point for the project in hand.</p>
<h3>Learning</h3>
<p>Java&#8217;s got a lot going for it as a learning language &#8211; it seems to be on the syllabus for most computer science degrees. There&#8217;s a wealth of online material available for free, including <a href="http://java.sun.com/docs/books/tutorial/java/index.html">Sun&#8217;s own learning trails</a> and <a href="http://www.javapassion.com/">Sang Shin&#8217;s excellent javapassion.com</a> learning site.</p>
<p>I know that the question of value in professional certification seems to polarize opinion, but there certainly is a <a href="http://www.sun.com/training/certification/java/scjp.xml">certification trail</a> in the Java language that&#8217;s not trivial to achieve.</p>
<p>The cost of achieving these certifications is financially quite insignificant, but (certainly for me &#8211; I&#8217;m sure there&#8217;s folks that find this stuff easier) demands a significant amount of time and commitment. The objectives and exam questions are put together by <a title="Bert Bates' response on javaranch" href="http://www.coderanch.com/t/471428/Programmer-Certification-SCJP/certification/Who-writes-objectives-questions-SCJP#2111234">teams of Sun engineers, Java developers and Java instructors, orchestrated by Sun</a>. (Thanks to Bert Bates and javaranch.com for that info!)</p>
<p>Having done a couple of certs myself, I found them to be a very useful useful guided tour of the language and its extensions. I found plenty of useful features and techniques whilst studying that have since steered me clear of errors and wasted time.</p>
<h3>The JCP and Standards</h3>
<p>New APIs for the Java language happen through the JCP, or Java Community Process. Everyone from from individuals to the largest IT players (IBM, Cisco Systems, Nokia &#8211; the members are listed <a href="http://jcp.org/en/participation/members">here</a>) are involved, and new standards happen in a publicly visible process of proposal and review.</p>
<p>Don&#8217;t believe me? Here&#8217;s the <a href="http://java.sun.com/javaee/technologies/javaee6.jsp">latest Java Enterprise Edition spec.</a> See those JSR numbers? They&#8217;re the specifications that have been developed, reviewed and approved as part of the JCP.</p>
<h3>Libraries</h3>
<p>There&#8217;s sometimes so much choice of open source Java libraries, it&#8217;s hard to know what to choose for a given problem. The Apache Software Foundation hosts<a href="http://projects.apache.org/indexes/language.html#Java"> loads of open source Java projects</a>, as does <a href="http://code.google.com/hosting/search?q=label%3aJava">Google Code</a> and <a href="http://www.java.net/community">java.net</a>.</p>
<p>Not that every library is a piece of awesome&#8230; but many are. Having lots of choice increases the chances there&#8217;ll be something out there that&#8217;s already been built and tested and fits the bill. The libraries and APIs I have to use with my language of choice to get things done make a big impact to my productivity.</p>
<h3>What &#8211; No Discussion of Technical Stuff?</h3>
<p>Nope. I reckon it&#8217;s rather pointless to try and discuss differences between the capabilities of one language versus another. Anything computable can be computed in any Turing-complete language, which I think covers any language you might seriously approach as a general purpose problem solving platform.</p>
<p>So it&#8217;s not about what can or can&#8217;t be done &#8211; it&#8217;s really about what language I can be most effective and productive in. The biggest productivity killer in Java seems to be the boilerplate code necessary to do simple things, but over the last couple of years, strides forward have been made with <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html">annotations</a>, for example, to reduce the boilerplate problem.</p>
<h3>That&#8217;s All Folks</h3>
<p>That&#8217;s about it, really. There&#8217;s plenty of other languages that can lay claim to some of the points I&#8217;ve made, but few that can claim them all.</p>
<p>There are other languages I choose for specific jobs (JavaScript for client-side behaviour in web browsers, PERL for sysadmin-type scripts) but my day to day workhorse, and the focus of my learning attention, is Java. For now, at least.</p>
<p>If I&#8217;ve missed anything or you disagree, feel free to drop me a comment!</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/why-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When Disabling DNS Caching Doesn&#8217;t</title>
		<link>http://blog.crossedstreams.com/development/when-disabling-dns-caching-doesnt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=when-disabling-dns-caching-doesnt</link>
		<comments>http://blog.crossedstreams.com/development/when-disabling-dns-caching-doesnt/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 20:39:11 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=85</guid>
		<description><![CDATA[If you&#8217;re going to fiddle with networkaddress.cache.ttl, do it before you touch the network. I was using the java.net.InetAddress class to resolve IP addresses from DNS names the other day. I wanted to test my code by manipulating my local system&#8217;s &#8216;hosts&#8217; file, to quickly simulate moving IP addresses around behind a DNS name, so [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><strong>If you&#8217;re going to fiddle with networkaddress.cache.ttl, do it before you touch the network.</strong></p>
<p>I was using the <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetAddress.html">java.net.InetAddress</a> class to resolve IP addresses from DNS names the other day.</p>
<p>I wanted to test my code by manipulating my local system&#8217;s &#8216;hosts&#8217; file, to quickly simulate moving IP addresses around behind a DNS name, so I also set disabled caching the resolved addresses from one call to the next by programmatically setting the <strong>networkaddress.cache.ttl</strong> Security property to &#8217;0&#8242;. According to the javadocs,</p>
<blockquote><p>The value [assigned to networkaddress.cache.ttl] is specified as as integer to indicate  the number of seconds to cache the successful lookup.</p></blockquote>
<p>which is true, and everything was peachy &#8211; my hosts file changes were immediately picked up.</p>
<p>At first.</p>
<p>I continued to build my code and all of a sudden, the caching came back and the changes I was making in my hosts file were ignored.</p>
<p>By backtracking my changes, I worked out that the breaking change happened when I executed a network call which involved name resolution BEFORE I disabled the cache.</p>
<p>I moved the setting of the property to happen before I touched the network et voila &#8211; everything peachy again.</p>
<p>This (feature|problem|bug) is documented in a couple of places when you know what you&#8217;re looking for, like <a href="http://www.rgagnon.com/javadetails/java-0445.html">here</a>.</p>
<p>And here&#8217;s a simple code demo of it in action:</p>
<pre class="prettyprint">public static void main(String[] args) throws Exception {
  String name = "google.com";

  //lookup(name);

  Security.setProperty("networkaddress.cache.ttl", "0");
  while (true) {
    lookup("google.com");
    Thread.sleep(1000);
  }

  public static void lookup(String name) throws Exception {
    System.out.println(name + ":" + InetAddress.getByName(name).getHostAddress());
  }
}</pre>
<p>Running the sample above as-is:<br />
google.com:74.125.127.100<br />
google.com:74.125.67.100<br />
google.com:74.125.127.100<br />
google.com:74.125.67.100<br />
google.com:74.125.127.100</p>
<p>But uncomment the first call to lookup(), and<br />
google.com:74.125.45.100<br />
google.com:74.125.45.100<br />
google.com:74.125.45.100<br />
google.com:74.125.45.100<br />
google.com:74.125.45.100</p>
<p>Cheers,</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/when-disabling-dns-caching-doesnt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Apache MyFaces on Tomcat 5.5.x and below</title>
		<link>http://blog.crossedstreams.com/java/running-apache-myfaces-on-tomcat-55x-and-below/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=running-apache-myfaces-on-tomcat-55x-and-below</link>
		<comments>http://blog.crossedstreams.com/java/running-apache-myfaces-on-tomcat-55x-and-below/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 22:14:20 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[javaserver faces]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[maven]]></category>
		<category><![CDATA[myfaces]]></category>
		<category><![CDATA[tomcat 5.5.x]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=44</guid>
		<description><![CDATA[I&#8217;ve recently been looking at JavaServer Faces (specifically Apache&#8217;s MyFaces), playing about on my home Tomcat 5.5.27 installation, using Maven2 to do my dependency management, builds etc&#8230; &#8230;largely unsuccessfully. I just could not get the darned example app to start, with exceptions along the lines of: 20-Apr-2009 22:27:04 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context initialized [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently been looking at <a href="http://www.ibm.com/developerworks/java/library/j-jsf1/index.html?S_TACT=105AGX02&amp;S_CMP=EDU" title="IBM Article on JSF">JavaServer Faces</a> (specifically Apache&#8217;s <a href="http://myfaces.apache.org/" title="Apache MyFaces">MyFaces</a>), playing about on my home Tomcat 5.5.27 installation, using <a href="http://maven.apache.org/" title="Apache Maven">Maven2</a> to do my dependency management, builds etc&#8230;</p>
<p>&#8230;largely unsuccessfully. I just could <em>not</em> get the darned example app to start, with exceptions along the lines of:</p>
<blockquote><p> 20-Apr-2009 22:27:04 org.apache.catalina.core.StandardContext listenerStart<br />
SEVERE: Exception sending context initialized event to listener instance of class org.apache.myfaces.webapp.StartupServletContextListener<br />
<strong>java.lang.NoClassDefFoundError: javax/el/ExpressionFactory</strong><br />
at org.apache.myfaces.webapp.StartupServletContextListener.getFacesInitializer&#8230;</p></blockquote>
<p>appearing in the tomcat localhost log files.<br />
<span id="more-44"></span></p>
<p>Solving the problem is easy when you know how, but it still took me a while to figure it out. There&#8217;s two versions of JSF and MyFaces, and by default when you set up your Maven project, you get the latest, greatest version, 1.2.x &#8211; which is incompatible with Tomcat versions below version 6.0. That&#8217;s what causes the exceptions above.</p>
<p>If you want to use Tomcat 5.5x or below, you&#8217;ll need MyFaces 1.1.x to get things working. If you&#8217;re using Maven, update the versions of myfaces-api and myfaces-impl in your pom.xml to refer to the 1.1.6 versions as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.myfaces.core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myfaces-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.myfaces.core<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>myfaces-impl<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compile<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  ...more stuff</pre></div></div>

<p>When I made these updates, the older versions were downloaded, and everything started working just fine.</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/java/running-apache-myfaces-on-tomcat-55x-and-below/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Faster Java &#8211; Strings</title>
		<link>http://blog.crossedstreams.com/development/faster-java-strings/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=faster-java-strings</link>
		<comments>http://blog.crossedstreams.com/development/faster-java-strings/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 18:20:21 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[concatenation]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[optimisation]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[stringbuffer]]></category>
		<category><![CDATA[stringbuilder]]></category>

		<guid isPermaLink="false">http://blog.crossedstreams.com/?p=31</guid>
		<description><![CDATA[If you&#8217;re building a long String in Java, don&#8217;t stick String objects together using &#8216;+&#8217;, for example: String str = "Hello"; str = str + world; str = str + "!"; Why not? It&#8217;s really slow when you do it a lot! What should you do instead? Use the append(String) method in StringBuffer (Java 1.4.2 [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re building a long String in Java, don&#8217;t stick String objects together using &#8216;+&#8217;, for example:</p>
<pre class="prettyprint">
String str = "Hello";
str = str + world;
str = str + "!";</pre>
<p><strong>Why not?</strong></p>
<p>It&#8217;s really slow when you do it a lot!</p>
<p><strong>What should you do instead?</strong></p>
<p>Use the append(String) method in <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html" title="StringBuffer Java 1.4 API documentation">StringBuffer</a> (Java 1.4.2 on), or <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html" title="Java 5 StringBuilder API Documentation">StringBuilder</a> (Java 5 on). StringBuilder is slightly faster than StringBuffer, but is not thread safe. Both are much faster than concatenating String objects &#8211; by orders of magnitude. For example:</p>
<pre class="prettyprint">
StringBuffer str = new StringBuffer("Hello");
str.append(world);
str.append("!");</pre>
<p><span id="more-31"></span></p>
<p>I set up a simple test rig to concatenate ints onto a String in a loop, by simple &#8216;+&#8217; concatenation, then with a StringBuilder. Running a half dozen times and taking a rough average, the results are pretty conclusive &#8211; concatenating the wrong way can really slow things down.</p>
<ul>
<li>Over 10,000 repetitions, &#8216;+&#8217; takes around 650 times as long (670 ms vs. 1ms).</li>
<li>Over 20,000 reps, it takes around 1,800 times longer(3,500ms vs. 2ms).</li>
<li>Over 100,000 reps takes around <strong>41,000</strong> times longer! (over <strong>5 minutes</strong> vs. 8ms).</li>
</ul>
<p><strong>Why&#8217;s it so slow?</strong><br />
Strings in Java are <em>immutable</em>, which means that once a String is instantiated (be it by new String() or as a literal &#8220;blah&#8221;) it cannot be changed. When you concatenate one String to another, you actually create a third String which is the result of the concatenation</p>
<p>Not too much of an overhead, except when that concatenation is in some nested loop structure or the like and becomes a real bottleneck in your code. In that situation, you&#8217;re creating loads of objects that you&#8217;ll never use again and you&#8217;ll probably be running the Garbage Collector a lot to mop up, which makes things go even slower.</p>
<p>I first came across this when I got caught out building a big HTML table from database query results. When I was using String concatenation, it ran like a dog and the speed was pretty obviously dependent on the amount of concatenation going on. Read up on it, switched to StringBuilder, and the processing to put the String together went (in the worst case) from taking minutes to milliseconds. That&#8217;s the kind of performance improvement people notice.</p>
<p>There&#8217;s a little more to it, and if you&#8217;re interested, there&#8217;s more info and further optimisation steps <a href="http://www.precisejava.com/javaperf/j2se/StringAndStringBuffer.htm" title="PreciseJava String optimisation">here</a>. The SCJP 5 qualification also covers this and a load of other simple gotchas in Java SE.</p>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/faster-java-strings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How do I send files to the Browser?</title>
		<link>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser</link>
		<comments>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 22:05:09 +0000</pubDate>
		<dc:creator>Paul Brabban</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[PERL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[mp3 doc pdf xls content-disposition content-type http]]></category>

		<guid isPermaLink="false">http://crossedstreams.com/wordpress/?p=6</guid>
		<description><![CDATA[So you&#8217;ve coded up your new webapp. It does some cool stuff and then produces files in a binary format (like .pdf, .doc, .mp3) and you need to deliver these files to the browser. So how do you do that? Random Junk If you just &#8216;include&#8217; the file, or you read the file into your [...]]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;ve coded up your new webapp. It does some cool stuff and then produces files in a binary format (like .pdf, .doc, .mp3) and you need to deliver these files to the browser. So how do you do that?<span id="more-6"></span></p>
<h3>Random Junk</h3>
<p>If you just &#8216;include&#8217; the file, or you read the file into your script and then print it out to the browser, you just get a jumble of weird characters. The problem is that the file content has been encoded to be read by a particular application and when the browser tries to render it as text, it applies the wrong encoding &#8211; so you get the random characters.</p>
<p>Luckily, it&#8217;s pretty easy to get a browser to understand that the content you&#8217;re sending isn&#8217;t supposed to be rendered like HTML. You just have to let the browser know what kind of data you&#8217;re sending before you send it, and that&#8217;s pretty easy. Setting just one HTTP header, the &#8216;Content-type&#8217; header, is all that&#8217;s needed. Most languages include support for setting a header, like PHP:</p>
<pre class="prettyprint">
header("Content-type: application/pdf");</pre>
<p>Setting this header will make most browsers throw up a dialog with options to run the file with an application, or save the file.</p>
<h3>PHP Example</h3>
<p>Here&#8217;s an example you can run, if you have PHP to hand. You&#8217;ll need a pdf file to send, and you&#8217;ll need to change the filename and path if it&#8217;s not in the same location as the PHP file.</p>
<pre class="prettyprint">
### set a header to tell the browser what kind of file I'm about to send
header("Content-type: application/pdf");
### then read in the .pdf file
$pdfLocation = "my_pdf_file.pdf";
$pdf = file_get_contents($pdfLocation);
### and the file out to the browser
echo $pdf;</pre>
<p>All good so far, but you might be wondering about that &#8216;application/pdf&#8217; bit. It&#8217;s a MIME (Multipurpose Internet Mail Extension) type, a code that provides a key for the applications that will be able to handle encoded data. IANA, the organisation that manages the types provides a reference for all registered types <a href="http://www.iana.org/assignments/media-types/">here</a>.</p>
<h3>A Word on Encoding</h3>
<p>When sending the file content, you&#8217;ll need to make sure that there&#8217;s no character encoding applied, because the data you&#8217;re dealing with isn&#8217;t a set of characters. Swapping stuff that just happens to look like a Windows-style newline for a UNIX-style newline is going to mess things up too. In the examples the file reading and writing is going on in binary mode, or using input/output streams instead of the usually more convenient character based stuff &#8211; to preserve the data from the file exactly as it was in the file.</p>
<p>When the dialog appears to run or save the file, the suggested filename is the name of the PHP script doing the work, not the name of the file &#8211; so another HTTP header that can come in useful is Content-Disposition. Amongst other things, this header lets you specify a filename for the file to be saved &#8211; however &#8211; although I&#8217;ve not had any issues myself, but I did see a blog post on <a href="http://www.hanselman.com/blog/TheContentDispositionSagaControllingTheSuggestedFileNameInTheBrowsersSaveAsDialog.aspx">IE compatibility problems</a>.</p>
<pre class="prettyprint">
header("Content-Disposition: inline; filename=filename.pdf";);</pre>
<p>Well, hope that&#8217;s of some use to you. I&#8217;ll finish up by providing a couple of simple implementations in other languages. The examples should run in an appropriate web environment without any special support.</p>
<h3>PERL example</h3>
<p>No helper method to set the header here, so we have to manually print the header in the right format, including a following blank line.</p>
<pre class="prettyprint">
#!/path/to/perl
# set HTTP header to tell browser what type of data to expect
# in this case, a PDF file
print "Content-type: application/pdf\n\n";
# then open the pdf file
$pdfLocation = "my_pdf_file.pdf";
$pdfBuffer = open(PDF, "&lt;$pdfLocation");
# set to work in binary mode
binmode(PDF);
# read 64k chunks of the source file and write
# them to the output stream
while (read (PDF, $pdfBuffer, 65536)) {
    print $pdfBuffer;
}</pre>
<h3>Java Servlet Example</h3>
<p>It&#8217;s long-winded and untidy to do this in one Servlet class but hey.</p>
<pre class="prettyprint">
package com.crossedstreams.blog.post6;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.OutputStream;
/**
 * Servlet implementation class PdfServlet
 * reads a PDF file on the local filesystem and pushes it out to the browser
 */
public class PdfServlet extends HttpServlet {
    /**
     * Sends a PDF file in response to HTTP GET request
     */
    @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
        // set HTTP header to tell browser what type of data to expect
        // in this case, a PDF file
        response.setContentType("application/pdf");
        // and give the browser a filename to save as
        response.setHeader("Content-disposition", "inline; filename=filename.pdf");
        // then open the pdf file
        String pdfLocation = "/path/to/my_pdf_file.pdf";
        FileInputStream pdfFile = new FileInputStream(pdfLocation);
        // get the output stream back to the client
        OutputStream out = response.getOutputStream();
        byte[] pdfBytes = new byte[65536];
        try {
            // read 64k chunks of the source file and write
            // them to the output stream
            while (pdfFile.read(pdfBytes) != -1) {
                out.write(pdfBytes);
            }
            // all done, make sure the data has all been sent
            out.flush();
        } finally {
            // tidy up, even if there were errors
            try {
                pdfFile.close();
                out.close();
            } catch (IOException e) {
                // I have to catch this exception
                // ...not a lot I can do with it though
            }
        }
    }
}</pre>
<div class="disclaimer">These are my thoughts and opinions and do not reflect
those of anyone else. Read the <a href="disclaimer">disclaimer</a> for more verbal
teflon.</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.crossedstreams.com/development/how-do-i-send-a-pdf-doc-mp3-etc-file-to-the-browser/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

