<?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>wuetender-junger-mann.de &#187; Java</title>
	<atom:link href="http://wuetender-junger-mann.de/wordpress/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://wuetender-junger-mann.de/wordpress</link>
	<description>Der Freiheit eine Gasse</description>
	<lastBuildDate>Tue, 20 Sep 2011 12:40:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Exception Handling &#8211; The Catch Block should go Last</title>
		<link>http://wuetender-junger-mann.de/wordpress/2011/02/exception-handling-the-catch-block-should-go-last/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2011/02/exception-handling-the-catch-block-should-go-last/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 13:22:36 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=1148</guid>
		<description><![CDATA[I have just stumbled across a piece of code like this: Object getSomeValue&#40;&#41; &#123; Object value = null; try &#123; value = errorProneOperation&#40;&#41;; &#125; catch &#40;IOException e&#41; &#123; throw new IllegalStateException&#40;e&#41;; &#125; return value; &#125; Now I find this really awkward. Why initialise something to null to keep the compiler happy? Surely you want to [...]]]></description>
			<content:encoded><![CDATA[<p>I have just stumbled across a piece of code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">Object</span> getSomeValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Object</span> value <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
           value <span style="color: #339933;">=</span> errorProneOperation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Now I find this really awkward. Why initialise something to null to keep the compiler happy?<br />
Surely you want to do this:
</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">Object</span> getSomeValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
           <span style="color: #003399;">Object</span> value <span style="color: #339933;">=</span> errorProneOperation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
           <span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">IllegalStateException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>
No useless initialisation here. Also the scope of the local variable is smaller, which is good. I think there is a general rule here, which is: &#8220;There should be nothing after your catch-handler apart from a potential finally&#8221;. As with all rules it might be broken, but only with good reason. I don&#8217;t want to see that crappy initialisation to null again.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2011/02/exception-handling-the-catch-block-should-go-last/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Java Lib to Launch External Processes</title>
		<link>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/#comments</comments>
		<pubDate>Wed, 01 Dec 2010 01:20:27 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Held der Kommandozeile]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=1096</guid>
		<description><![CDATA[I recently redesigned some of the code I tend to use to spawn external processes (pdflatex anyone?) in java. The implementation is still a bit buggy, but I am more interested in people&#8217;s opinions about the API (non-blocking killable invocations are not yet supported). The project on google code is called jproc. Here is the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently redesigned some of the code I tend to use to spawn external processes (pdflatex anyone?) in java. The implementation is still a bit buggy, but I am more interested in people&#8217;s opinions about the API (non-blocking killable invocations are not yet supported). The project on google code is called <a href="http://code.google.com/p/jproc/">jproc</a>. Here is the cookbook so far:
</p>
<p>To launch an external program  we&#8217;ll use a <code>ProcBuilder</code>. The run method<br />
builds and spawns the actual process and blocks until the process exits.<br />
 The process takes care of writing the output to a stream (as opposed to the standard<br />
facilities in the JDK that expect the client to actively consume the<br />
output from an input stream:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ByteArrayOutputStream</span> output <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withOutputStream</span><span style="color: #009900;">&#40;</span>output<span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The input can be read from an arbitrary input stream, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ByteArrayInputStream</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ByteArrayInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello cruel World&quot;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;wc&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArgs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-w&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withInputStream</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If all you want to get is the string that gets returned and if there<br />
is not a lot of data, using a streams is quite cumbersome. So for convenience<br />
if no stream is provdied the output is captured by default and can be<br />
obtained from the result.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, result.<span style="color: #006633;">getExitValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo <span style="color: #000099; font-weight: bold;">\&quot;</span>Hello World!<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, result.<span style="color: #006633;">getProcString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For providing input there is a convenience method too:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cat&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">withInput</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a string&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is a string&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Some external programs are using environment variables. These can also<br />
be set using the <code>withVar</code> method</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bash&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArgs</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;-c&quot;</span>, <span style="color: #0000ff;">&quot;echo $MYVAR&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withVar</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MYVAR&quot;</span>,<span style="color: #0000ff;">&quot;my value&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my value<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;bash -c <span style="color: #000099; font-weight: bold;">\&quot;</span>echo $MYVAR<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>, result.<span style="color: #006633;">getProcString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A common usecase for external programs is batch processing of data.<br />
These programs might always run into difficulties. Therefore a timeout can be<br />
specified. There is a default timeout of 5000ms. If the program does not terminate within the timeout<br />
interval it will be terminated and the failure is indicated through<br />
an exception:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sleep&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withTimeoutMillis</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should time out&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>TimeoutException ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Process 'sleep 2' timed out after 1000ms.&quot;</span>, ex.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Even if the process does not timeout, we might be interested in the<br />
execution time. It is also available through the result:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sleep&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0.5&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withTimeoutMillis</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertTrue<span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getExecutionTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">500</span> <span style="color: #339933;">&amp;&amp;</span> result.<span style="color: #006633;">getExecutionTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>By default the new program is spawned in the working directory of<br />
the parent process. This can be overidden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;pwd&quot;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">withWorkingDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, result.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is a time honoured tradition that programs signal a failure<br />
by returning a non-zero exit value. However in java failure is<br />
signalled through exceptions. Non-Zero exit values therefore<br />
get translated into an exception, that also grants access to<br />
the output on standard error.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls&quot;</span><span style="color: #009900;">&#41;</span>
                            .<span style="color: #006633;">withArg</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;xyz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    fail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Should throw exception&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>ExternalProcessFailureException ex<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls: xyz: No such file or directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ex.<span style="color: #006633;">getStderr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, ex.<span style="color: #006633;">getExitValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls xyz&quot;</span>, ex.<span style="color: #006633;">getCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ls: xyz: No such file or directory<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, ex.<span style="color: #006633;">getStderr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    assertTrue<span style="color: #009900;">&#40;</span>ex.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Input and output can also be provided as <code>byte[]</code>.<br />
<code>ProcBuilder</code> also copes with large amounts of<br />
data.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> MEGA <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span> <span style="color: #339933;">*</span> MEGA<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> data.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    data<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">round</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Math</span>.<span style="color: #006633;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">255</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">128</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
ProcResult result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;gzip&quot;</span><span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">withInput</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
   .<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertTrue<span style="color: #009900;">&#40;</span>result.<span style="color: #006633;">getOutputBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">*</span> MEGA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The builder allows to build and spawn several processes from<br />
the same builder instance:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">ProcBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ProcBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;uuidgen&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> uuid1 <span style="color: #339933;">=</span> builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> uuid2 <span style="color: #339933;">=</span> builder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOutputString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertNotNull<span style="color: #009900;">&#40;</span>uuid1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertNotNull<span style="color: #009900;">&#40;</span>uuid2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertTrue<span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>uuid1.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>uuid2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For convenience there is also a static method that just runs a<br />
program and captures the ouput:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> ProcBuilder.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;echo&quot;</span>, <span style="color: #0000ff;">&quot;Hello World!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Also there is a static method that filters a given string through<br />
a program:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> output <span style="color: #339933;">=</span> ProcBuilder.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x y z&quot;</span>,<span style="color: #0000ff;">&quot;sed&quot;</span> ,<span style="color: #0000ff;">&quot;s/y/a/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;x a z<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, output<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2010/12/java-lib-to-launch-external-processes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Thoughts on handling Translations and Views on Source Code</title>
		<link>http://wuetender-junger-mann.de/wordpress/2010/11/thoughts-on-handling-translations-and-views-on-source-code/</link>
		<comments>http://wuetender-junger-mann.de/wordpress/2010/11/thoughts-on-handling-translations-and-views-on-source-code/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 08:17:46 +0000</pubDate>
		<dc:creator>felix</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://wuetender-junger-mann.de/wordpress/?p=1064</guid>
		<description><![CDATA[One of my recent java projects was to be used by users with three different languages. We went with the standard java approach of using properties files for messages. In intellij there is decent tool support for that. However it seems a bit odd to have a strongly typed language and then rely on string [...]]]></description>
			<content:encoded><![CDATA[<p>One of my recent java projects was to be used by users with three different languages.  We went with the standard java approach of using properties files for messages. In intellij there is decent tool support for that.<br />
However it seems a bit odd to have a strongly typed language and then rely on string keys for text lookup. At some point we introduced enums representing the keys, but as they were not automatically generated they involved a lot of repetition. Also you are never quite sure how many arguments a message needs. </p>
<p>So I had this idea of using interfaces to represent resource bundles. Each message could be represented as a method, with parameters representing the arguments to the placeholders. It would look somewhat like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ExampleMessagePool <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> bye<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> warning<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>From the clients point of view it would works as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">var factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MessagePoolFactory<span style="color: #339933;">&lt;</span>ExampleMessagePool<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>ExampleMessagePool.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
ExampleMessagePool english <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getLanguageSource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;en&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello Matthieu!&quot;</span>, english.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Matthieu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Good bye Felix!&quot;</span>, english.<span style="color: #006633;">bye</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Felix&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attention!&quot;</span>, english.<span style="color: #006633;">warning</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
ExampleMessagePool french <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getLanguageSource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fr&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bonjour Matthieu!&quot;</span>, french.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Matthieu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Au revoir Felix!&quot;</span>, french.<span style="color: #006633;">bye</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Felix&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Attention!&quot;</span>, french.<span style="color: #006633;">warning</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
ExampleMessagePool german <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getLanguageSource</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;de&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Guten Tag Matthieu!&quot;</span>, german.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Matthieu&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Auf Wiedersehen Felix!&quot;</span>, german.<span style="color: #006633;">bye</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Felix&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
assertEquals<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Achtung!&quot;</span>, german.<span style="color: #006633;">warning</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
The interesting point here is that I get completion and also a hint as of which arguments a particular message takes. The MessagePoolFactory takes care of creating instances for the respective languages. The next question is obviously, where these messages do come from. One way would be to use annotations:
</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@MessagePool<span style="color: #009900;">&#40;</span>languages<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;en&quot;</span>, <span style="color: #0000ff;">&quot;de&quot;</span>, <span style="color: #0000ff;">&quot;fr&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ExampleMessagePool <span style="color: #009900;">&#123;</span>
    @Translations<span style="color: #009900;">&#40;</span>
            entries <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello {0}!&quot;</span><span style="color: #009900;">&#41;</span>,
                @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;de&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Guten Tag {0}!&quot;</span><span style="color: #009900;">&#41;</span>,
                @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;fr&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Bonjour {0}!&quot;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Translations<span style="color: #009900;">&#40;</span>
	        entries <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Good bye {0}!&quot;</span><span style="color: #009900;">&#41;</span>,
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;de&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Auf Wiedersehen {0}!&quot;</span><span style="color: #009900;">&#41;</span>,
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;fr&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Au revoir {0}!&quot;</span><span style="color: #009900;">&#41;</span>
	        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span>
	<span style="color: #003399;">String</span> bye<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Translations<span style="color: #009900;">&#40;</span>
	        entries <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;en&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Attention!&quot;</span><span style="color: #009900;">&#41;</span>,
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;de&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Achtung!&quot;</span><span style="color: #009900;">&#41;</span>,
	            @Entry<span style="color: #009900;">&#40;</span>key <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;fr&quot;</span>, value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Attention!&quot;</span><span style="color: #009900;">&#41;</span>
	        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#41;</span>
	<span style="color: #003399;">String</span> warning<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
This approach is very sexy in so far as it allows to refactor method and parameter names. Also the <a href="http://wuetender-junger-mann.de/wordpress/wp-content/uploads/2010/11/MessagePoolFactory.txt">implementation</a> is trivial not least because java deals with unicode source files. On the other hand making your translators edit the java sources is probably a bit of a challenge. However you could provide a narrow view on that source code. The eclipse platform lends itself to that kind of experiment, so I actually did a bit of spike. Which I am demoing here:</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/vACair4bgxk?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/vACair4bgxk?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p>Whilst this is very cool, it&#8217;s an entirely static approach. Especially with translations I find it beneficial if they are stored in a database so that they can be edited at run-time. Ideally some key users can then maintain translations, which are part of their domain anyway. Then the interface could be annotated with some sort of GUID, that could later be used at runtime to identify entries in the database:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> ExampleMessagePool <span style="color: #009900;">&#123;</span>
    @Id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;21850286-A914-4C13-88BA-75ACA2248B71&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DA3CD6BE-64E8-4501-A674-0789043CBD6F&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> bye<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Id<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;38211D99-BA2A-4572-9C23-976AC64181D8&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #003399;">String</span> warning<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
This way the refactorability would be preservered. Also tools support could be provided. Annotating elements with GUUIDs seems to be an interesting concept. Just imagine your database mapping being immune to name changes.
</p>
<p>There is definitely some more thinking required here, but it strikes me that the default mechanisms in java are very rudimentary. Much less than a talented developer can dream up in a rainy afternoon.<br />
Further developments could include  static or, if you go for the runtime approach, dynamic tools to analyse, whether translations are present, or whether there are duplicates in message pools.</p>
]]></content:encoded>
			<wfw:commentRss>http://wuetender-junger-mann.de/wordpress/2010/11/thoughts-on-handling-translations-and-views-on-source-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

