<?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>Jeff Sinclair's Website &#187; java</title>
	<atom:link href="http://www.cooljeff.co.uk/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cooljeff.co.uk</link>
	<description>my 2 pence to the world</description>
	<lastBuildDate>Sat, 12 Dec 2009 22:17:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Garbage collection of the permanent generation (permgen)</title>
		<link>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/</link>
		<comments>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 09:45:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[hotspot]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=153</guid>
		<description><![CDATA[There is often confusion around what the permanent generation contains and given its name, whether or not it can be garbage collected. To cut a long story short, the permanent generation can be garbage collected and is where reflective class meta data, as well as string constants are stored. 
This blog entry takes a practical [...]]]></description>
			<content:encoded><![CDATA[<p>There is often confusion around what the permanent generation contains and given its name, whether or not it can be garbage collected. To cut a long story short, the permanent generation can be garbage collected and is where reflective class meta data, as well as string constants are stored. </p>
<p>This blog entry takes a practical approach to answer the following questions:</p>
<ul>
<li>Can the permanent generation be garbage collected?</li>
<li>Does the value of -Xmx (the maximum heap size) include the permanent generation?</li>
</ul>
<h4>Can the permanent generation be garbage collected?</h4>
<p>Yes it can. There is often the misconception that string constants cannot be garbage collected. However the only requirement that the JVM specification stipulates is that identity comparison works for constant string values. There is no requirement that the constant be the same object throughout the lifetime of the JVM. Hence if there are no references left to a string constant, there is no reason why it cannot be garbage collected.</p>
<p>To prove this take the following code:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PermGenDemo <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-weight: bold;">int</span> i = <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #006600; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;string-&quot;</span> + ++i<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">intern</span><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: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>If the above code is run using the following command:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
$ java -verbose:<span style="color: #c20cb9; font-weight: bold;">gc</span> -XX:<span style="color: #007800;">PermSize</span>=8m -XX:<span style="color: #007800;">MaxPermSize</span>=64m PermGenDemo</pre>
<p>The following output will be seen (note output cut for brevity):</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Full GC <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSYoungGen: 32K-<span style="color: #000000; font-weight: bold;">&gt;</span>0K<span style="color: #7a0874; font-weight: bold;">&#40;</span>28928K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> . . . <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSPermGen: 65535K-<span style="color: #000000; font-weight: bold;">&gt;</span>6461K<span style="color: #7a0874; font-weight: bold;">&#40;</span>65536K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Full GC <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSYoungGen: 32K-<span style="color: #000000; font-weight: bold;">&gt;</span>0K<span style="color: #7a0874; font-weight: bold;">&#40;</span>31168K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> . . . <span style="color: #7a0874; font-weight: bold;">&#91;</span>PSPermGen: 65535K-<span style="color: #000000; font-weight: bold;">&gt;</span>6461K<span style="color: #7a0874; font-weight: bold;">&#40;</span>65536K<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre>
<p>The above conclusively shows that the permanent generation is collected and when it requires collecting, triggers a full collection.</p>
<h4>Does the value of -Xmx (the maximum heap size) include the permanent generation?</h4>
<p>No it does not. The size of the permanent generation is controlled by the following JVM options:</p>
<p>   * <em>-XX:MaxPermSize</em> controls the maximum permanent generation size.<br />
   * <em>-XX:PermSize</em> controls the initial permanent generation size.</p>
<p>The permanent generation is like any other generational compartment: it can grow to a maximum, technically can shrink and can also be garbage collected.</p>
<p>To demonstrate that the permanent generation is not included in the maximum heap settings, pmap can be used (e.g. by running <em>pmap &lt;pid&gt; | tail -1</em>).</p>
<p>The following table illustrates the output of pmap using different JVM heap and permanent generation settings:</p>
<table border="1">
<tr>
<td><strong>JVM options</strong></td>
<td><strong>pmap output</strong></td>
</tr>
<tr>
<td>java -Xms256m -Xmx256m -XX:PermSize=64m -XX:MaxPermSize=64m</td>
<td>512204K</td>
</tr>
<tr>
<td>java -Xms512m -Xmx512m -XX:PermSize=64m -XX:MaxPermSize=64m</td>
<td>760556K</td>
</tr>
<tr>
<td>java -Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m</td>
<td>825188K</td>
</tr>
</table>
<p>In the above <em>-Xms=-Xmx</em> and <em>-XX:PermSize=-XX:MaxPermSize</em> are set deliberately to stop the heap expanding and ensure constant results. The first value of <em>512204K</em> gives us a base value which takes into account any overhead introduced by native libraries (including the jvm itself) being mapped into memory. The second value of <em>760556K</em> is an exact increase of <em>256m</em>, which is exactly how much the heap was increased by. The third value of <em>825188K</em> is an exact increase of <em>64m</em>, which is exactly how much the permanent generation was increased by. </p>
<p>The above results clearly show that the settings for permgen are unrelated to that of the heap.</p>
<h4>Conclusion</h4>
<ul>
<li>The permanent generation can be garbage collected like other generations.</li>
<li>The permanent generation is additional memory to the java heap.</li>
<li><em>-XX:MaxPermSize</em> controls the maximum memory used by the permanent generation.</li>
<li><em>-XX:PermSize</em> controls the initial memory used by the permanent generation.</li>
</ul>
<h4>Resources</h4>
<ol>
<li><a href=" http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp">Java Hotspot VM Options</a></li>
<li><a href="http://blogs.sun.com/jonthecollector/entry/presenting_the_permanent_generation">Presenting the Permanent Generation</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/11/01/garbage-collection-of-the-permanent-generation-permgen/feed/</wfw:commentRss>
		<slash:comments>270</slash:comments>
		</item>
		<item>
		<title>Detecting all running JVMs</title>
		<link>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/</link>
		<comments>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 21:19:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[hotspot]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=108</guid>
		<description><![CDATA[Several well known JVM tools appear to be able to magically detect the Java processes running on a system. Good examples of this are: jps and Visual VM. As of Java 6 it is even possible to find out yourself which Java processes are running on the local machine that you have access to using [...]]]></description>
			<content:encoded><![CDATA[<p>Several well known JVM tools appear to be able to magically detect the Java processes running on a system. Good examples of this are: <a href="http://java.sun.com/javase/6/docs/technotes/tools/share/jps.html" target="_blank">jps</a> and <a href="https://visualvm.dev.java.net/" target="_blank">Visual VM</a>. As of Java 6 it is even possible to find out yourself which Java processes are running on the local machine that you have access to using the <a href="http://java.sun.com/javase/6/docs/technotes/guides/attach/index.html" target="_blank">Dynamic Attach API</a>. These APIs can be found in the tools.jar (JAVA_HOME/lib/tools.jar).</p>
<p>The following code illustrates how the Dynamic Attach API can be used to list all of the local virtual machine identifiers (which for the HotSpot VM map onto the JVM PID):</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.dynamicattach</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.tools.attach.VirtualMachine</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sun.tools.attach.VirtualMachineDescriptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JVMFinder <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    List<span style="color: #339933;">&lt;</span>VirtualMachineDescriptor<span style="color: #339933;">&gt;</span> vmDescriptors = VirtualMachine.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>VirtualMachineDescriptor vmDescriptor : vmDescriptors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Name: &quot;</span> + vmDescriptor.<span style="color: #006633;">displayName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                                    + <span style="color: #0000ff;">&quot; PID: &quot;</span> + vmDescriptor.<span style="color: #006633;">id</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>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The above gives the following output on my machine (cut out a few for brevity):</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
Name: uk.co.cooljeff.dynamicattach.JVMFinder PID: <span style="color: #000000;">21116</span></pre>
<p>I&#8217;m about to spoil the fun now for those of you who were hoping to see some wacky OS dependent mechanism or hidden RMI registry for finding this information. The solution is quite simple, the JVM dumps a file containing performance data to a standard location which it uses to work out what JVMs are running on a host.</p>
<p>Specifically the performance data is for <a href="http://java.sun.com/performance/jvmstat/" target="_blank">jvmstat</a>, however it is this file that forms the basis of the Dynamic Attach implementation that HotSpot uses.</p>
<p>The following piece of code replicates what the LocalVmManager (part of jvmstat) does to locate running Java processes:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">uk.co.cooljeff.dynamicattach</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FilenameFilter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Matcher</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.regex.Pattern</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.jvmstat.perfdata.monitor.protocol.local.PerfDataFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JVMDataLocation <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #006600; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #003399; font-weight: bold;">Exception</span></a> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html"><span style="color: #003399; font-weight: bold;">Pattern</span></a> filePattern = <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html"><span style="color: #003399; font-weight: bold;">Pattern</span></a>.<span style="color: #006633;">compile</span><span style="color: #009900;">&#40;</span>PerfDataFile.<span style="color: #006633;">userDirNamePattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html"><span style="color: #003399; font-weight: bold;">Matcher</span></a> fileMatcher = filePattern.<span style="color: #006633;">matcher</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html"><span style="color: #003399; font-weight: bold;">FilenameFilter</span></a> fileFilter = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/FilenameFilter.html"><span style="color: #003399; font-weight: bold;">FilenameFilter</span></a><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;">public</span> <span style="color: #006600; font-weight: bold;">boolean</span> accept<span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a> dir, <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #003399; font-weight: bold;">String</span></a> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fileMatcher.<span style="color: #006633;">reset</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> fileMatcher.<span style="color: #006633;">matches</span><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: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> files = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a><span style="color: #009900;">&#40;</span>PerfDataFile.<span style="color: #006633;">getTempDirectory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">listFiles</span><span style="color: #009900;">&#40;</span>fileFilter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000;  font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html"><span style="color: #003399; font-weight: bold;">File</span></a> file : files<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html"><span style="color: #003399; font-weight: bold;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PerfDataDir: &quot;</span> + file<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Which gives the following on my machine:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_root
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_tomcat55
PerfDataDir: <span style="color: #000000; font-weight: bold;">/</span>tmp<span style="color: #000000; font-weight: bold;">/</span>hsperfdata_jeffsinc</pre>
<p>If you take a look at one of these directories you will find 1 file per Java process running by the specific user. The name of the file is the local virtual machine identifier (lvmid) which for HotSpot will correspond to the process id. If you run <em>strings</em> on the file you will see all of the properties relating to that JVM instance.</p>
<p>It is as simple as that, on Linux the JVM simply looks for all files that match: <em>/tmp/hsperfdata_*/*</em>.</p>
<p>Resources:</p>
<ul>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/attach/index.html">Dynamic Attach API</a></li>
<li><a href="https://jdk7.dev.java.net">OpenJDK 7 source code</a></li>
<li><a href="https://visualvm.dev.java.net">Visual VM</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/04/05/detecting-all-running-jvms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building the OpenJDK on Ubuntu 8.10</title>
		<link>http://www.cooljeff.co.uk/2009/01/03/building-the-openjdk-on-ubuntu-810/</link>
		<comments>http://www.cooljeff.co.uk/2009/01/03/building-the-openjdk-on-ubuntu-810/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 23:54:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[openjdk]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=79</guid>
		<description><![CDATA[When I asked a colleague at work what I should do during my vacation, he said build the OpenJDK&#8230; so I did!
In hindsight, it would have been easier to have built IcedTea (RedHat&#8217;s OpenJDK hybrid) rather than Sun&#8217;s raw OpenJDK. However since I spent quite a bit of time setting up the environment I was [...]]]></description>
			<content:encoded><![CDATA[<p>When I asked a colleague at work what I should do during my vacation, he said build the OpenJDK&#8230; so I did!</p>
<p>In hindsight, it would have been easier to have built IcedTea (RedHat&#8217;s OpenJDK hybrid) rather than Sun&#8217;s raw OpenJDK. However since I spent quite a bit of time setting up the environment I was determined to get it to build.</p>
<p>At a high level I did the following:</p>
<ul>
<li>Downloaded the source (~ 85Mb): <a title="OpenJDK download" href="http://download.java.net/openjdk/jdk7/" target="_blank">http://download.java.net/openjdk/jdk7/</a>.</li>
<li>Downloaded the binary plugins (the bits of the OpenJDK that are yet to be open sourced).</li>
<li>Downloaded the dependencies.</li>
<li>Modified some environment variables.</li>
<li>Checked the environment is ok via the sanity script.</li>
<li>Made 1 change to the source code.</li>
<li>Made 1 change to the make compiler flags.</li>
<li>Ran the build.</li>
</ul>
<p>The README file did not cover all of the dependencies needed to build the OpenJDK on Ubuntu, here is what worked for me:</p>
<ul>
<li>sudo apt-get install build-essential</li>
<li>sudo apt-get install gawk</li>
<li>sudo apt-get install m4</li>
<li>sudo apt-get install libasound2-dev</li>
<li>sudo apt-get install libcupsys2-dev</li>
<li>sudo apt-get install sun-java6-jdk</li>
<li>sudo apt-get install x11proto-print-dev</li>
<li>sudo apt-get install libxp-dev</li>
<li>sudo apt-get install libfreetype6-dev</li>
<li>sudo apt-get install libxt-dev</li>
<li>sudo apt-get install libxtst-dev</li>
</ul>
<p>The OpenJDK comes with a script (<em>jdk/make/jdk_generic_profile.sh</em>) to help set up your environment. The main updates I needed to make were:</p>
<ul>
<li>The <em>bootjdk </em>variable which holds the folder name of your Java 6 install.</li>
<li>The <em>jdk_instances</em> variable which holds the parent directory of your <em>bootjdk</em>.</li>
<li>The <em>binplugs</em> variable which holds the location of the non open source bits of the OpenJDK.</li>
<li>Ensure <em>ANT_HOME</em> was exported.</li>
<li>Ensusre <em>ANT_HOME/bin</em> was on the <em>PATH</em>.</li>
</ul>
<p>You can see my full environment here: <a title="jdk_generic_profile" href="http://www.cooljeff.co.uk/wp-content/uploads/2009/01/jdk_generic_profile.sh" target="_blank">jdk_generic_profile</a>.</p>
<p>The environment was then verified using the <em>sanity</em> make target:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
$ . jdk<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #000000; font-weight: bold;">/</span>jdk_generic_profile.sh
$ <span style="color: #c20cb9; font-weight: bold;">make</span> sanity</pre>
<p>Once this passed I tried building but got a couple of errors.</p>
<p>The first error was:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>openjdk<span style="color: #000000; font-weight: bold;">/</span>hotspot<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>vm<span style="color: #000000; font-weight: bold;">/</span>libadt<span style="color: #000000; font-weight: bold;">/</span>port.hpp:
In <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #ff0000;">'void bcopy(const void*, void*, size_t)'</span>:
<span style="color: #000000; font-weight: bold;">/</span>openjdk<span style="color: #000000; font-weight: bold;">/</span>hotspot<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>vm<span style="color: #000000; font-weight: bold;">/</span>libadt<span style="color: #000000; font-weight: bold;">/</span>port.hpp:<span style="color: #000000;">40</span>:
error: <span style="color: #ff0000;">'void bcopy(const void*, void*, size_t)'</span> redeclared inline
without <span style="color: #ff0000;">'gnu_inline'</span> attribute
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>bits<span style="color: #000000; font-weight: bold;">/</span>string3.h:<span style="color: #000000;">90</span>:
error: <span style="color: #ff0000;">'void bcopy(const void*, void*, size_t)'</span> previously defined here
<span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">6</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: <span style="color: #000000; font-weight: bold;">***</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>incls<span style="color: #000000; font-weight: bold;">/</span>_precompiled.incl.gch<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">1</span></pre>
<p>I understand what the error means but I could not work out why the error occurs because after looking through <em>/usr/include/string.h</em>, the include of <em>/usr/include/bits/string3.h</em> is only done if the user specifies <em>_FORTIFY_SOURCE</em> which as far as I can tell is not done in the OpenJDK. My workaround is quite simple, I commented out the redeclaration of<em> inline void bcopy(const void*, void*, size_t)</em> since it appears to already be inlined somehow.</p>
<p>The second error was:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">/</span>openjdk<span style="color: #000000; font-weight: bold;">/</span>hotspot<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>vm<span style="color: #000000; font-weight: bold;">/</span>runtime<span style="color: #000000; font-weight: bold;">/</span>arguments.cpp:
In static member <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #ff0000;">'static void Arguments::set_aggressive_opts_flags()'</span>:
<span style="color: #000000; font-weight: bold;">/</span>openjdk<span style="color: #000000; font-weight: bold;">/</span>hotspot<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>vm<span style="color: #000000; font-weight: bold;">/</span>runtime<span style="color: #000000; font-weight: bold;">/</span>arguments.cpp:<span style="color: #000000;">1359</span>:
error: format <span style="color: #ff0000;">'%d'</span> expects <span style="color: #7a0874; font-weight: bold;">type</span> <span style="color: #ff0000;">'int'</span>, but argument <span style="color: #000000;">3</span> has <span style="color: #7a0874; font-weight: bold;">type</span> <span style="color: #ff0000;">'intx'</span></pre>
<p>This occurs because of a flag that treats WARNINGS as ERRORS. The correct approach would have been to go and fix the warnings, instead I disabled the flag <em>WARNINGS_ARE_ERRORS = -Werror</em> which can be found in <em>hotspot/make/linux/makefiles/gcc.make</em>.</p>
<p>After making these changes I was able to run the build (just typed make) and saw the following output in the end:</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
Control linux amd64 1.7.0-internal build_product_image build finished:
Control linux amd64 1.7.0-internal all_product_build build finished:
Control linux amd64 1.7.0-internal all build finished:</pre>
<p>I had a working JDK which could be found in <em>build/linux-amd64/j2sdk-image/bin</em>.</p>
<pre class="bash" style="font-family:monospace;">&nbsp;
$ .<span style="color: #000000; font-weight: bold;">/</span>java <span style="color: #660033;">-version</span>
openjdk version <span style="color: #ff0000;">&quot;1.7.0-internal&quot;</span>
OpenJDK Runtime Environment <span style="color: #7a0874; font-weight: bold;">&#40;</span>build 1.7.0-internal-jeffsinc_2009_01_03_10_52-b00<span style="color: #7a0874; font-weight: bold;">&#41;</span>
OpenJDK <span style="color: #000000;">64</span>-Bit Server VM <span style="color: #7a0874; font-weight: bold;">&#40;</span>build <span style="color: #000000;">14.0</span>-b09, mixed mode<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/01/03/building-the-openjdk-on-ubuntu-810/feed/</wfw:commentRss>
		<slash:comments>410</slash:comments>
		</item>
		<item>
		<title>APT v AspectJ</title>
		<link>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/</link>
		<comments>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 18:35:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[apt]]></category>
		<category><![CDATA[aspectj]]></category>

		<guid isPermaLink="false">http://www.cooljeff.co.uk/?p=39</guid>
		<description><![CDATA[APT has interested me for quite some time because after reading various blog entries and articles, it was unclear to me where APT fits in relation to other tools that provide functionality triggered by annotations. Specifically I've been interested in any overlap between APT and AspectJ.

Both AspectJ and APT provide the following common functionality:

<ul>
<li>Ability to intercept annotations.</li>
<li>Ability to declare custom compiler errors and warnings based off annotation matching.</li>
</ul>

I've attempted to implement an architecture enforcement requirement using both tools in order to better understand where APT fits in the developer toolbox.]]></description>
			<content:encoded><![CDATA[<p>APT has interested me for quite some time because after reading various blog entries and articles, it was unclear to me where APT fits in relation to other tools that provide functionality triggered by annotations. Specifically I&#8217;ve been interested in any overlap between APT and AspectJ.</p>
<p>Both AspectJ and APT provide the following common functionality:</p>
<ul>
<li>Ability to intercept annotations.</li>
<li>Ability to declare custom compiler errors and warnings based off annotation matching.</li>
</ul>
<p>I attempted to implement an architecture enforcement requirement using both tools. The requirement was simple: a compile error should occur when an attempt is made to access a class annotated with <em>@RestrictedResource</em> unless the caller is annotated with <em>@SuppressRestrictions</em>.</p>
<p>This was relatively simple with AspectJ, the following Aspect provides the enforcement:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> aspect RestrictedResourceEnforcer <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> pointcut accessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> :
        call<span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>@RestrictedResource <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>..<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                || call<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>@RestrictedResource <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>.<span style="color: #000000; font-weight: bold;">new</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: #000000; font-weight: bold;">private</span> pointcut withinUnrestrictedCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> :
        @withincode<span style="color: #009900;">&#40;</span>SuppressRestrictions<span style="color: #009900;">&#41;</span> || @within<span style="color: #009900;">&#40;</span>SuppressRestrictions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    declare error : accessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>withinUnrestrictedCode<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>:
        <span style="color: #0000ff;">&quot;Attempt to access a restricted resource.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>The following test demonstrated the compiler error as well as the lack of compiler error should <em>@SuppressRestrictions</em> be used:</p>
<pre class="java5" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RestrictedResourceEnforcerTest <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> testAccessToRestrictedResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Compile error: attempt to access restricted resource.</span>
        RestrictedClass restrictedClass = <span style="color: #000000; font-weight: bold;">new</span> RestrictedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Compile error: attempt to access restricted resource.</span>
        restrictedClass.<span style="color: #006633;">method1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @SuppressRestrictions
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> testAccessToSuppressedRestrictionResource<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        RestrictedClass restrictedClass = <span style="color: #000000; font-weight: bold;">new</span> RestrictedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        restrictedClass.<span style="color: #006633;">method1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @RestrictedResource
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> RestrictedClass <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> method1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre>
<p>Immediately I realised that it is not possible to implement this kind of architecture enforcement using APT without annotating the calling class (i.e <em>RestrictedResourceEnforcerTest</em>). Without marking the calling class, I&#8217;m not going to get a process() callback from APT.</p>
<p>This attempt has shown me that APT is only of real use for processing that is completely contained within the annotated class.</p>
<p>I still wanted try APT in direct comparison with AspectJ so I tried enforcing immutability on any class with an <em>@Immutable</em> annotation. I gave up pretty quickly using APT for this because although it is technically possible, it requires a lot of work with ASM to generate the correct source to enforce immutability (i.e. return unmodifiable collections on getters, throw an exception with setters). AspectJ provides a much more maintainable solution without ever having to go near something like ASM.</p>
<p>So I&#8217;m left wondering if APT has any value to your regular Java developer (rather than toolkit developer), I don&#8217;t think it has. APT&#8217;s use-case looks purely in the space of creating extensions to the regular Java compiler to generate bye products directly related to the class with the annotation.</p>
<p>As a general rule I&#8217;d break down APT and AspectJ usage as follows:</p>
<ul>
<li>Use AspectJ if you need to add a functional requirement to existing entities. Examples include: monitoring, architecture enforcement, transactional functionality.</li>
<li>Use APT if you need to generate bye products for framework integration. Examples include: schema generation, source code generation tools (e.g. JAXB, JAXWS)</li>
</ul>
<p>APT is not designed to be updating code to meet a functional requirement, AspectJ is. Instead APT provides a compiler extension that allows you to generate bye products driven by meta data on a class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cooljeff.co.uk/2009/01/02/apt-v-aspectj/feed/</wfw:commentRss>
		<slash:comments>1106</slash:comments>
		</item>
	</channel>
</rss>

