<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.3.2" -->
<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/"
	>

<channel>
	<title>javajiggle.com</title>
	<link>http://javajiggle.com</link>
	<description>Java Jiggle. See what's shaking.</description>
	<pubDate>Fri, 15 Aug 2008 18:03:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.2</generator>
	<language>en</language>
			<item>
		<title>Know your Logic.  About: &#038;&#038;, &#038;, &#124;&#124; and &#124; operators.</title>
		<link>http://javajiggle.com/2008/04/08/know-your-logic-about-and-operators/</link>
		<comments>http://javajiggle.com/2008/04/08/know-your-logic-about-and-operators/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 02:30:06 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2008/04/08/know-your-logic-about-and-operators/</guid>
		<description><![CDATA[Know Your Logic
Many are not aware of the optimized logic statements.   This could lead to the logic errors.   In Java there are two AND and two OR statements.   One is optimized and one is not.   The optimized operator will not evaluate a second (right) statement if the [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom: 0in"><strong>Know Your Logic</strong></p>
<p style="margin-bottom: 0in"><img src="https://duke.dev.java.net/images/misc/DukeTango.png" alt="https://duke.dev.java.net/images/misc/DukeTango.png" align="left" height="170" width="228" />Many are not aware of the optimized logic statements.   This could lead to the logic errors.   In Java there are two AND and two OR statements.   One is optimized and one is not.   The optimized operator will not evaluate a second (right) statement if the final outcome is clear from evaluation of the first (left) statement.</p>
<p style="margin-bottom: 0in">For example, A || B, if A is TRUE the result of the statement is TRUE regardless of what B evaluates to.   Therefore B will not be evaluated.   That is an optimized OR statement, if you require that both sides of the operator evaluated, use unoptimized operator |.   That would be, A | B, so even if A is true, B will be evaluated regardless.</p>
<p style="margin-bottom: 0in">The AND operator works in a similar fashion.   For example, A &amp;&amp; B, if A evaluates to FALSE the result of the statement will be FALSE  regardless of what B evaluates to.   Therefore B will not be evaluated.   Again, if you require both sides of the expression evaluated, use unoptimized &amp; operator.</p>
<p style="margin-bottom: 0in">That would be, A &amp; B, so event if A evaluates to false, B will be evaluated regardless.</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong><u>See this example:</u></strong></p>
<p style="margin-bottom: 0in"><em>class test {</em></p>
<blockquote><p><em>public static boolean checkSomething( String from ){</em></p></blockquote>
<blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>		System.out.println(&#8221;checkSomething() from-&gt; &#8221; + from );</em></p>
</blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>		return true;</em></p>
</blockquote>
<p style="margin-bottom: 0in"><em>	}</em></p>
</blockquote>
<p style="margin-bottom: 0in">&nbsp;</p>
<blockquote>
<p style="margin-bottom: 0in"><em>	public static void main( String [] arg ){</em></p>
<blockquote>
<p style="margin-bottom: 0in"><em>		if( true || checkSomething(&#8221; || &#8220;) )</em></p>
</blockquote>
<blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>			System.out.println(&#8221; || test&#8221;);</em></p>
</blockquote>
</blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>		if( true | checkSomething( &#8221; | &#8220;) )</em></p>
</blockquote>
<blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>			System.out.println(&#8221; | test&#8221;);</em></p>
</blockquote>
</blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>		if( false &amp;&amp; checkSomething( &#8221; &amp;&amp; &#8220;) )</em></p>
</blockquote>
<blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>			System.out.println(&#8221; &amp;&amp; test&#8221; );</em></p>
</blockquote>
</blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>		if( false &amp; checkSomething( &#8221; &amp; &#8220;) )</em></p>
</blockquote>
<blockquote>
<blockquote>
<p style="margin-bottom: 0in"><em>			System.out.println(&#8221; &amp; test&#8221; );</em></p>
</blockquote>
</blockquote>
<p style="margin-bottom: 0in"><em>	}</em></p>
</blockquote>
<p style="margin-bottom: 0in"><em>}</em></p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong><u>Output:</u></strong></p>
<blockquote>
<p style="margin-bottom: 0in"> || test</p>
<p style="margin-bottom: 0in">checkSomething() from-&gt;  |</p>
<p style="margin-bottom: 0in"> | test</p>
<p style="margin-bottom: 0in">checkSomething() from-&gt;  &amp;</p>
</blockquote>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in">You can see, that <em>checkSomething(String)</em> is evaluated only with unoptimized operators.</p>
<p style="margin-bottom: 0in">Know your logic! Code bug free!</p>
<p style="margin-bottom: 0in">I know you want to, give us a jiggle <img src='http://javajiggle.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2008/04/08/know-your-logic-about-and-operators/feed/</wfw:commentRss>
		</item>
		<item>
		<title>If JNI based application is crashing, check signal handling!</title>
		<link>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/</link>
		<comments>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 05:03:48 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/</guid>
		<description><![CDATA[!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 	-- 	
When designing an application that is made up of Java and Native code it is important to handle signals correctly.   What I&#8217;m trying to say is that JVM uses signals internally and if your application registers itself as [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 	-- 	</style>
<p style="margin-bottom: 0in"><img src="https://duke.dev.java.net/images/web/Duke_Thread.gif" align="left" height="191" width="241" />When designing an application that is made up of Java and Native code it is important to handle signals correctly.   What I&#8217;m trying to say is that JVM uses signals internally and if your application registers itself as a signal handler it will intercept signals intended for JVM.   As a result JVM&#8217;s signal handler will not be invoked.   Eventually this will lead to incorrect behavior and most likely a JVM crash.</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in">Don&#8217;t worry Sun provides an excellent <strong>signal chaining</strong> library that makes solving this problem very easy. Let&#8217;s elaborate on this a bit.</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>First thing first, what are signals?</strong></p>
<p style="margin-bottom: 0in">First of all, lets make sure we are on the same page about signals.    As stated in wikipedia “A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems.”  See <a href="http://en.wikipedia.org/wiki/Signal_%28computing%29">http://en.wikipedia.org/wiki/Signal_%28computing%29</a></p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>Problem Description</strong></p>
<p style="margin-bottom: 0in">Solaris and Linux JVMs use signals internally.   This means that the JVM will originate signals and it will need to handle them.   The problem arises when native code registers itself as a signal handler following JVMs initialization.   Since there can be only one signal handler for a particular signal, the native code signal handler will overwrite JVMs signal handler.   When this happens, every time JVM originates a signal, the signal will be intercepted by the native code signal handler.   As a result JVM signal handler will neverget invoked.   This can lead to the unexplained behavior that is very difficult to debug.</p>
<p style="margin-bottom: 0in"><img src="http://javajiggle.com/SCfiles/NormalJVM.png" /></p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>Example</strong></p>
<p style="margin-bottom: 0in">Check the attached sample code for example.   In this example, the C++ file creates a JVM and registers a signal handler immediately after.   This will override JVMs signal handler.   If you let this program run you will witness a JVM crash.   JVMs will originate signals, its signal handler will never get invoked and the problem will never get addressed.   Tested on Ubuntu linux with Java 1.6.</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><a href="http://javajiggle.com/SCfiles/testjni.cpp.txt" title="native c++ code">C++ code</a></p>
<p style="margin-bottom: 0in"><a href="http://javajiggle.com/SCfiles/jniProgressBar.java.txt" title="java code">coresponding java code</a></p>
<p style="margin-bottom: 0in"><strong>To compile Java:</strong></p>
<p style="margin-bottom: 0in">javac *.java</p>
<p style="margin-bottom: 0in">jar cvf testJNI.jar *.class</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>To compile and link native code:</strong></p>
<p style="margin-bottom: 0in; font-style: normal">g++ -c -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/ -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/linux -o ../bin/jnitest.o testjni.cpp</p>
<p style="margin-bottom: 0in; font-style: normal">g++ ../bin/jnitest.o -L /usr/lib/jvm/java-6-sun-1.6.0.03/jre/lib/i386/client</p>
<p style="margin-bottom: 0in; font-style: normal"> -ljvm -o ../bin/jnitest.x</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /><title></title><meta name="GENERATOR" content="OpenOffice.org 2.3  (Linux)" /></p>
<style type="text/css"> 	<!-- 		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 	--> 	</style>
<p style="margin-bottom: 0in"><strong>Solutions</strong></p>
<p style="margin-bottom: 0in">It turns out that the solution is incredibly simple.   In fact this problem can be solved three different ways:</p>
<ol>
<li>
<p style="margin-bottom: 0in">remove signal handlers from your 	native code.</p>
</li>
<li>
<p style="margin-bottom: 0in">recompile with signal chaining 	library.</p>
</li>
<li>
<p style="margin-bottom: 0in">set LD_PRELOAD env. variable prior 	to launching the program.</p>
</li>
</ol>
<p><img src="http://javajiggle.com/SCfiles/withljsig.png" height="375" width="500" /></p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>Signal Chaining</strong></p>
<p style="margin-bottom: 0in">Using signal chaining requires no code modification and is explained here:</p>
<p style="margin-bottom: 0in"><a href="http://java.sun.com/j2se/1.4.2/docs/guide/vm/signal-chaining.html">http://java.sun.com/j2se/1.4.2/docs/guide/vm/signal-chaining.html</a></p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in"><strong>How to fix Example above.</strong></p>
<p style="margin-bottom: 0in">Fortunately there is a fine signal chaining library provided for free by Sun<sup>TM</sup>.</p>
<p style="margin-bottom: 0in">Here is how I linked the attached sample code and enabled signal chaining.   The part in red is needed to enable the signal chaining, which solves the problem.     Java code does not need to be recompiled.</p>
<p style="margin-bottom: 0in">&nbsp;</p>
<p style="margin-bottom: 0in; font-style: normal">g++ -c -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/ -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/linux -o ../bin/jnitest.o testjni.cpp</p>
<p style="margin-bottom: 0in; font-style: normal"><span style="font-style: normal">g++ ../bin/jnitest.o -L /usr/lib/jvm/java-6-sun-1.6.0.03/jre/lib/i386/client</span><font color="#800000"><strong>-ljsig</strong></font> -ljvm -o ../bin/jnitest.x</p>
<p style="margin-bottom: 0in; font-style: normal">&nbsp;</p>
<p style="margin-bottom: 0in; font-style: normal">Any questions, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Firefox buttons in your JTabbedPane. (Example)</title>
		<link>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/</link>
		<comments>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 02:09:35 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/</guid>
		<description><![CDATA[For my last post I showed how easy it is to add components to the tabs in JTabbedPane.   Someone replied that the example was a bit primitive and asked me how to add buttons that look similar to the Firefox tab buttons.   Below is an example that  uses Fixfox buttons.
import [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">For my last post I showed how easy it is to add components to the tabs in JTabbedPane.<span>   </span>Someone replied that the example was a bit primitive and asked me how to add buttons that look similar to the Firefox tab buttons.<span>   </span>Below is an example that  uses Fixfox buttons.</p>
<p><em>import java.awt.*;<br />
import java.awt.event.*;<br />
import java.net.*;</em></p>
<p><em>import javax.swing.*;</em></p>
<p><em>public class JtabbedCloseExample extends JFrame {</em></p>
<blockquote><p><em>JTabbedPane jtabbedPane;</em></p>
<p><em>class TabButton extends JPanel implements ActionListener {</em></p>
<blockquote><p><em> ImageIcon reg = null;<br />
ImageIcon over = null;public TabButton( String label ){</em></p>
<blockquote><p><em> super(new FlowLayout());add( new JLabel( label ));<br />
try{</em></p>
<blockquote><p><em> // load firefox buttons<br />
reg = new ImageIcon(new URL(&#8221;http://javajiggle.com/images/regular.JPG&#8221;));<br />
over = new ImageIcon(new URL(&#8221;http://javajiggle.com/images/hoverOver.JPG&#8221;));</em></p></blockquote>
<p><em>} catch( Exception e ){</em></p>
<blockquote><p><em> e.printStackTrace();</em></p></blockquote>
<p><em>}<br />
setOpaque(false);<br />
final TabButton self = this;<br />
final JButton button = new JButton(reg);<br />
button.setOpaque(false);<br />
button.setRolloverIcon(over);<br />
button.setPressedIcon(over);<br />
button.setBorderPainted( false );<br />
button.setContentAreaFilled(false);<br />
button.addActionListener( this );<br />
add( button );</em></p></blockquote>
<p><em>}</em></p>
<p><em>public void actionPerformed(ActionEvent ae ){</em></p>
<blockquote><p><em> // close the tab which was clicked<br />
jtabbedPane.remove(jtabbedPane.indexOfTabComponent( this ) );</em></p></blockquote>
<p><em>}</em></p></blockquote>
<p><em>}</em></p>
<p><em>public JtabbedCloseExample() {</em></p>
<blockquote><p><em>jtabbedPane = new JTabbedPane();<br />
jtabbedPane.add(new JTextArea(&#8221;Tab 1 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.add(new JTextArea(&#8221;Tab 2 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.add(new JTextArea(&#8221;Tab 3 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.setTabComponentAt( 0, new TabButton(&#8221;Tab 1&#8243;) );<br />
jtabbedPane.setTabComponentAt( 1, new TabButton(&#8221;Tab 2&#8243;) );<br />
jtabbedPane.setTabComponentAt( 2, new TabButton(&#8221;Tab 3&#8243;) );<br />
getContentPane().add( jtabbedPane );<br />
setSize(new Dimension(300, 200));<br />
setVisible(true);</em></p></blockquote>
<p><em>}</em></p>
<p><em>public static void main ( String [] args ){</em></p>
<blockquote><p><em> new JtabbedCloseExample();</em></p></blockquote>
<p><em>}</em></p></blockquote>
<p><em>}</em></p>
<p><img src="http://javajiggle.com/images/firefoxButtons.PNG" alt="JTabbedPane using firefox buttons." height="200" width="300" /></p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>JDK 6 Rules!  Adding components to the tabs on JTabbedPane is now a breeze. Check it out!</title>
		<link>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/</link>
		<comments>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 02:36:44 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/</guid>
		<description><![CDATA[As you already know, JTabbedPane provides a convenient way for components such as JPanels to share the same real estate on the screen.    It also allows a user to select a desired panel by clicking on a tab.   If you&#8217;ve ever used a Firefox browser than you truly appreciate tabbed [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><img src="https://duke.dev.java.net/images/comfyChair/ComfyChairRadSmall.jpg" alt="JDK 6 Rules!" align="left" height="270" width="270" />As you already know, JTabbedPane provides a convenient way for components such as JPanels to share the same real estate on the screen.<span>    </span>It also allows a user to select a desired panel by clicking on a tab.<span>   </span>If you&#8217;ve ever used a Firefox browser than you truly appreciate tabbed panes.<span>   </span>Firefox displays each web page in a separate tab, allowing a user to switch between pages by clicking on a corresponding tab.</p>
<p class="MsoNormal"><o:p> </o:p>Up until JDK 1.6, we had limited control of what was rendered on a tab itself.<span>   </span>In particular we were limited to the label, the icon and a tooltip.<span>   </span>In other words we could not place a button on a tab itself as is done in a Firefox browser.<span>  </span><span> </span>Many improvised by manually drawing components and simulating their behavior.<span>   </span>Although this solution worked great, the code was way too complicated for a simple task.  With JDK 6 came three new methods which completely changed the behavior of a tabbed pane.   You can now set componets on a tab itself.  In addition, swing will render these components for you.   All possible with three methods below:</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>public void setTabComponentAt(int index, Component component)</strong> – allows you to add a component such as a button to a tab.<span>   </span>The rendering and the event handling is done for you by swing.<span>   </span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>public Component getTabComponentAt(int index)</strong> – gets a component for a corresponding index that was set by a setTabComponentAt(..) method above.<span>   </span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>public int indexOfTabComponent(Component tabComponent)</strong> – reverse lookup, that returns an index of a tab that corresponds to the tab component set by a setTabComponentAt(..) method above.<span>   </span>This is particularly handy if you are using a button to remove a corresponding tab from a tabbed pane.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Below is a simple example that illustrates how to implement a Firefox style close button on the individual tab.<span>   </span>Keep in mind this example won’t work on versions prior to JDK 1.6!</p>
<pre><o:p> </o:p></pre>
<blockquote>
<p class="MsoNormal"><strong>import java.awt.*;</strong></p>
<p class="MsoNormal"><strong>import java.awt.event.*;</strong></p>
<p class="MsoNormal"><strong><o:p> </o:p></strong></p>
<p class="MsoNormal"><strong>import javax.swing.*;</strong></p>
<p class="MsoNormal"><strong><o:p> </o:p></strong></p>
<p class="MsoNormal"><strong>public class JtabbedCloseExample extends JFrame {</strong></p>
<p class="MsoNormal"><strong><span>    </span></strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>    </span>JTabbedPane jtabbedPane;</strong></p>
<p class="MsoNormal"><strong><span>    </span>class TabButton extends JPanel implements ActionListener {</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>        </span>public TabButton( String label ){</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>         </span><span>   </span>super(new FlowLayout());</strong></p>
<p class="MsoNormal"><strong><span>            </span>add( new JLabel( label ));</strong></p>
<p class="MsoNormal"><strong><span>            </span>JButton button = new JButton(&#8221;X&#8221;);</strong></p>
<p class="MsoNormal"><strong><span>            </span>button.addActionListener( this );</strong></p>
<p class="MsoNormal"><strong><span>            </span>button.setMargin(new Insets(0,0,0,0));</strong></p>
<p class="MsoNormal"><strong><span>            </span>add( button );<span>            </span></strong></p>
</blockquote>
</blockquote>
<p class="MsoNormal"><strong><span>     </span><span>   </span>        }</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>        </span>public void actionPerformed(ActionEvent ae ){</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>            </span>// close the tab which was clicked<br />
<span></span>jtabbedPane.remove(jtabbedPane.indexOfTabComponent( this ) );</strong></p></blockquote>
<p class="MsoNormal"><strong><span>        </span>}</strong></p>
</blockquote>
<p class="MsoNormal"><strong><span>    </span>}</strong></p>
<p class="MsoNormal"><strong><span>    </span></strong></p>
<p class="MsoNormal"><strong><span>    </span></strong></p>
<p class="MsoNormal"><strong><span>    </span>public JtabbedCloseExample() {</strong></p>
<p class="MsoNormal"><strong><span>       </span></strong></p>
<blockquote>
<p class="MsoNormal"><strong><span>       </span><span> </span>jtabbedPane = new JTabbedPane();<span>   </span></strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.add(new JTextArea(&#8221;Tab 1 text area&#8221;));<span>        </span></strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.add(new JTextArea(&#8221;Tab 2 text area&#8221;));</strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.add(new JTextArea(&#8221;Tab 3 text area&#8221;));</strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.setTabComponentAt( 0, new TabButton(&#8221;Tab 1&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.setTabComponentAt( 1, new TabButton(&#8221;Tab 2&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span>        </span>jtabbedPane.setTabComponentAt( 2, new TabButton(&#8221;Tab 3&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span>        </span>getContentPane().add( jtabbedPane );</strong></p>
<p class="MsoNormal"><strong><span>        </span>setSize(new Dimension(300, 200));</strong></p>
<p class="MsoNormal"><strong><span>        </span>setVisible(true);</strong></p>
</blockquote>
<p class="MsoNormal"><strong><span>    </span>}</strong></p>
<p class="MsoNormal"><strong><o:p> </o:p></strong></p>
<p class="MsoNormal"><strong><span>    </span>public static void main ( String [] args ){</strong></p>
<p class="MsoNormal"><strong><span>        </span>    new JtabbedCloseExample();</strong></p>
<p class="MsoNormal"><strong><span>    </span>}</strong></p>
</blockquote>
<p class="MsoNormal"><strong>}</strong></p>
<p class="MsoNormal"><img src="file:///C:/DOCUME%7E1/P/LOCALS%7E1/Temp/moz-screenshot-1.jpg" /> <img src="http://javajiggle.com/images/jtabbedpane.JPG" align="left" height="204" width="303" /></p>
</blockquote>
<blockquote></blockquote>
<blockquote><p>This is not the best looking window.   However, it clearly illustrates the easy with which you can add your custom components to the tabs!</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to launch your Java™ RMI server and keep it running on Unix.</title>
		<link>http://javajiggle.com/2007/11/06/how-to-launch-your-java-server-and-keep-it-running-on-unix/</link>
		<comments>http://javajiggle.com/2007/11/06/how-to-launch-your-java-server-and-keep-it-running-on-unix/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 00:03:57 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://javajiggle.com/?p=8</guid>
		<description><![CDATA[I want to open this blog by discussing a few techniques of launching a java server and it’s side kick rmi-registry.   It is often that we (java developers) are faced with a requirement for launching a server and creating a failsafe mechanism.   To keep it short I will focus on the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="https://duke.dev.java.net/images/guitar/DukeAsKeith-daylightSmall.png" title="Duke on Java Server" alt="Duke on Java Server" align="left" height="163" width="179" />I want to open this blog by discussing a few techniques of launching a java server and it’s side kick rmi-registry.<span>   </span>It is often that we (java developers) are faced with a requirement for launching a server and creating a failsafe mechanism.<span>   </span>To keep it short I will focus on the Unix based platforms.<span>   </span>If there is strong interest I will write an article on how to achieve the same thing on windows.</p>
<p class="MsoNormal"><span>  </span><br />
Before I get into details, I want to make it clear what it is that we trying to achieve here.<span>    </span>Our goal is to launch a java program that acts as a server of some capacity.<span>   </span>To be more specific, let’s assume that we are launching an RMI based server.<span>   </span>In other words, we want to launch a java server that will run in the background waiting for requests.<span>   </span>Since we are running an RMI based server, we need to launch an rmiregistry along with it.<span>   </span>For those not familiar, rmiregistry is a separate executable usually provided along with a JDK.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>Option 1. Launch a server with “</strong><strong>&amp;</strong><strong>” at the end of the command string. </strong><span>  </span></p>
<p class="MsoNormal" style="text-indent: 0.5in"><strong><em>java myServer &amp;<o:p></o:p></em></strong></p>
<p class="MsoNormal">This actually would not work!<span>   </span>When launched this way the process runs in the background but is still associated with the tty (terminal) that launched it.<span>   </span>This means that when you exit a terminal by logging off, the terminal will send a signal to stop the jvm running your program.<span>   </span>Needless to say there is no failsafe mechanism here.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>Option 2.<span>   </span>Use a “</strong><em><strong>nohup</strong></em><strong>” command.</strong><span><strong>  </strong> </span></p>
<p class="MsoNormal" style="text-indent: 0.5in"><strong><em>nohup java myServer &amp;<o:p></o:p></em></strong></p>
<p class="MsoNormal">Nohup command is a unix command that runs a process and suspends a signal used by the terminal to stop all of the programs associated with it.<span>   </span>This allows the program to run even after a user logged off.<span>   </span>What happens if the jvm or the rmiregistry crashes?<span>   </span>Once the process is kicked off using a nohup command and you log off the system, there is nothing you can do if the process decides to retire other than logging back in and manually restarting the process.</p>
<p class="MsoNormal">Man page for <a href="http://bama.ua.edu/cgi-bin/man-cgi?nohup+1" title="http://bama.ua.edu/cgi-bin/man-cgi?nohup+1" target="_blank">nohup.</a></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><strong>Option 3. Use a “</strong><em><strong>cron job”</strong></em><strong> to test if the process is running, if not restart it.</strong><span>   </span></p>
<p class="MsoNormal"><span></span>Cron is a process dedicated to scheduling jobs.<span>   </span>Cron can be configured to run a process every 5 minutes or so.<span>   </span>This process will check if the server is running by trying to connect to it.<span>   </span>If it is not running cron restarts it.<span>   </span>The syntax is relatively simple.<span>    </span>However, this does require that you implement a whole separate program or a complicated shell script that will check the server status and restart it if necessary.<span>    </span>I will not get into a cron usage here and will instead jump ahead to the next solution which I believe is a bit more elegant.  <span></span></p>
<p class="MsoNormal">Man page for <a href="http://bama.ua.edu/cgi-bin/man-cgi?crontab+1" title="http://bama.ua.edu/cgi-bin/man-cgi?crontab+1" target="_blank">cron.</a></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><strong>Option 4.<span>  </span>Use an <em>“init daemon”</em> to start a java server and an rmi-registry.</strong><span>   </span></p>
<p class="MsoNormal">The Init daemon is an OS built-in process perfectly suited to launch any server application, including a java server and an RMI registry.<span>   </span>There are three basic features that the init daemon offers:</p>
<ol style="margin-top: 0in" start="1" type="1">
<li class="MsoNormal">The      init daemon will run your process in the background.</li>
<li class="MsoNormal">The      init daemon will restart your server if the server was restarted.<span>   </span></li>
<li class="MsoNormal">The      init daemon will monitor your process and re-spawn it if it crashed.<span>   </span></li>
<li class="MsoNormal">Init      daemon is very easy to configure.</li>
</ol>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Unfortunately init is often overlooked by many java developers not familiar with unix technologies.<span>   </span><br />
<strong><span style="font-size: 10pt">Note: The example below was tested on Sun Solaris.<span>   </span>Other unix based <st1:place w:st="on"><st1:city w:st="on">OSs</st1:city></st1:place> have similar technology in place, however the syntax may vary. </span></strong><span>  </span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The init daemon is configured using an <em>/etc/inittab</em> file.<span>   </span>Before we configure the init daemon, we need to create 4 launch scripts, two for the server and two for the rmi-registry.<span>   </span>Why two scripts?<span>  </span>Because whenever the process is launched by the init daemon it runs in the<strong> super user mode</strong>.<span>   </span>Running in a super user mode is highly undesirable and is often prohibited by the administrator.<span>    </span>For that purpose, we have two scripts, first will switch users and second will actually launch your server. Don’t worry; these scripts are one line each!<span>   </span></p>
<ol style="margin-top: 0in" start="1" type="1">
<li class="MsoNormal">Switch      users script for the rmi-registry (<em>/usr/bin/su_for_rmiregistry</em>):<br />
<strong>#!/bin/sh<br />
exec su –s pupsik –c “exec <em>/home/pupsik</em></strong><strong>/runRMI”<o:p></o:p></strong></li>
</ol>
<p class="MsoNormal" style="margin-left: 0.25in"><o:p> </o:p></p>
<ol style="margin-top: 0in" start="2" type="1">
<li class="MsoNormal">Switch      users script for the java server. (<em>/usr/bin/su_for_javaserver</em>)<br />
<strong>#!/bin/sh<br />
exec su –s pupsik –c “exec /home/pupsik/serverLaunchScript”</strong></li>
<li class="MsoNormal">Launch      an RMI registry. (<em>/home/pupsik/</em>runRMI).<br />
<strong>#!/bin/sh<br />
exec &lt;jre_path&gt;/bin/rmiregistry<br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--></strong></li>
<li class="MsoNormal">Launch      a java server.<span>   </span>(<em>/home/pupsik/serverLaunchScript</em>).<br />
<strong>#!/bin/sh<br />
exec &lt;jre_path&gt;/bin/java –cp &lt;classpath&gt; myServer.jar      &lt;server class&gt; </strong></li>
</ol>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">We used two unix commands above:<span>  </span>SU (switch user) that switched execution from a default super user to pupsik;<span>   </span>Exec that launched a command specified in the argument but did not create a new process and so that process id stays the same.<span>   </span>The text, heap and stack are replaced by the new program.<br />
Man page for <a href="http://bama.ua.edu/cgi-bin/man-cgi?su+1M" title="http://bama.ua.edu/cgi-bin/man-cgi?su+1M" target="_blank">su</a>.  Man page for <a href="http://ibgwww.colorado.edu/%7Elessem/psyc5112/usail/man/solaris/exec.1.html" title="http://ibgwww.colorado.edu/~lessem/psyc5112/usail/man/solaris/exec.1.html" target="_blank">exec</a>.<br />
<span style="color: red"></span><br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--></p>
<p class="MsoNormal"><span> </span>It is time to configure the init daemon.<span>   </span>You will need super user access to edit /etc/inittab that configures the init daemon.<span>   </span>Add two lines used to launch scripts 1 and 2 as follows:</p>
<p class="MsoNormal" style="margin-left: 0.5in"><strong><em>mm:234:respawn:/usr/bin/su_for_rmiregistry<br />
mc:234:respawn:/usr/bin/su_for_javaserver<br />
</em> <!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--><o:p></o:p></strong></p>
<p class="MsoNormal">This tells the init daemon to launch this process during system run levels 2, 3 and 4.<span>   </span>Respawn these processes if they crash.<span>   </span>Now tell the init daemon to reload the inittab file by typing <em><strong>init –q</strong>.</em><span>   </span>Check to see if your process is running using a ps command as follows:</p>
<p class="MsoNormal"><em><strong>ps –ef | grep pupsik</strong>.</em></p>
<p class="MsoNormal">For the ultimate test, use a kill command to terminate your java process.<span>   </span>Do the PS command again, the process should be respawned automatically by the init daemon.<span>  </span>(Make sure a new process has a different process id than the one you just killed.)</p>
<p class="MsoNormal">I’m sure you have questions.<span>  </span>Let me know.  <span></span></p>
<p class="MsoNormal">Man page for the<a href="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" title="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" target="_blank"> </a><a href="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" title="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" target="_blank">init</a><a href="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" title="http://www.ussg.iu.edu/usail/man/solaris/init.1.html" target="_blank">.</a></p>
<p class="MsoNormal">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p class="MsoNormal">I thought I should add this comment to the article:</p>
<p>Armin that’s a good point.  You are right, that if the RMI registry crashes, the java servers need to be reset.</p>
<p>This can be acomplished by launching the rmi registry and the java server(s) in one script.</p>
<p>1. The idea is that the rmi sever will be launched first.<br />
2. The java rmi server will be launched next.<br />
3. The launch script will than block using a wait command to check if rmi registry terminates.<br />
4. If wait unblocks, that means that rmi registry terminated.  When this happens, kill the java server(s) and repeat steps 1-3.</p>
<p>Below is a sample script.  You can tested it by killing the rmi registry and see it respawn both rmi registry and a java server.</p>
<p>Here is a sample: (Note: this should work but has not been tested for syntax)</p>
<blockquote><p><strong><em>#!/bin/sh</em></strong></p>
<p><strong><em>#kick off the rmi registry<br />
./rmiRegistry &amp;</em></strong></p>
<p><strong><em>#store the PID of the RMI registry<br />
rmiRegistryPID=$!<br />
echo Started RMI registry with PID= $rmiRegistryPID”</em></strong></p>
<p><strong><em>#kick off the java server in a script<br />
./startJavaServer &amp;</em></strong></p>
<p><strong><em>#store PID for java server<br />
javaServerPID=$!<br />
echo Started Java server with PID= $javaServerPID”</em></strong></p>
<p><strong><em>While [ 1 ]<br />
do<br />
</em></strong></p>
<blockquote><p><strong><em> wait $rmiRegistryPID</em></strong><br />
<strong><em> echo “RMI Registry (PID=$rmiRegistryPID ) terminated…”</em></strong><strong><em><font color="#ff0000"><br />
#kill -9 $javaServerPID<br />
<font color="#0000ff">kill -TERM $javaServerPID</font></font><br />
echo “Terminated Java Server”</em></strong></p>
<p><strong><em>./rmiRegistry &amp;<br />
rmiRegistryPID=$!<br />
echo “Respawned RMI registry with PID= $rmiRegistryPID”</em></strong></p>
<p><strong><em>./startJavaServer &amp;</em></strong><br />
<strong><em> javaServerPID=$!</em></strong><br />
<strong><em> echo “Respawned Java server with PID= $javaServerPID”</em></strong></p></blockquote>
<p><strong><em> done</em></strong></p></blockquote>
<p class="MsoNormal"> &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p class="MsoNormal"> startJavaServer script could look something like that:</p>
<blockquote>
<p class="MsoNormal"><strong>#!/bin/sh<br />
done=0<br />
while [ “$done” -eq 0 ]<br />
do<br />
</strong></p>
<blockquote><p><strong>         java &amp;</strong><br />
<strong>         jvmPID=$!</strong><br />
<strong>         trap “kill -TERM $jvmPID;done=1? TERM</strong><br />
<strong>         wait $jvmPID</strong><br />
<strong>         echo “respawning JVM”</strong></p></blockquote>
<p><strong> done</strong><br />
<strong> kill -TERM $jvmPID</strong></p></blockquote>
<p class="MsoNormal"><strong><br />
</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/11/06/how-to-launch-your-java-server-and-keep-it-running-on-unix/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Welcome to Java™ Jiggle!</title>
		<link>http://javajiggle.com/2007/11/06/welcome-to-java-jiggle/</link>
		<comments>http://javajiggle.com/2007/11/06/welcome-to-java-jiggle/#comments</comments>
		<pubDate>Tue, 06 Nov 2007 23:07:21 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://javajiggle.com/?p=7</guid>
		<description><![CDATA[&#160;
This blog is for those who Love working with Java™ technologies.   This is mostly technical blog for Java™ developers and those learning java.   Please share your thoughts and neat Java tricks.   Prepare for takeoff…
First Java™ article is coming soon (tomorrow).  Stay tunned.

Trademarks
Java™ and all Java-based marks are trademarks [...]]]></description>
			<content:encoded><![CDATA[<p class="post-body">&nbsp;</p>
<p class="MsoNormal">This blog is for those who Love working with Java™ technologies. <span>  </span>This is mostly technical blog for Java™ developers and those learning java. <span>  </span>Please share your thoughts and neat Java tricks.<span>   </span>Prepare for takeoff…<br />
First Java™ article is coming soon (tomorrow).  Stay tunned.</p>
<p><img src="https://duke.dev.java.net/images/JDK6/DukeP51thumbsup-small.png" title="prepare for take off" alt="prepare for take off" border="1" height="164" width="327" /></p>
<p class="MsoNormal"><strong>Trademarks</strong></p>
<p class="MsoNormal">Java™ and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc, in the <st1:country-region w:st="on"><st1:place w:st="on">United States</st1:place></st1:country-region> and other countries. Java™ Jiggle and the website javajiggle.com are independent of Sun Microsystems, Inc. and have no relationship, formal or informal.</p>
<p>About Sun™ and Java™ <a href="http://www.sun.com/policies/trademarks/" target="_blank">Trademarks</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/11/06/welcome-to-java-jiggle/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
