<?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>Jiramot.info &#187; java</title>
	<atom:link href="http://www.jiramot.info/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jiramot.info</link>
	<description>Developer&#039;s Life</description>
	<lastBuildDate>Mon, 06 Feb 2012 08:49:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>How to change Tomcat default character encoding</title>
		<link>http://www.jiramot.info/how-to-change-tomcat-default-character-encoding</link>
		<comments>http://www.jiramot.info/how-to-change-tomcat-default-character-encoding#comments</comments>
		<pubDate>Thu, 28 Jul 2011 07:39:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=1035</guid>
		<description><![CDATA[ export JAVA_OPTS="-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8" ]]></description>
			<content:encoded><![CDATA[<pre>
export JAVA_OPTS="-Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8"
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/how-to-change-tomcat-default-character-encoding/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set timeout in Apache HttpClient 4</title>
		<link>http://www.jiramot.info/how-to-set-timeout-in-apache-httpclient-4</link>
		<comments>http://www.jiramot.info/how-to-set-timeout-in-apache-httpclient-4#comments</comments>
		<pubDate>Mon, 06 Sep 2010 18:29:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=675</guid>
		<description><![CDATA[ HttpParams httpParams = new BasicHttpParams&#40;&#41;; HttpConnectionParams.setConnectionTimeout&#40;httpParams, connectionTimeoutMillis&#41;; HttpConnectionParams.setSoTimeout&#40;httpParams, socketTimeoutMillis&#41;; HttpClient httpClient = new DefaultHttpClient&#40;httpParams&#41; ]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">HttpParams httpParams <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BasicHttpParams<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
HttpConnectionParams.<span style="color: #006633;">setConnectionTimeout</span><span style="color: #009900;">&#40;</span>httpParams, connectionTimeoutMillis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
HttpConnectionParams.<span style="color: #006633;">setSoTimeout</span><span style="color: #009900;">&#40;</span>httpParams, socketTimeoutMillis<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
HttpClient httpClient <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #009900;">&#40;</span>httpParams<span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/how-to-set-timeout-in-apache-httpclient-4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set java Servlet timeout</title>
		<link>http://www.jiramot.info/how-to-set-java-servlet-timeout</link>
		<comments>http://www.jiramot.info/how-to-set-java-servlet-timeout#comments</comments>
		<pubDate>Mon, 06 Sep 2010 18:13:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=673</guid>
		<description><![CDATA[<p>Edit web.xml Specified the timeout value in “minute” , enclose with “session-config” element.</p> &#60;web-app ...&#62; &#60;session-config&#62; &#60;session-timeout&#62;20&#60;/session-timeout&#62; &#60;/session-config&#62; &#60;/web-app&#62; ]]></description>
			<content:encoded><![CDATA[<p>Edit web.xml<br />
Specified the timeout value in “minute” , enclose with “session-config” element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> ...<span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;session-timeout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>20<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-timeout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/session-config<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/how-to-set-java-servlet-timeout/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hacking Java ClassLoader</title>
		<link>http://www.jiramot.info/hacking-java-classloader</link>
		<comments>http://www.jiramot.info/hacking-java-classloader#comments</comments>
		<pubDate>Wed, 10 Feb 2010 04:15:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=505</guid>
		<description><![CDATA[<p>เนื่องจากว่าการทำงานส่วนมาก ต้องเขียน Business Logic ที่ Store Procedure ใน MS-SQL ทำให้เกิดความลำบากกับการทำ version ของโค๊ดเป็นอย่างมาก เลยอยากจะเขียน Script สั่นๆสักตัวเพื่อดูด Store Procedure มาเก็บเป้นไฟล์ให้ แล้วเราจะได้ดูสิ่งที่ต่างกัน (Diff) และเก็บลง SVN Server ส่วนตัว (ที่ทำงานใช้ CVS ซึ่งไม่ชอบเอาซะเลย)</p> <p>ก็เลยกะว่างานนี้เขียนสั่นๆ เอา Groovy ดีกว่า ก็เลยเริ่มจาก start groovy console (run&#62;cmd&#62;groovyConsole ) จากนั้นก็เริ่มเขียน code</p> <p>ทันใดนั้นนึงขึ้นมาได้ว่า กูจะ add jar ยังไงว่ะ ไม่ได้ start เป็น project เอ้ หรือว่า จะไปสร้าง classpath ยังไงละเนี่ย ก็เลยลองไปหาว่าเจ้า [...]]]></description>
			<content:encoded><![CDATA[<p>เนื่องจากว่าการทำงานส่วนมาก ต้องเขียน Business Logic ที่ Store Procedure ใน MS-SQL ทำให้เกิดความลำบากกับการทำ version ของโค๊ดเป็นอย่างมาก เลยอยากจะเขียน Script สั่นๆสักตัวเพื่อดูด Store Procedure มาเก็บเป้นไฟล์ให้ แล้วเราจะได้ดูสิ่งที่ต่างกัน (Diff) และเก็บลง SVN Server ส่วนตัว (ที่ทำงานใช้ CVS ซึ่งไม่ชอบเอาซะเลย)</p>
<p>ก็เลยกะว่างานนี้เขียนสั่นๆ เอา Groovy ดีกว่า ก็เลยเริ่มจาก start groovy console (run&gt;cmd&gt;groovyConsole ) จากนั้นก็เริ่มเขียน code</p>
<p>ทันใดนั้นนึงขึ้นมาได้ว่า กูจะ add jar ยังไงว่ะ ไม่ได้ start เป็น project เอ้ หรือว่า จะไปสร้าง classpath ยังไงละเนี่ย ก็เลยลองไปหาว่าเจ้า Groovy Console มันจะ add classpath ได้ไงบ้าง</p>
<p>ไปเจอที่บอกว่าให้แก groovy start แต่เอ้ มันไม่เท่อ่ะ ไหน groovy บอกว่าตัวเองเป็น dynamic ก็นึกว่ามันจะเอา env ข้างๆมาให้ด้วย เลยเอา jar ไปวางใน Folder ที่ทำการสั่ง groovyConsole (ในใจนึงว่ามันจะเหมือน grails console) เอ๋ ไม่ได้นี่นา</p>
<p>ไม่ไหวแระ ไหนๆก็ไหนๆ เลยลองเขียน ให้ JAVA มันโหลด JAR แบบ on the fly เลยละกัน ไม่อยากเขียน cmd ไป เรียก class</p>
<p><span id="more-505"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dynmicLoadJar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">File</span> libFolder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;libs&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;folder: &quot;</span> <span style="color: #339933;">+</span>libFolder.<span style="color: #006633;">getAbsolutePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003399;">File</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> jars <span style="color: #339933;">=</span> libFolder.<span style="color: #006633;">listFiles</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jar: &quot;</span> <span style="color: #339933;">+</span> jars<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	List<span style="color: #339933;">&lt;</span>URL<span style="color: #339933;">&gt;</span> urls <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>URL<span style="color: #339933;">&gt;</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><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> jars.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jars<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;.jar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				urls.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>jars<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">toURL</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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				e.<span style="color: #006633;">printStackTrace</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>
&nbsp;
	<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">URL</span> url <span style="color: #339933;">:</span> urls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dynamic add &quot;</span> <span style="color: #339933;">+</span> url.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; to class path&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">URLClassLoader</span> classLoader <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">URLClassLoader</span><span style="color: #009900;">&#41;</span> <span style="color: #003399;">ClassLoader</span>.<span style="color: #006633;">getSystemClassLoader</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;">Class</span> clazz <span style="color: #339933;">=</span> <span style="color: #003399;">URLClassLoader</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">Method</span> method <span style="color: #339933;">=</span> clazz.<span style="color: #006633;">getDeclaredMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;addURL&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">Class</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">URL</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			method.<span style="color: #006633;">setAccessible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			method.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>classLoader, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span>url<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">Thread</span>.<span style="color: #006633;">currentThread</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getContextClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;context.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error when dynamic load jar&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ex.<span style="color: #006633;">printStackTrace</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></div></div>

<p>ซึ่งจะทำการโหลด JAR ที่อยู่ใน folder libs มาให้เอง โดยใช้ reflection ยัดเข้าไปใน URLClassLoader ของ Java</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/hacking-java-classloader/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Java Serialize and De-serialize</title>
		<link>http://www.jiramot.info/java-serialize-and-de-serialize</link>
		<comments>http://www.jiramot.info/java-serialize-and-de-serialize#comments</comments>
		<pubDate>Wed, 01 Jul 2009 19:56:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=35</guid>
		<description><![CDATA[Introduction to Object Serialization <p>Java object serialization is used to persist Java objects to a file, database, network, process or any other system. Serialization flattens objects into an ordered, or serialized stream of bytes. The ordered stream of bytes can then be read at a later time, or in another environment, to recreate the [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction to Object Serialization</h2>
<p>Java object serialization is used to persist Java objects to a file, database, network, process or any other system. Serialization flattens objects into an ordered, or serialized stream of bytes. The ordered stream of bytes can then be read at a later time, or in another environment, to recreate the original objects.</p>
<p>Java serialization does not cannot occur for transient or static fields. Marking the field transient prevents the state from being written to the stream and from being restored during deserialization. Java provides classes to support writing objects to streams and restoring objects from streams. Only objects that support the java.io.<strong>Serializable</strong> interface or the java.io.<strong>Externalizable</strong> interface can be written to streams.<br />
<span class="style4">public interface Serializable </span></p>
<ul>
<li>The Serializable interface has no methods or fields. (Marker Interface)</li>
<li>Only objects of classes that implement java.io.Serializable interface can be serialized or deserialized</li>
</ul>
<p><span id="more-35"></span></p>
<h2>Transient Fields and Java Serialization</h2>
<p>The transient keyword is a modifier applied to instance variables in a class. It specifies that the variable is not part of the persistent state of the object and thus never saved during serialization.</p>
<p>You can use the transient keyword to describe temporary variables, or variables that contain local information,<br />
such as a process ID or a time lapse.</p>
<h2>Input and Output Object Streams</h2>
<p>ObjectOutputStream is the primary output stream class that implements the ObjectOutput interface for serializing objects. ObjectInputStream is the primary input stream class that implements the ObjectInput interface for deserializing objects.These high-level streams are each chained to a low-level stream, such as FileInputStream or FileOutputStream.<br />
The low-level streams handle the bytes of data. The writeObject method saves the state of the class by writing the individual fields to the ObjectOutputStream. The readObject method is used to deserialize the object from<br />
the object input stream.</p>
<p class="style4">Case 1: Below is an example that demonstrates object Serialization into a File</p>
<p><span class="style5"> <span style="font-weight: 400; font-style: italic; text-decoration: underline;">PersonDetails is the bean class that implements the Serializable interface </span></span></p>
<blockquote>
<pre>import java.io.Serializable;</pre>
<pre>public class PersonDetails implements Serializable {

	private String name;
	private int age;
	private String sex;
	public PersonDetails(String name, int age, String sex) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/java-serialize-and-de-serialize/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Read/Write Windows registry with Java</title>
		<link>http://www.jiramot.info/readwrite-windows-registry-with-java</link>
		<comments>http://www.jiramot.info/readwrite-windows-registry-with-java#comments</comments>
		<pubDate>Thu, 16 Apr 2009 08:16:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=231</guid>
		<description><![CDATA[เป็น Class Utils ที่เอาไว้เขียน/อ่าน system registry <p></p> import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.ArrayList; import java.util.List; import java.util.prefs.Preferences; public class WinRegistry { // inspired by // http://javabyexample.wisdomplug.com/java-concepts/34-core-java/62-java-registry-wrapper.html // http://www.snipcode.org/java/1-java/23-java-class-for-accessing-reading-and-writing-from-windows-registry.html // http://snipplr.com/view/6620/accessing-windows-registry-in-java/ public static final int HKEY_CURRENT_USER = 0x80000001; public static final int HKEY_LOCAL_MACHINE = 0x80000002; public static final int REG_SUCCESS = [...]]]></description>
			<content:encoded><![CDATA[<h1>เป็น Class  Utils ที่เอาไว้เขียน/อ่าน system registry</h1>
<p><span id="more-231"></span></p>
<pre>
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

public class WinRegistry {
    // inspired by
    // http://javabyexample.wisdomplug.com/java-concepts/34-core-java/62-java-registry-wrapper.html
    // http://www.snipcode.org/java/1-java/23-java-class-for-accessing-reading-and-writing-from-windows-registry.html
    // http://snipplr.com/view/6620/accessing-windows-registry-in-java/

    public static final int HKEY_CURRENT_USER = 0x80000001;
    public static final int HKEY_LOCAL_MACHINE = 0x80000002;
    public static final int REG_SUCCESS = 0;
    public static final int REG_NOTFOUND = 2;
    public static final int REG_ACCESSDENIED = 5;
    private static final int KEY_ALL_ACCESS = 0xf003f;
    private static final int KEY_READ = 0x20019;
    private static Preferences userRoot = Preferences.userRoot();
    private static Preferences systemRoot = Preferences.systemRoot();
    private static Class userClass = userRoot.getClass();
    private static Method regOpenKey = null;
    private static Method regCloseKey = null;
    private static Method regQueryValueEx = null;
    private static Method regEnumValue = null;
    private static Method regQueryInfoKey = null;
    private static Method regEnumKeyEx = null;
    private static Method regCreateKeyEx = null;
    private static Method regSetValueEx = null;
    private static Method regDeleteKey = null;
    private static Method regDeleteValue = null;

    static {
        try {
            regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey",
                    new Class[]{int.class, byte[].class, int.class});
            regOpenKey.setAccessible(true);
            regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey",
                    new Class[]{int.class});
            regCloseKey.setAccessible(true);
            regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx",
                    new Class[]{int.class, byte[].class});
            regQueryValueEx.setAccessible(true);
            regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue",
                    new Class[]{int.class, int.class, int.class});
            regEnumValue.setAccessible(true);
            regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1",
                    new Class[]{int.class});
            regQueryInfoKey.setAccessible(true);
            regEnumKeyEx = userClass.getDeclaredMethod(
                    "WindowsRegEnumKeyEx", new Class[]{int.class, int.class,
                        int.class});
            regEnumKeyEx.setAccessible(true);
            regCreateKeyEx = userClass.getDeclaredMethod(
                    "WindowsRegCreateKeyEx", new Class[]{int.class,
                        byte[].class});
            regCreateKeyEx.setAccessible(true);
            regSetValueEx = userClass.getDeclaredMethod(
                    "WindowsRegSetValueEx", new Class[]{int.class,
                        byte[].class, byte[].class});
            regSetValueEx.setAccessible(true);
            regDeleteValue = userClass.getDeclaredMethod(
                    "WindowsRegDeleteValue", new Class[]{int.class,
                        byte[].class});
            regDeleteValue.setAccessible(true);
            regDeleteKey = userClass.getDeclaredMethod(
                    "WindowsRegDeleteKey", new Class[]{int.class,
                        byte[].class});
            regDeleteKey.setAccessible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private WinRegistry() {
    }

    /**
     * Read a value from key and value name
     * @param hkey   HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
     * @param key
     * @param valueName
     * @return the value
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static String readString(int hkey, String key, String valueName)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        if (hkey == HKEY_LOCAL_MACHINE) {
            return readString(systemRoot, hkey, key, valueName);
        } else if (hkey == HKEY_CURRENT_USER) {
            return readString(userRoot, hkey, key, valueName);
        } else {
            throw new IllegalArgumentException("hkey=" + hkey);
        }
    }

    /**
     * Read value(s) and value name(s) form given key
     * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
     * @param key
     * @return the value name(s) plus the value(s)
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static Map readStringValues(int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        if (hkey == HKEY_LOCAL_MACHINE) {
            return readStringValues(systemRoot, hkey, key);
        } else if (hkey == HKEY_CURRENT_USER) {
            return readStringValues(userRoot, hkey, key);
        } else {
            throw new IllegalArgumentException("hkey=" + hkey);
        }
    }

    /**
     * Read the value name(s) from a given key
     * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
     * @param key
     * @return the value name(s)
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static List readStringSubKeys(int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        if (hkey == HKEY_LOCAL_MACHINE) {
            return readStringSubKeys(systemRoot, hkey, key);
        } else if (hkey == HKEY_CURRENT_USER) {
            return readStringSubKeys(userRoot, hkey, key);
        } else {
            throw new IllegalArgumentException("hkey=" + hkey);
        }
    }

    /**
     * Create a key
     * @param hkey  HKEY_CURRENT_USER/HKEY_LOCAL_MACHINE
     * @param key
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static void createKey(int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int[] ret;
        if (hkey == HKEY_LOCAL_MACHINE) {
            ret = createKey(systemRoot, hkey, key);
            regCloseKey.invoke(systemRoot, new Object[]{new Integer(ret[0])});
        } else if (hkey == HKEY_CURRENT_USER) {
            ret = createKey(userRoot, hkey, key);
            regCloseKey.invoke(userRoot, new Object[]{new Integer(ret[0])});
        } else {
            throw new IllegalArgumentException("hkey=" + hkey);
        }
        if (ret[1] != REG_SUCCESS) {
            throw new IllegalArgumentException("rc=" + ret[1] + "  key=" + key);
        }
    }

    /**
     * Write a value in a given key/value name
     * @param hkey
     * @param key
     * @param valueName
     * @param value
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static void writeStringValue(int hkey, String key, String valueName, String value)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        if (hkey == HKEY_LOCAL_MACHINE) {
            writeStringValue(systemRoot, hkey, key, valueName, value);
        } else if (hkey == HKEY_CURRENT_USER) {
            writeStringValue(userRoot, hkey, key, valueName, value);
        } else {
            throw new IllegalArgumentException("hkey=" + hkey);
        }
    }

    /**
     * Delete a given key
     * @param hkey
     * @param key
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static void deleteKey(int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int rc = -1;
        if (hkey == HKEY_LOCAL_MACHINE) {
            rc = deleteKey(systemRoot, hkey, key);
        } else if (hkey == HKEY_CURRENT_USER) {
            rc = deleteKey(userRoot, hkey, key);
        }
        if (rc != REG_SUCCESS) {
            throw new IllegalArgumentException("rc=" + rc + "  key=" + key);
        }
    }

    /**
     * delete a value from a given key/value name
     * @param hkey
     * @param key
     * @param value
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static void deleteValue(int hkey, String key, String value)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int rc = -1;
        if (hkey == HKEY_LOCAL_MACHINE) {
            rc = deleteValue(systemRoot, hkey, key, value);
        } else if (hkey == HKEY_CURRENT_USER) {
            rc = deleteValue(userRoot, hkey, key, value);
        }
        if (rc != REG_SUCCESS) {
            throw new IllegalArgumentException("rc=" + rc + "  key=" + key + "  value=" + value);
        }
    }

    // =====================
    private static int deleteValue(Preferences root, int hkey, String key, String value)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{
                    new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS)});
        if (handles[1] != REG_SUCCESS) {
            return handles[1];  // can be REG_NOTFOUND, REG_ACCESSDENIED
        }
        int rc = ((Integer) regDeleteValue.invoke(root,
                new Object[]{
                    new Integer(handles[0]), toCstr(value)
                })).intValue();
        regCloseKey.invoke(root, new Object[]{new Integer(handles[0])});
        return rc;
    }

    private static int deleteKey(Preferences root, int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int rc = ((Integer) regDeleteKey.invoke(root,
                new Object[]{new Integer(hkey), toCstr(key)})).intValue();
        return rc;  // can REG_NOTFOUND, REG_ACCESSDENIED, REG_SUCCESS
    }

    private static String readString(Preferences root, int hkey, String key, String value)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{
                    new Integer(hkey), toCstr(key), new Integer(KEY_READ)});
        if (handles[1] != REG_SUCCESS) {
            return null;
        }
        byte[] valb = (byte[]) regQueryValueEx.invoke(root, new Object[]{
                    new Integer(handles[0]), toCstr(value)});
        regCloseKey.invoke(root, new Object[]{new Integer(handles[0])});
        return (valb != null ? new String(valb).trim() : null);
    }

    private static Map readStringValues(Preferences root, int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        HashMap results = new HashMap();
        int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{
                    new Integer(hkey), toCstr(key), new Integer(KEY_READ)});
        if (handles[1] != REG_SUCCESS) {
            return null;
        }
        int[] info = (int[]) regQueryInfoKey.invoke(root,
                new Object[]{new Integer(handles[0])});

        int count = info[2]; // count
        int maxlen = info[3]; // value length max
        for (int index = 0; index &lt; count; index++) {
            byte[] name = (byte[]) regEnumValue.invoke(root, new Object[]{
                        new Integer(handles[0]), new Integer(index), new Integer(maxlen + 1)});
            String value = readString(hkey, key, new String(name));
            results.put(new String(name).trim(), value);
        }
        regCloseKey.invoke(root, new Object[]{new Integer(handles[0])});
        return results;
    }

    private static List readStringSubKeys(Preferences root, int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        List results = new ArrayList();
        int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{
                    new Integer(hkey), toCstr(key), new Integer(KEY_READ)
                });
        if (handles[1] != REG_SUCCESS) {
            return null;
        }
        int[] info = (int[]) regQueryInfoKey.invoke(root,
                new Object[]{new Integer(handles[0])});

        int count = info[2]; // count
        int maxlen = info[3]; // value length max
        for (int index = 0; index &lt; count; index++) {
            byte[] name = (byte[]) regEnumKeyEx.invoke(root, new Object[]{
                        new Integer(handles[0]), new Integer(index), new Integer(maxlen + 1)
                    });
            results.add(new String(name).trim());
        }
        regCloseKey.invoke(root, new Object[]{new Integer(handles[0])});
        return results;
    }

    private static int[] createKey(Preferences root, int hkey, String key)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        return (int[]) regCreateKeyEx.invoke(root,
                new Object[]{new Integer(hkey), toCstr(key)});
    }

    private static void writeStringValue(Preferences root, int hkey, String key, String valueName, String value)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException {
        int[] handles = (int[]) regOpenKey.invoke(root, new Object[]{
                    new Integer(hkey), toCstr(key), new Integer(KEY_ALL_ACCESS)});

        regSetValueEx.invoke(root,
                new Object[]{
                    new Integer(handles[0]), toCstr(valueName), toCstr(value)
                });
        regCloseKey.invoke(root, new Object[]{new Integer(handles[0])});
    }

    // utility
    private static byte[] toCstr(String str) {
        byte[] result = new byte[str.length() + 1];

        for (int i = 0; i &lt; str.length(); i++) {
            result[i] = (byte) str.charAt(i);
        }
        result[str.length()] = 0;
        return result;
    }
}</pre>
<p>ทดสอบโดย</p>
<pre>
public class WinRegistryTest {

    public static void main(String args[]) throws Exception {
        String value = "";

        // IE Download directory (HKEY_CURRENT_USER)
        value = WinRegistry.readString(
                WinRegistry.HKEY_CURRENT_USER,
                "Software\\Microsoft\\Internet Explorer",
                "Download Directory");
        System.out.println("IE Download directory = " + value);

        // Query for Acrobat Reader installation path (HKEY_LOCAL_MACHINE)
        value = WinRegistry.readString(
                WinRegistry.HKEY_LOCAL_MACHINE,
                "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\AcroRd32.exe",
                "");
        System.out.println("Acrobat Reader Path = " + value);

        // Loop through installed JRE and print the JAVA_HOME value
        // HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
        java.util.Map res1 = WinRegistry.readStringValues(
                WinRegistry.HKEY_LOCAL_MACHINE,
                "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
        System.out.println(res1.toString());

        java.util.List res2 = WinRegistry.readStringSubKeys(
                WinRegistry.HKEY_LOCAL_MACHINE,
                "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
        System.out.println(res2.toString());

        WinRegistry.createKey(
                WinRegistry.HKEY_CURRENT_USER, "SOFTWARE\\rgagnon.com");
        WinRegistry.writeStringValue(
                WinRegistry.HKEY_CURRENT_USER,
                "SOFTWARE\\rgagnon.com",
                "HowTo",
                "java");

//      WinRegistry.deleteValue(
//          WinRegistry.HKEY_CURRENT_USER,
//          "SOFTWARE\\rgagnon.com", "HowTo");
//      WinRegistry.deleteKey(
//          WinRegistry.HKEY_CURRENT_USER,
//          "SOFTWARE\\rgagnon.com\\");

        System.out.println("Done.");
    }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/readwrite-windows-registry-with-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Problem! Java Comm api on windows</title>
		<link>http://www.jiramot.info/problem-java-comm-api-on-windows</link>
		<comments>http://www.jiramot.info/problem-java-comm-api-on-windows#comments</comments>
		<pubDate>Tue, 24 Mar 2009 12:25:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=227</guid>
		<description><![CDATA[<p>วันนี้นั่งเอาโค๊ดเก่าที่เคยทำไว้สมัยเรียนมาลองเล่นใหม่ ซึ่งใช้ java ในการส่ง SMS หลักการมันก็คือ ใช้ Java Comm api ยิง AT Command ไปยังมือถือ เพื่อให้ส่ง SMS ให้</p> <p>เอาโค๊ดมารันแล้ว รันไม่ได้ เจอเจ้านี้เข้าไป</p> <p> Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path Caught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriver Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path Caught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriver Exception in thread [...]]]></description>
			<content:encoded><![CDATA[<p>วันนี้นั่งเอาโค๊ดเก่าที่เคยทำไว้สมัยเรียนมาลองเล่นใหม่ ซึ่งใช้ java ในการส่ง SMS หลักการมันก็คือ ใช้ Java Comm api ยิง AT Command ไปยังมือถือ เพื่อให้ส่ง SMS ให้</p>
<p>เอาโค๊ดมารันแล้ว รันไม่ได้ เจอเจ้านี้เข้าไป</p>
<blockquote><p>
Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path<br />
Caught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriver<br />
Error loading SolarisSerial: java.lang.UnsatisfiedLinkError: no SolarisSerialParallel in java.library.path<br />
Caught java.lang.UnsatisfiedLinkError: com.sun.comm.SolarisDriver.readRegistrySerial(Ljava/util/Vector;Ljava/lang/String;)I while loading driver com.sun.comm.SolarisDriver<br />
Exception in thread &#8220;main&#8221; java.lang.IllegalArgumentException: defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, or DISPOSE_ON_CLOSE<br />
        at javax.swing.JDialog.setDefaultCloseOperation(JDialog.java:705)<br />
        at org.jrm.sms.utils.PortChooser.finishGUI(CommPortOpen.java:344)<br />
        at org.jrm.sms.utils.PortChooser.<init>(CommPortOpen.java:268)<br />
        at org.jrm.sms.utils.CommPortOpen.<init>(CommPortOpen.java:114)<br />
        at org.jrm.sms.utils.CommPortOpen.main(CommPortOpen.java:103)</p>
</blockquote>
<p><span id="more-227"></span><br />
แล้ว solaris เกี่ยวอะไรด้วยอ่ะ เรารันบน windows นี่นา ก็เลยไปอ่านปัญหาในเน็ต ก็เลยพบว่า Java Comm api ที่ดาวโหลดมาเป็นเวอร์ชัน 3 ซึ่ง SUN เอาไม่ทำ Support windows แล้ว  งานเข้าหละทีนี้</p>
<p>ตอนเลือกดาวโหลดมาก็ แอบเห็นว่าไม่มี windows version ก็เลยไปเลือกเป็น Generic version พอเอามา แตก jar ก็เจอ Solaris lib แทน </p>
<p>สุดท้าย ต้องกลับไปหา api version 2 แทน<br />
comm.jar, win32comm.dll และ javax.comm.properties </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/problem-java-comm-api-on-windows/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Object serialization in groovy</title>
		<link>http://www.jiramot.info/object-serialization-in-groovy</link>
		<comments>http://www.jiramot.info/object-serialization-in-groovy#comments</comments>
		<pubDate>Wed, 18 Mar 2009 04:10:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=225</guid>
		<description><![CDATA[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // put this in a file named Name.groovy and compile using groovyc class Name implements Serializable &#123; def fname, lname &#125; // Now run the code below to see the Object serialization and de-serialization. def [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// put this in a file named Name.groovy and compile using groovyc </span>
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Name</span> <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span>  <span style="color: #009900;">&#123;</span>
   def fname, lname
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Now run the code below to see the Object serialization and de-serialization.</span>
def test1<span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Name</span><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;fn1&quot;</span>, lname <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;ln1&quot;</span><span style="color: #009900;">&#41;</span>
def test2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Name</span><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;fn2&quot;</span>, lname<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;ln2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">//serialization</span>
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C:/config.txt&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">withObjectOutputStream</span> <span style="color: #009900;">&#123;</span> out <span style="color: #339933;">-&gt;</span>
    out <span style="color: #339933;">&lt;&lt;</span> test1
    out <span style="color: #339933;">&lt;&lt;</span> test2
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//de-serialization</span>
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;C:/config.txt&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">withObjectInputStream</span> <span style="color: #009900;">&#123;</span> instream <span style="color: #339933;">-&gt;</span>
    instream.<span style="color: #006633;">eachObject</span> <span style="color: #009900;">&#123;</span> 
        println it
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Source: <a target="_blank" rel="nofollow" href="http://www.jiramot.info/goto/http://satish.name/?p=66" >satish.name</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/object-serialization-in-groovy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Print Text mode by using Java</title>
		<link>http://www.jiramot.info/print-text-mode-by-using-java</link>
		<comments>http://www.jiramot.info/print-text-mode-by-using-java#comments</comments>
		<pubDate>Tue, 17 Mar 2009 15:24:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=223</guid>
		<description><![CDATA[<p>พอดีว่าต้องเขียน project ด้วย grails ที่ต้องทำการพิมพ์รายงานที่เป็น text mode เกิดมาก็พึ่งรู้นี่แหละว่า ไอเครื่องพิมพ์หัวเข็ม รุ่นเก่าๆ ที่มันมีปุ่มเปลี่ยน font มันคืออะไร ที่แท้มันก็คือ เอาไว้เปลี่ยน font เวลาเราจะพิมพ์เป็น text mode นั่นเอง</p> <p>วิธีการพิมพ์แบบ text mode ทดลองได้โดย</p> 1 c:&#62;echo &#34;test print&#34;&#62;lpt1 <p>โดยเครื่องพิมพ์ของเราจะต้องต่อที่พอร์ท lpt1 </p> <p>สำหรับ code java ก็ทำได้ง่ายโดยการเปลี่ยน OutputStream ให้เป็นเครื่องพิมพ์นั่นเอง</p> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [...]]]></description>
			<content:encoded><![CDATA[<p>พอดีว่าต้องเขียน project ด้วย grails ที่ต้องทำการพิมพ์รายงานที่เป็น text mode เกิดมาก็พึ่งรู้นี่แหละว่า ไอเครื่องพิมพ์หัวเข็ม รุ่นเก่าๆ ที่มันมีปุ่มเปลี่ยน font มันคืออะไร ที่แท้มันก็คือ เอาไว้เปลี่ยน font เวลาเราจะพิมพ์เป็น text mode นั่นเอง</p>
<p>วิธีการพิมพ์แบบ text mode ทดลองได้โดย</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">c<span style="color: #339933;">:&gt;</span>echo <span style="color: #0000ff;">&quot;test print&quot;</span><span style="color: #339933;">&gt;</span>lpt1</pre></td></tr></table></div>

<p>โดยเครื่องพิมพ์ของเราจะต้องต่อที่พอร์ท lpt1 </p>
<p>สำหรับ code java ก็ทำได้ง่ายโดยการเปลี่ยน OutputStream ให้เป็นเครื่องพิมพ์นั่นเอง</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> lpt <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: #000066; font-weight: bold;">void</span> main <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">FileOutputStream</span> os <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;LPT1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #666666; font-style: italic;">//wrap stream in &quot;friendly&quot; PrintStream</span>
                <span style="color: #003399;">PrintStream</span> ps <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintStream</span><span style="color: #009900;">&#40;</span>os<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                            <span style="color: #666666; font-style: italic;">//print text here</span>
                ps.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello world!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                            <span style="color: #666666; font-style: italic;">//form feed -- this is important</span>
                            <span style="color: #666666; font-style: italic;">//Without the form feed, the text</span>
                            <span style="color: #666666; font-style: italic;">//will simply sit in the print</span>
                            <span style="color: #666666; font-style: italic;">//buffer until something else</span>
                            <span style="color: #666666; font-style: italic;">//gets printed.</span>
                ps.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\f</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                            <span style="color: #666666; font-style: italic;">//flush buffer and close</span>
                ps.<span style="color: #006633;">close</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: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Exception occurred: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>ปล. Java เองก็มี Library ในการพิมพ์แบบ Text mode ที่ชื่อว่า RTextPrint ซึ่งราคา $50 USD ^^<br />
ปล.2 บังเอินว่างานที่ทำดันเป็นทำเวปซ่ะนี่ เลยใช้ไม่ได้ เพราะต้องพิมพ์ที่เครื่องของผู้ใช้งาน ไม่ได้พิมพ์โดยเซอร์เวอร์ คงต้องลองหา การพิมพ์ Text จาก Browser โดยให้สามารถปรับ line space ได้แทน </p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/print-text-mode-by-using-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get last day in current month using Java</title>
		<link>http://www.jiramot.info/how-to-get-last-day-in-current-month-using-java</link>
		<comments>http://www.jiramot.info/how-to-get-last-day-in-current-month-using-java#comments</comments>
		<pubDate>Fri, 19 Dec 2008 09:33:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.jiramot.info/?p=147</guid>
		<description><![CDATA[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 &#160; import java.util.Calendar; &#160; public class Main &#123; &#160; public static void main&#40;String&#91;&#93; args&#41; &#123; // [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><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: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Get a calendar instance</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #003399;">Calendar</span> calendar <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Get the last date of the current month. To get the last date for a</span>
    <span style="color: #666666; font-style: italic;">// specific month you can set the calendar month using calendar object</span>
    <span style="color: #666666; font-style: italic;">// calendar.set(Calendar.MONTH, theMonth) method.</span>
    <span style="color: #666666; font-style: italic;">//	</span>
    <span style="color: #000066; font-weight: bold;">int</span> lastDate <span style="color: #339933;">=</span> calendar.<span style="color: #006633;">getActualMaximum</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Set the calendar date to the last date of the month so then we can</span>
    <span style="color: #666666; font-style: italic;">// get the last day of the month</span>
    <span style="color: #666666; font-style: italic;">//</span>
    calendar.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span>, lastDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> lastDay <span style="color: #339933;">=</span> calendar.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DAY_OF_WEEK</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Print the current date and the last date of the month</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last Date: &quot;</span> <span style="color: #339933;">+</span> calendar.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// The lastDay will be in a value from 1 to 7 where 1 = monday and 7 =</span>
    <span style="color: #666666; font-style: italic;">// saturday respectively.</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last Day : &quot;</span> <span style="color: #339933;">+</span> lastDay<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jiramot.info/how-to-get-last-day-in-current-month-using-java/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

