<?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>Purposeful Procrastination &#187; Java</title>
	<atom:link href="http://jtnlex.com/blog/category/programming/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://jtnlex.com/blog</link>
	<description>It'll be useful someday, surely.</description>
	<lastBuildDate>Fri, 15 Jul 2011 04:19:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Google Closure Compiler in .NET via IKVM</title>
		<link>http://jtnlex.com/blog/2011/07/15/google-closure-compiler-in-net-via-ikvm/</link>
		<comments>http://jtnlex.com/blog/2011/07/15/google-closure-compiler-in-net-via-ikvm/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 04:17:33 +0000</pubDate>
		<dc:creator>JT</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[ikvm]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming tips]]></category>

		<guid isPermaLink="false">http://jtnlex.com/blog/?p=146</guid>
		<description><![CDATA[The Google Closure Compiler is a great tool for minifying JavaScript &#8211; it has the ability to both minify and optimize code, with output that&#8217;s both lightweight and performant. If you&#8217;re living on the .NET stack though, the fact that it&#8217;s in Java might be an impediment to either use or integration. In this post [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a> is a great tool for minifying JavaScript &#8211; it has the ability to both minify and optimize code, with output that&#8217;s both lightweight and performant. If you&#8217;re living on the .NET stack though, the fact that it&#8217;s in Java might be an impediment to either use or integration.</p>
<p>In this post I&#8217;m going to go through the basics of using <a href="http://www.ikvm.net/">IKVM</a> to cross-compile the Java libraries to .NET and allow the closure compiler to run on the .NET platform.</p>
<p>If you haven&#8217;t come across IKVM before, the short description is that it&#8217;s a platform bridge for Java to .NET. It translates Java byte code to MSIL, and provides a .NET compiled version of the OpenJDK runtime. You can find out more on the website &#8211; in short, it&#8217;s amazing.</p>
<p>There are <a href="http://sourceforge.net/apps/mediawiki/ikvm/index.php?title=User%27s_Guide#Overview">two ways to roll with this</a>. IKVM supports a both dynamic mode, where jars or classes are cross-compiled at runtime; and a static mode, where a compiler produces a DLL or EXE from the jars or classes. In the static case, the resulting DLL can then be used from other .NET code to integrate Java APIs into .NET applications.</p>
<h3>Getting started</h3>
<p>First thing&#8217;s first: <a href="http://www.ikvm.net/download.html">download IKVM</a> and the <a href="http://code.google.com/p/closure-compiler/">closure compiler jar</a>. You will also need the <a href="http://code.google.com/p/closure-compiler/source/browse/trunk/lib/json.jar"><code>json.jar</code> library</a> that can be found in the <a href="http://code.google.com/p/closure-compiler/source/browse/trunk/lib/">closure compiler source</a>. (Note that none of the following requires a JRE or JDK for this to work &#8211; just the .NET runtime, version 2 and above.)</p>
<p>Extract IKVM and the closure compiler to separate folders on your disk, placing the <code>json.jar</code> file in the same folder as <code>compiler.jar</code>.</p>
<h3>Running in dynamic mode</h3>
<p>This is the simplest method of execution, as the ikvm executable works the same way that the JRE does. Open a command line in the folder where the closure compiler jar resides and execute:</p>
<pre class="brush:plain">
[path-to-ikvm]\bin\ikvm -jar compiler.jar --js=[source-javascript] --js_output_file=[destination-javascript-file]
</pre>
<p>This will run the compiler and output the result to the specified location.</p>
<p>While this mode is simple, you will have noticed that it&#8217;s slow to run &#8211; it took about 15s on my system compared with less than 5s using the JRE. This penalty comes from the fact that IKVM needs to cross-compile the jar before it runs &#8211; let&#8217;s speed it up.</p>
<h3>Static compilation</h3>
<p>For this section, we&#8217;ll compile the compiler.jar to compiler.exe &#8211; a Windows executable that runs on the .NET runtime.</p>
<p>From the command line, execute the following:</p>
<pre class="brush:plain">
[path to ikvm]\bin\<strong>ikvmc</strong> -out:compiler.exe json.jar compiler.jar
</pre>
<p>Note that we&#8217;re using <strong>ikvmc</strong> (the compiler) here, not <strong>ikvm</strong> (the runtime). The above command instructs the IKVM compiler to compile the two jars into a single executable, and put the result into compiler.exe.</p>
<p>Why <code>json.jar</code>? Good question. This jar is a static dependency of the closure compiler, though I&#8217;m not sure where it&#8217;s  used (by the closure compiler). Adding it to the command line in this way means that it is compiled into the same executable as as the closure compiler, making the resulting executable easier to handle.  It is possible to compile the <code>json.jar</code> separately, then reference the resulting DLL when compiling <code>closure.jar</code>:</p>
<pre class="brush:plain">
[path to ikvm]\bin\ikvmc -out:json.dll json.jar
[path to ikvm]\bin\ikvmc -out:compiler.exe -reference:json.dll compiler.jar
</pre>
<p>Not rocket science &#8211; you&#8217;ll notice the <code>json.dll</code> is also output in the current folder, and that the <code>compiler.exe</code> decreases in size.</p>
<p>Now if you&#8217;ve executed the above commands, you will have noticed that a number of warnings are output by the compiler, for example:</p>
<pre class="brush:plain">
Warning IKVMC0100: class "org.apache.xmlbeans.XmlCursor$TokenType" not found
Warning IKVMC0100: class "org.apache.xmlbeans.XmlCursor" not found
Warning IKVMC0111: emitted java.lang.NoClassDefFoundError in "com.google.javascript.jscomp.mozilla.rhino.xml.impl.xmlbeans.LogicalEquality.nodesEqual(Lorg.apache.xmlbeans.XmlCursor;Lorg.apache.xmlbeans.XmlCursor;)Z"    ("org.apache.xmlbeans.XmlCursor")
</pre>
<p>What you&#8217;re being warned about here is an unresolved dependency in the <code>compiler.jar</code>. The Rhino JavaScript interpreter, which is included by the closure compiler has a dependency on the <a href="http://xmlbeans.apache.org/">Apache XML Beans project</a>. This isn&#8217;t used by the closure compiler AFAIK, so it can be ignored. You could, however, get the XML beans project and start compiling that (and all its dependencies) but in this case it&#8217;s not worth it. The same goes for the ANT dependency that is also shown in the warnings. The closure compiler doesn&#8217;t use these bits so you can ignore it.</p>
<h3>Running compiler.exe</h3>
<p>Now you have the executable, but when it runs it will crash &#8211; why? Merely that the executable needs some IKVM DLLs present to find the core library parts. These are all present in the IKVM bin folder. The selection of these that we need is:</p>
<ul>
<li>IKVM.OpenJDK.Core.dll</li>
<li>IKVM.OpenJDK.SwingAWT.dll</li>
<li>IKVM.OpenJDK.Text.dll</li>
<li>IKVM.OpenJDK.Util.dll</li>
<li>IKVM.OpenJDK.XML.API.dll</li>
<li>IKVM.Runtime.dll</li>
</ul>
<p>The dependency list can be found by inspecting the executable for its library references, saving you from needing to copy the entire set of IKVM library DLLs.</p>
<p>Copy the above list from the IKVM bin folder to be alongside compiler.exe. You can now run &#8220;<code>compiler -?</code>&#8221; without issue. </p>
<p>Of course, compiling JavaScript is also on the cards:</p>
<pre class="brush:plain">
compiler --js=[source-javascript] --js_output_file=[destination-javascript-file]
</pre>
<p>There&#8217;s no explicit use of  IKVM here &#8211; all that&#8217;s used are the runtime DLLs. If you look at the decompiled version of the code you&#8217;ll see that there&#8217;s very little extra weight added by IKVM, and some of that (the line number symbols) is optional.</p>
<h3>Speed</h3>
<p>Running a test on the JQuery source code on my machines resulted in the following results.</p>
<table>
<tr>
<td>JRE</td>
<td>7s</td>
</tr>
<tr>
<td>IKVM</td>
<td>16s</td>
</tr>
<tr>
<td>Compiled</td>
<td>8s</td>
</tr>
</table>
<p>As you can see, while the runtime compilation option is expensive, the compiled .NET code runs at a comparable speed to its &#8216;native&#8217; JRE counterpart. <a href="https://twitter.com/JeroenFrijters">Jeroen Frijters</a> has done an amazing job to get IKVM performing as it does.</p>
<p>This is just the start of what is possible &#8211; the real reason I wanted to look at the Closure Compiler is to see about integrating it with the <a href="http://clientdependency.codeplex.com/">Client Dependency Framework</a> &#8211; a filter that enables minification and bundling via an ASP.NET handler. More on that to come&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jtnlex.com/blog/2011/07/15/google-closure-compiler-in-net-via-ikvm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

