﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-敬的世界-随笔分类-Java</title><link>http://www.blogjava.net/nikita/category/39100.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 09 Feb 2012 17:33:10 GMT</lastBuildDate><pubDate>Thu, 09 Feb 2012 17:33:10 GMT</pubDate><ttl>60</ttl><item><title>解决ServletUrlRenderer WARN警告</title><link>http://www.blogjava.net/nikita/archive/2012/02/09/369656.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Thu, 09 Feb 2012 04:42:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2012/02/09/369656.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/369656.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2012/02/09/369656.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/369656.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/369656.html</trackback:ping><description><![CDATA[
<img src ="http://www.blogjava.net/nikita/aggbug/369656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2012-02-09 12:42 <a href="http://www.blogjava.net/nikita/archive/2012/02/09/369656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Struts 2与AJAX（第一部分） </title><link>http://www.blogjava.net/nikita/archive/2011/08/07/355970.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Sun, 07 Aug 2011 14:36:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2011/08/07/355970.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/355970.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2011/08/07/355970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/355970.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/355970.html</trackback:ping><description><![CDATA[link to : <a href="/max/archive/2007/06/12/123682.html">http://www.blogjava.net/max/archive/2007/06/12/123682.html</a><img src ="http://www.blogjava.net/nikita/aggbug/355970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2011-08-07 22:36 <a href="http://www.blogjava.net/nikita/archive/2011/08/07/355970.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Fix Out of Memory errors by increasing available memory </title><link>http://www.blogjava.net/nikita/archive/2010/09/09/331477.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Thu, 09 Sep 2010 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2010/09/09/331477.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/331477.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2010/09/09/331477.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/331477.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/331477.html</trackback:ping><description><![CDATA[
		<div class="page-metadata" sizset="44" sizcache="3">
				<ul sizset="44" sizcache="3">
						<li class="page-metadata-item noprint" sizset="44" sizcache="3">
								<a class="page-metadata-icon page-restrictions hidden" id="content-metadata-page-restrictions" title="Page restrictions apply. Click the lock icon to view or edit the restriction." href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#" jquery1284000845122="38">
										<span>Page restrictions apply</span>
								</a>
						</li>
						<li class="page-metadata-modification-info" sizset="45" sizcache="3">Added by <a class="url fn confluence-userlink userlink-0" title="" href="http://confluence.atlassian.com/display/~edawson" jquery1284000845122="8" data-processed="true" data-username="edawson">Edwin Dawson [Atlassian Technical Writer]</a>, last edited by <a class="url fn confluence-userlink userlink-1" title="" href="http://confluence.atlassian.com/display/~amyers" jquery1284000845122="11" data-processed="true" data-username="amyers">Andrew Myers [Atlassian]</a> on Jun 16, 2010 <span class="noprint" sizset="47" sizcache="3"> (<a id="view-change-link" href="http://confluence.atlassian.com/pages/diffpages.action?pageId=165609660&amp;originalId=218277603">view change</a>)</span></li>
						<li class="show-hide-comment-link" sizset="48" sizcache="3">
								<a class="inline-control-link" id="show-version-comment" href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#" jquery1284000845122="45">show comment</a>
								<a class="inline-control-link" id="hide-version-comment" style="DISPLAY: none" href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#" jquery1284000845122="46">hide comment</a>
						</li>
				</ul>
				<div class="noteMacro" id="version-comment" style="DISPLAY: none">
						<strong>Comment:</strong> 256m is now the default MaxPermSize<br /></div>
		</div>
		<a class="assistive" href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#page-metadata-start">Go to start of metadata</a>
		<div class="assistive" id="page-metadata-end">
		</div>
		<fieldset class="hidden parameters" sizset="75" sizcache="0">
				<input title="browsePageTreeMode" type="hidden" value="view" />
		</fieldset>
		<div class="wiki-content" sizset="51" sizcache="3">
				<!-- wiki content -->
				<p>
						<font color="#006699">
								<b>I am getting Out of Memory errors, how can I allocate more memory to FishEye?</b>
						</font>
				</p>
				<p>Since the default memory setting usually is around 64MB or 128MB, you might have to adjust the settings to run a bigger FishEye instance with sufficient memory. <br /><b>On this page:</b><br /></p>
				<div sizset="51" sizcache="3">
						<ul sizset="51" sizcache="3">
								<li sizset="51" sizcache="3">
										<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryErrors">Out Of Memory Errors</a>
										<ul sizset="52" sizcache="3">
												<li sizset="52" sizcache="3">
														<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3AJavaHeapSpace">OutOfMemoryError: Java Heap Space</a>
												</li>
												<li sizset="53" sizcache="3">
														<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3APermGenspace%2CorPermanentGenerationSize">OutOfMemoryError: PermGen space, or Permanent Generation Size</a>
												</li>
												<li sizset="54" sizcache="3">
														<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3Aunabletocreatenewnativethread">OutOfMemoryError: unable to create new native thread</a>
												</li>
												<li sizset="55" sizcache="3">
														<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3AGCoverheadlimitexceeded">OutOfMemoryError: GC overhead limit exceeded</a>
												</li>
												<li sizset="56" sizcache="3">
														<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-java.lang.OutOfMemoryError%3Arequested32756bytesforChunkPool%3A%3Aallocate.Outofswapspace%3F">java.lang.OutOfMemoryError: requested 32756 bytes for ChunkPool::allocate. Out of swap space?</a>
												</li>
										</ul>
								</li>
						</ul>
				</div>
				<br class="atl-forced-newline" />
				<p>
				</p>
				<h3 sizset="57" sizcache="3">
						<a name="FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryErrors">
						</a>Out Of Memory Errors</h3>
				<p sizset="58" sizcache="3">There are a number of different memory errors that the JVM will throw. The most common are listed as follows.<br />In the following, you will be required to set your memory settings via your FISHEYE_OPTS <a class="external-link" href="http://confluence.atlassian.com/x/l4CDBQ" rel="nofollow">Environment Variables</a>.<br />You will need to restart your server after setting your FISHEYE_OPTS.</p>
				<p>After having set the FISHEYE_OPTS and restarting your server, go to Administration &gt; Sys Info/Support &gt; System Info, and check your JVM Input Arguments to ensure that your server is picking up your FISHEYE_OPTS as expected. <br class="atl-forced-newline" /></p>
				<h4 sizset="59" sizcache="3">
						<a name="FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3AJavaHeapSpace">
						</a>OutOfMemoryError: Java Heap Space</h4>
				<div class="panelMacro" sizset="60" sizcache="3">
						<table class="noteMacro" sizset="60" sizcache="3">
								<colgroup>
										<col width="24" />
										<col />
								</colgroup>
								<tbody sizset="60" sizcache="3">
										<tr sizset="60" sizcache="3">
												<td valign="top">
														<img height="16" alt="" src="http://confluence.atlassian.com/images/icons/emoticons/warning.gif" width="16" align="absMiddle" border="0" />
												</td>
												<td sizset="60" sizcache="3">If you are running Fisheye/Crucible as a windows service, increasing memory needs to be done in the wrapper.conf file. Refere to the <a class="external-link" href="http://confluence.atlassian.com/pages/viewpage.action?pageId=91554194" rel="nofollow">Can Fisheye be run as a Windows Service</a> for instructions.</td>
										</tr>
								</tbody>
						</table>
				</div>
				<p sizset="61" sizcache="3">To solve this error, you will need to add the argument <tt>-Xmx1024m</tt> to <a class="external-link" href="http://confluence.atlassian.com/x/l4CDBQ" rel="nofollow">FISHEYE_OPTS</a>, in addition to any argument you use to set the heap size. Often you need to increase the amount of memory allocated to fisheye during the initial scan and period and once this is completed you can reduce back down.</p>
				<div class="panel" style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px">
						<div class="panelContent">
								<p>FISHEYE_OPTS="-Xms128m <font color="red">-Xmx1024m</font> -XX:MaxPermSize=256m"</p>
						</div>
				</div>
				<p>After having set the FISHEYE_OPTS and restarting your server, go to Administration &gt; Sys Info/Support &gt; System Info, and check your JVM Input Arguments to ensure that your server is picking up your FISHEYE_OPTS as expected. <br class="atl-forced-newline" /></p>
				<h4 sizset="62" sizcache="3">
						<a name="FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3APermGenspace%2CorPermanentGenerationSize">
						</a>OutOfMemoryError: PermGen space, or Permanent Generation Size</h4>
				<p sizset="63" sizcache="3">
						<a name="FixOutofMemoryerrorsbyincreasingavailablememory-permgen">
						</a>If you get the error message: <tt>java.lang.OutOfMemoryError: PermGen space</tt> this means that you have exceeded Java's fixed 64MB block for loading class files. You will need to add the argument <tt>-XX:MaxPermSize=256m</tt> to <a class="external-link" href="http://confluence.atlassian.com/x/l4CDBQ" rel="nofollow">FISHEYE_OPTS</a>, in addition to any argument you use to set the heap size.</p>
				<div class="panel" style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px">
						<div class="panelContent">
								<p>FISHEYE_OPTS="-Xms128m -Xmx512m <font color="red">-XX:MaxPermSize=256m</font>"</p>
						</div>
				</div>
				<p>After having set the FISHEYE_OPTS and restarting your server, go to Administration &gt; Sys Info/Support &gt; System Info, and check your JVM Input Arguments to ensure that your server is picking up your FISHEYE_OPTS as expected. <br class="atl-forced-newline" /></p>
				<h4 sizset="65" sizcache="3">
						<a name="FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3Aunabletocreatenewnativethread">
						</a>OutOfMemoryError: unable to create new native thread</h4>
				<p>This error occurs when the operating system is unable to create new threads. This is due to the JVM Heap taking up the available RAM.<br /></p>
				<blockquote>
						<p>
								<cite>Big heaps take away from the space that can be allocated for the stack of a new thread</cite>
						</p>
				</blockquote>For Linux the maximum heap size of the JVM cannot be greater than 2GB. If you only have 2GB RAM in your server, it is <b>not</b> recommended to set the Max size of the JVM that high.<br />The size of the stack per thread can also contribute to this problem. The stack size can reduce the number of threads that can be created. 
<p></p><p sizset="66" sizcache="3">To fix this problem, you should reduce the size of your JVM Heap and also the size of the stack per thread.<br />The stack size can be changed with the following (example) parameter being added to your <a class="external-link" href="http://confluence.atlassian.com/x/l4CDBQ" rel="nofollow">FISHEYE_OPTS</a>:</p><div class="panel" style="BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px"><div class="panelContent"><p>FISHEYE_OPTS="-Xms128m -Xmx1024m -XX:MaxPermSize=256m <font color="red">-Xss512k</font>"</p></div></div><p sizset="67" sizcache="3">Please refer to <a class="external-link" href="http://goobsoft.homeip.net/Wiki.jsp?page=JavaDebianTuning" rel="nofollow">this guide</a> as a reference for JVM tuning.</p><p>After having set the FISHEYE_OPTS and restarting your server, go to Administration &gt; Sys Info/Support &gt; System Info, and check your JVM Input Arguments to ensure that your server is picking up your FISHEYE_OPTS as expected. <br class="atl-forced-newline" /><br class="atl-forced-newline" /></p><h4 sizset="68" sizcache="3"><a name="FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3AGCoverheadlimitexceeded"></a>OutOfMemoryError: GC overhead limit exceeded</h4><p>This error indicates that the JVM took too long to free up memory during its GC process. This error can be thrown from the Parallel or Concurrent collectors.<br /></p><blockquote><p>The parallel collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line.</p></blockquote>This kind of OutOfMemoryError can be caused if your java process is starting to use swapped memory for its heap. This will cause the JVM to take a lot longer than normal to perform normal GC operations. This can eventually cause a timeout to occur and cause this error. 
<p></p><p>To overcome this issue, you need to make sure that all processes can't allocate more memory than there is system memory. In practice this is impossible to do for all processes. At a minimum you should make sure that all your jvm's do not have a total maximum memory allocation than your normally available system memory.</p><p sizset="69" sizcache="3">Please refer to <a class="external-link" href="http://java.sun.com/javase/technologies/hotspot/gc/gc_tuning_6.html" rel="nofollow">this guide</a> for more information. <br class="atl-forced-newline" /><br class="atl-forced-newline" /></p><h4 sizset="70" sizcache="3"><a name="FixOutofMemoryerrorsbyincreasingavailablememory-java.lang.OutOfMemoryError%3Arequested32756bytesforChunkPool%3A%3Aallocate.Outofswapspace%3F"></a>java.lang.OutOfMemoryError: requested 32756 bytes for ChunkPool::allocate. Out of swap space?</h4><p sizset="71" sizcache="3">Essentially the native objects does not have enough memory to use. This is usually because you have allocated too much memory to your heap reducing the amount available for native objects. See <a class="external-link" href="http://www.codingthearchitecture.com/2008/01/14/jvm_lies_the_outofmemory_myth.html" rel="nofollow">this article</a>.</p><p>The solution is to reduce the amount of heap memory you have allocated. For example if you have set -Xmx4096, you should consider reducing this to -Xmx2048m.</p><p sizset="72" sizcache="3">Remember if you are using a 32bit JVM you cannot allocate more than -Xmx2048m for linux (and even less for windows). Using a 64 bit JVM can resolve this problem, but is not recommended for fisheye/crucible instances (refer to <a class="external-link" href="http://confluence.atlassian.com/display/FISHEYE/System+Requirements" rel="nofollow">System Requirements</a>).</p><p sizset="73" sizcache="3"><br class="atl-forced-newline" /><br class="atl-forced-newline" />Read the <a title="Tuning FishEye" href="http://confluence.atlassian.com/display/FISHEYE/Tuning+FishEye">Tuning FishEye</a> page for more detail on adjusting resource limits and performance settings in FishEye.<br /></p></div>
		<!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
         <rdf:Description
    rdf:about="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory"
    dc:identifier="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory"
    dc:title="Fix Out of Memory errors by increasing available memory"
    trackback:ping="http://confluence.atlassian.com/rpc/trackback/165609660"/>
</rdf:RDF>
-->
		<br />源自 ：<a href="http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3Aunabletocreatenewnativethread">http://confluence.atlassian.com/display/FISHEYE/Fix+Out+of+Memory+errors+by+increasing+available+memory#FixOutofMemoryerrorsbyincreasingavailablememory-OutOfMemoryError%3Aunabletocreatenewnativethread</a>,<br /><br /><img src ="http://www.blogjava.net/nikita/aggbug/331477.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2010-09-09 10:56 <a href="http://www.blogjava.net/nikita/archive/2010/09/09/331477.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Problems Opening an Editor in Eclipse</title><link>http://www.blogjava.net/nikita/archive/2010/06/21/324030.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Mon, 21 Jun 2010 01:55:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2010/06/21/324030.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/324030.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2010/06/21/324030.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/324030.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/324030.html</trackback:ping><description><![CDATA[
		<div class="postText">
				<p>I used to be able to open *.sql files in my Eclipse text editor. For some reason that stopped working this morning: </p>
				<p>    Invalid menu handle. </p>
				<p>
						<a href="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/InvalidMenuHandle_2.png">
								<img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="64" alt="InvalidMenuHandle" src="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/InvalidMenuHandle_thumb.png" width="244" border="0" />
						</a>
				</p>
				<p>    Problems opening an editor... unable to open external editor. </p>
				<p>
						<a href="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/ProblemsOpeningEditor_2.png">
								<img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="121" alt="ProblemsOpeningEditor" src="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/ProblemsOpeningEditor_thumb.png" width="244" border="0" />
						</a>
				</p>
				<p>To fix this behavior for ALL files of a certain extension: </p>
				<p>    Window &gt; Preferences - </p>
				<p>... and on the Preferences Tree: </p>
				<p>    General &gt; Editors &gt; File Associations - </p>
				<p>... add your File Type extension, for example *.sql </p>
				<p>... and finally add your Associated Editor. I chose "internal editors" - "text editor" </p>
				<p>
						<a href="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/TextEditorPreferences_2.png">
								<img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="244" alt="TextEditorPreferences" src="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/TextEditorPreferences_thumb.png" width="233" border="0" />
						</a>
				</p>
				<p>You can also change the behavior "temporarily" for a single file. Right click the file, click "Open With..." and select the editor... </p>
				<p>
						<a href="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/OpenWith_2.png">
								<img style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height="244" alt="OpenWith" src="http://www.davestone.net/blog/image.axd?picture=WindowsLiveWriter/ProblemsOpeninganEditorinEclipse_7B37/OpenWith_thumb.png" width="187" border="0" />
						</a>
				</p>
		</div>
<img src ="http://www.blogjava.net/nikita/aggbug/324030.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2010-06-21 09:55 <a href="http://www.blogjava.net/nikita/archive/2010/06/21/324030.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>The Java serialization algorithm revealed</title><link>http://www.blogjava.net/nikita/archive/2010/04/10/317951.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Sat, 10 Apr 2010 09:20:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2010/04/10/317951.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/317951.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2010/04/10/317951.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/317951.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/317951.html</trackback:ping><description><![CDATA[
		<p>
				<em>Serialization</em> is the process of saving an object's state to a sequence of bytes; <em>deserialization</em> is the process of rebuilding those bytes into a live object. The Java Serialization API provides a standard mechanism for developers to handle object serialization. In this tip, you will see how to serialize an object, and why serialization is sometimes necessary. You'll learn about the serialization algorithm used in Java, and see an example that illustrates the serialized format of an object. By the time you're done, you should have a solid knowledge of how the serialization algorithm works and what entities are serialized as part of the object at a low level.</p>
		<h2>Why is serialization required?</h2>
		<p>In today's world, a typical enterprise application will have multiple components and will be distributed across various systems and networks. In Java, everything is represented as objects; if two Java components want to communicate with each other, there needs be a mechanism to exchange data. One way to achieve this is to define your own protocol and transfer an object. This means that the receiving end must know the protocol used by the sender to re-create the object, which would make it very difficult to talk to third-party components. Hence, there needs to be a generic and efficient protocol to transfer the object between components. Serialization is defined for this purpose, and Java components use this protocol to transfer objects.</p>
		<p>Figure 1 shows a high-level view of client/server communication, where an object is transferred from the client to the server through serialization.</p>
		<a href="http://www.javaworld.com/javaworld/jw-05-2009/images/jtip050709-fig1.gif">
				<img height="156" alt="A high-level view of serialization in action" src="http://www.javaworld.com/javaworld/jw-05-2009/images/jtip050709-fig1_thumb.gif" width="350" />
		</a>
		<h4>Figure 1. A high-level view of serialization in action (click to enlarge)</h4>
		<h2>How to serialize an object</h2>
		<p>In order to serialize an object, you need to ensure that the class of the object implements the <code>java.io.Serializable</code> interface, as shown in Listing 1.</p>
		<h4>Listing 1. Implementing Serializable</h4>
		<pre>
				<div class="codeblock">
						<code>import java.io.Serializable;<br /><br />class TestSerial implements Serializable {<br />	public byte version = 100;<br />	public byte count = 0;<br />}</code>
				</div>
		</pre>
		<p>In Listing 1, the only thing you had to do differently from creating a normal class is implement the <code>java.io.Serializable</code> interface. The <code>Serializable</code> interface is a marker interface; it declares no methods at all. It tells the serialization mechanism that the class can be serialized.</p>
		<p>Now that you have made the class eligible for serialization, the next step is to actually serialize the object. That is done by calling the <code>writeObject()</code> method of the <code>java.io.ObjectOutputStream</code> class, as shown in Listing 2.</p>
		<h4>Listing 2. Calling writeObject()</h4>
		<pre>
				<div class="codeblock">
						<code>public static void main(String args[]) throws IOException {<br />	FileOutputStream fos = new FileOutputStream("temp.out");<br />	ObjectOutputStream oos = new ObjectOutputStream(fos);<br />	TestSerial ts = new TestSerial();<br />	oos.writeObject(ts);<br />	oos.flush();<br />	oos.close();<br />}</code>
				</div>
		</pre>
		<p>Listing 2 stores the state of the <code>TestSerial</code> object in a file called <code>temp.out</code>. <code>oos.writeObject(ts);</code> actually kicks off the serialization algorithm, which in turn writes the object to <code>temp.out</code>.</p>
		<p>To re-create the object from the persistent file, you would employ the code in Listing 3.</p>
		<h4>Listing 3. Recreating a serialized object</h4>
		<pre>
				<div class="codeblock">
						<code>public static void main(String args[]) throws IOException {<br />	FileInputStream fis = new FileInputStream("temp.out");<br />	ObjectInputStream oin = new ObjectInputStream(fis);<br />	TestSerial ts = (TestSerial) oin.readObject();<br />	System.out.println("version="+ts.version);<br />}</code>
				</div>
		</pre>
		<p>In Listing 3, the object's restoration occurs with the <code>oin.readObject()</code> method call. This method call reads in the raw bytes that we previously persisted and creates a live object that is an exact replica of the original object graph. Because <code>readObject()</code> can read any serializable object, a cast to the correct type is required. </p>
		<p>Executing this code will print <code>version=100</code> on the standard output.</p>
		<h2>The serialized format of an object</h2>
		<p>What does the serialized version of the object look like? Remember, the sample code in the previous section saved the serialized version of the <code>TestSerial</code> object into the file <code>temp.out</code>. Listing 4 shows the contents of <code>temp.out</code>, displayed in hexadecimal. (You need a hexadecimal editor to see the output in hexadecimal format.)</p>
		<h4>Listing 4. Hexadecimal form of TestSerial</h4>
		<pre>
				<div class="codeblock">
						<code>AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65<br />73 74 A0 0C 34 00 FE B1 DD F9 02 00 02 42 00 05<br />63 6F 75 6E 74 42 00 07 76 65 72 73 69 6F 6E 78<br />70 00 64</code>
				</div>
		</pre>
		<p>If you look again at the actual <code>TestSerial</code> object, you'll see that it has only two byte members, as shown in Listing 5.</p>
		<h4>Listing 5. TestSerial's byte members</h4>
		<pre>
				<div class="codeblock">
						<code>	public byte version = 100;<br />	public byte count = 0;</code>
				</div>
		</pre>
		<p>The size of a byte variable is one byte, and hence the total size of the object (without the header) is two bytes. But if you look at the size of the serialized object in Listing 4, you'll see 51 bytes. Surprise! Where did the extra bytes come from, and what is their significance? They are introduced by the serialization algorithm, and are required in order to to re-create the object. In the next section, you'll explore this algorithm in detail.</p>
		<h2>Java's serialization algorithm</h2>
		<p>By now, you should have a pretty good knowledge of how to serialize an object. But how does the process work under the hood? In general the serialization algorithm does the following:</p>
		<ul>
				<li>It writes out the metadata of the class associated with an instance. 
</li>
				<li>It recursively writes out the description of the superclass until it finds <code>java.lang.object</code>. 
</li>
				<li>Once it finishes writing the metadata information, it then starts with the actual data associated with the instance. But this time, it starts from the topmost superclass. 
</li>
				<li>It recursively writes the data associated with the instance, starting from the least superclass to the most-derived class. </li>
		</ul>
		<p>I've written a different example object for this section that will cover all possible cases. The new sample object to be serialized is shown in Listing 6.</p>
		<h4>Listing 6. Sample serialized object</h4>
		<pre>
				<div class="codeblock">
						<code>class parent implements Serializable {<br />	int parentVersion = 10;<br />}<br /><br />class contain implements Serializable{<br />	int containVersion = 11;<br />}<br />public class SerialTest extends parent implements Serializable {<br />	int version = 66;<br />	contain con = new contain();<br /><br />	public int getVersion() {<br />		return version;<br />	}<br />	public static void main(String args[]) throws IOException {<br />		FileOutputStream fos = new FileOutputStream("temp.out");<br />		ObjectOutputStream oos = new ObjectOutputStream(fos);<br />		SerialTest st = new SerialTest();<br />		oos.writeObject(st);<br />		oos.flush();<br />		oos.close();<br />	}<br />}</code>
				</div>
		</pre>
		<p>This example is a straightforward one. It serializes an object of type <code>SerialTest</code>, which is derived from <code>parent</code> and has a container object, <code>contain</code>. The serialized format of this object is shown in Listing 7.</p>
		<h4>Listing 7. Serialized form of sample object</h4>
		<pre>
				<div class="codeblock">
						<code>AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65<br />73 74 05 52 81 5A AC 66 02 F6 02 00 02 49 00 07<br />76 65 72 73 69 6F 6E 4C 00 03 63 6F 6E 74 00 09<br />4C 63 6F 6E 74 61 69 6E 3B 78 72 00 06 70 61 72<br />65 6E 74 0E DB D2 BD 85 EE 63 7A 02 00 01 49 00<br />0D 70 61 72 65 6E 74 56 65 72 73 69 6F 6E 78 70<br />00 00 00 0A 00 00 00 42 73 72 00 07 63 6F 6E 74<br />61 69 6E FC BB E6 0E FB CB 60 C7 02 00 01 49 00<br />0E 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E 78<br />70 00 00 00 0B</code>
				</div>
		</pre>
		<p>Figure 2 offers a high-level look at the serialization algorithm for this scenario.</p>
		<img height="711" alt="An outline of the serialization algorithm" src="http://www.javaworld.com/javaworld/jw-05-2009/images/jtip050709-fig2.gif" width="350" />
		<h4>Figure 2. An outline of the serialization algorithm</h4>
		<p>Let's go through the serialized format of the object in detail and see what each byte represents. Begin with the serialization protocol information:</p>
		<ul>
				<li>
						<code>AC ED</code>: <code>STREAM_MAGIC</code>. Specifies that this is a serialization protocol. 
</li>
				<li>
						<code>00 05</code>: <code>STREAM_VERSION</code>. The serialization version. 
</li>
				<li>
						<code>0x73</code>: <code>TC_OBJECT</code>. Specifies that this is a new <code>Object</code>. </li>
		</ul>
		<p>The first step of the serialization algorithm is to write the description of the class associated with an instance. The example serializes an object of type <code>SerialTest</code>, so the algorithm starts by writing the description of the <code>SerialTest</code> class.</p>
		<ul>
				<li>
						<code>0x72</code>: <code>TC_CLASSDESC</code>. Specifies that this is a new class. 
</li>
				<li>
						<code>00 0A</code>: Length of the class name. 
</li>
				<li>
						<code>53 65 72 69 61 6c 54 65 73 74</code>: <code>SerialTest</code>, the name of the class. 
</li>
				<li>
						<code>05 52 81 5A AC 66 02 F6</code>: <code>SerialVersionUID</code>, the serial version identifier of this class. 
</li>
				<li>
						<code>0x02</code>: Various flags. This particular flag says that the object supports serialization. 
</li>
				<li>
						<code>00 02</code>: Number of fields in this class. </li>
		</ul>
		<p>Next, the algorithm writes the field <code>int version = 66;</code>.</p>
		<ul>
				<li>
						<code>0x49</code>: Field type code. 49 represents "I", which stands for <code>Int</code>. 
</li>
				<li>
						<code>00 07</code>: Length of the field name. 
</li>
				<li>
						<code>76 65 72 73 69 6F 6E</code>: <code>version</code>, the name of the field. </li>
		</ul>
		<p>And then the algorithm writes the next field, <code>contain con = new contain();</code>. This is an object, so it will write the canonical JVM signature of this field.</p>
		<ul>
				<li>
						<code>0x74</code>: <code>TC_STRING</code>. Represents a new string. 
</li>
				<li>
						<code>00 09</code>: Length of the string. 
</li>
				<li>
						<code>4C 63 6F 6E 74 61 69 6E 3B</code>: <code>Lcontain;</code>, the canonical JVM signature. 
</li>
				<li>
						<code>0x78</code>: <code>TC_ENDBLOCKDATA</code>, the end of the optional block data for an object. </li>
		</ul>
		<p>The next step of the algorithm is to write the description of the <code>parent</code> class, which is the immediate superclass of <code>SerialTest</code>.</p>
		<ul>
				<li>
						<code>0x72</code>: <code>TC_CLASSDESC</code>. Specifies that this is a new class. 
</li>
				<li>
						<code>00 06</code>: Length of the class name. 
</li>
				<li>
						<code>70 61 72 65 6E 74</code>: <code>SerialTest</code>, the name of the class 
</li>
				<li>
						<code>0E DB D2 BD 85 EE 63 7A</code>: <code>SerialVersionUID</code>, the serial version identifier of this class. 
</li>
				<li>
						<code>0x02</code>: Various flags. This flag notes that the object supports serialization. 
</li>
				<li>
						<code>00 01</code>: Number of fields in this class. </li>
		</ul>
		<p>Now the algorithm will write the field description for the <code>parent</code> class. <code>parent</code> has one field, <code>int parentVersion = 100;</code>.</p>
		<ul>
				<li>
						<code>0x49</code>: Field type code. 49 represents "I", which stands for <code>Int</code>. 
</li>
				<li>
						<code>00 0D</code>: Length of the field name. 
</li>
				<li>
						<code>70 61 72 65 6E 74 56 65 72 73 69 6F 6E</code>: <code>parentVersion</code>, the name of the field. 
</li>
				<li>
						<code>0x78</code>: <code>TC_ENDBLOCKDATA</code>, the end of block data for this object. 
</li>
				<li>
						<code>0x70</code>: <code>TC_NULL</code>, which represents the fact that there are no more superclasses because we have reached the top of the class hierarchy. </li>
		</ul>
		<p>So far, the serialization algorithm has written the description of the class associated with the instance and all its superclasses. Next, it will write the actual data associated with the instance. It writes the parent class members first:</p>
		<ul>
				<li>
						<code>00 00 00 0A</code>: 10, the value of <code>parentVersion</code>. </li>
		</ul>
		<p>Then it moves on to <code>SerialTest</code>.</p>
		<ul>
				<li>
						<code>00 00 00 42</code>: 66, the value of <code>version</code>. </li>
		</ul>
		<p>The next few bytes are interesting. The algorithm needs to write the information about the <code>contain</code> object, shown in Listing 8.</p>
		<h4>Listing 8. The contain object</h4>
		<pre>
				<div class="codeblock">
						<code>contain con = new contain();</code>
				</div>
		</pre>
		<p>Remember, the serialization algorithm hasn't written the class description for the <code>contain</code> class yet. This is the opportunity to write this description.</p>
		<ul>
				<li>
						<code>0x73</code>: <code>TC_OBJECT</code>, designating a new object. 
</li>
				<li>
						<code>0x72</code>: <code>TC_CLASSDESC</code>. 
</li>
				<li>
						<code>00 07</code>: Length of the class name. 
</li>
				<li>
						<code>63 6F 6E 74 61 69 6E</code>: <code>contain</code>, the name of the class. 
</li>
				<li>
						<code>FC BB E6 0E FB CB 60 C7</code>: <code>SerialVersionUID</code>, the serial version identifier of this class. 
</li>
				<li>
						<code>0x02</code>: Various flags. This flag indicates that this class supports serialization. 
</li>
				<li>
						<code>00 01</code>: Number of fields in this class. </li>
		</ul>
		<p>Next, the algorithm must write the description for <code>contain</code>'s only field, <code>int containVersion = 11;</code>.</p>
		<ul>
				<li>
						<code>0x49</code>: Field type code. 49 represents "I", which stands for <code>Int</code>. 
</li>
				<li>
						<code>00 0E</code>: Length of the field name. 
</li>
				<li>
						<code>63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E</code>: <code>containVersion</code>, the name of the field. 
</li>
				<li>
						<code>0x78</code>: <code>TC_ENDBLOCKDATA</code>. </li>
		</ul>
		<p>Next, the serialization algorithm checks to see if <code>contain</code> has any parent classes. If it did, the algorithm would start writing that class; but in this case there is no superclass for <code>contain</code>, so the algorithm writes <code>TC_NULL</code>.</p>
		<ul>
				<li>
						<code>0x70</code>: <code>TC_NULL</code>. </li>
		</ul>
		<p>Finally, the algorithm writes the actual data associated with <code>contain</code>.</p>
		<ul>
				<li>
						<code>00 00 00 0B</code>: 11, the value of <code>containVersion</code>. </li>
		</ul>
		<h2>Conclusion</h2>
		<p>In this tip, you have seen how to serialize an object, and learned how the serialization algorithm works in detail. I hope this article gives you more detail on what happens when you actually serialize an object.<br /><br />From  ： <a href="http://www.javaworld.com/community/node/2915">http://www.javaworld.com/community/node/2915</a></p>
<img src ="http://www.blogjava.net/nikita/aggbug/317951.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2010-04-10 17:20 <a href="http://www.blogjava.net/nikita/archive/2010/04/10/317951.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>嵌套类和内部类</title><link>http://www.blogjava.net/nikita/archive/2009/10/14/298294.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Wed, 14 Oct 2009 15:26:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/10/14/298294.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/298294.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/10/14/298294.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/298294.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/298294.html</trackback:ping><description><![CDATA[
		<font size="2"> 来自: <a href="/lincode/archive/2008/08/13/221594.html">http://www.blogjava.net/lincode/archive/2008/08/13/221594.html</a><br /><br />一 概念：<br /><br />Java将嵌套类分成两个主要的分类：静态嵌套类（static nested classes）和内部类（inner classes）。<br /><br />1 <strong>嵌套类</strong>（nested classes），在一个类内部定义的另一个类，称为嵌套类。<br />  嵌套类分为两种， static 修饰的 静态嵌套类， 和无 static 修饰的 非静态嵌套类，也叫作 内部类。<br /><br />2 <strong>内部类</strong>（inner classes）， 无 static 修饰的 非静态嵌套类。<br /><br />Java还进一步将内部类分为：<br /><br /><strong>实例内部类</strong>（instance inner class） ，假设在声明嵌套类时，没有在前面加上static关键字。这样得到的是个实例内部类。静态嵌套类只能够访问外部类的static成员，而实例内部类能够同时访问static和实例成员。<br /><br /><strong>本地内部类</strong>（local inner class），Java允许在任意的代码段中放入一个类，该类的代码放在一对大括号中{  }。这意味着类能够出现在一个方法中，甚至是在if语句的一对括号中。这样的类就称为本地内部类。相对于实例内部类来说，本地内部类和有一个好处。他除了能够访问外部类的实例和类字段外（称为实例和类方法），还能够访问本地变量和方法的参数。<br /><br /><strong>匿名内部类</strong>（and anonymous inner class），没有给出类名的内部类为匿名内部类。<br /><br />二 使用：<br /><br />1. 访问内部类：<br /><br />我们可以在嵌套类（包括静态的和非静态的 ）前加上 private，protected 或 public 关键字，以指示该类在外部类以外的访问级别。<br /><br />具体访问方法如下：<br />(1).内部类，要以如下形式访问， outclass 是 OutClass 的一个实例，它放在 new 前指示内部类的实例是属于一个特定的外部类的实例的。 这是因为，只有外部类的实例存在，他的实例内部类（ 也就是非静态内部类）才能存在。<br /></font>
		<span style="COLOR: rgb(0,128,0)">
				<br />
				<font size="2">   //</font>
		</span>
		<font size="2">
				<span style="COLOR: rgb(0,128,0)"> innerclass</span>
				<br />
				<span style="COLOR: rgb(0,0,0)">  OutClass outclass </span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)"> </span>
				<span style="COLOR: rgb(0,0,255)">new</span>
		</font>
		<span style="COLOR: rgb(0,0,0)">
				<font size="2"> OutClass();<br />  </font>
		</span>
		<span style="COLOR: rgb(0,128,0)">
				<br />
		</span>
		<font size="2">
				<span style="COLOR: rgb(0,0,0)">  OutClass.InnerClass innerclass </span>
				<span style="COLOR: rgb(0,0,0)">=</span>
				<span style="COLOR: rgb(0,0,0)"> outclass.</span>
				<span style="COLOR: rgb(0,0,255)">new</span>
		</font>
		<font size="2">
				<span style="COLOR: rgb(0,0,0)"> InnerClass();<br />  innerclass.normalMethod();</span>
				<br />
				<br />(2).静态嵌套类， 可以用 OutClass.StaticInnerClass 来指示。<br />静态嵌套类，可以有实例成员（实例方法 和 实例变量）<br />访问静态嵌套类的实例域，需要先实例化静态嵌套类。<br />这个类是静态的，它不依赖于它外部类的某个实例，所以无须先实例化外部类。这和内部类是不一样的。<br /><br /><span style="COLOR: rgb(0,0,0)">  OutClass.StaticInnerClass staticinnerclass </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span></font>
		<font size="2">
				<span style="COLOR: rgb(0,0,0)"> OutClass.StaticInnerClass();<br />  staticinnerclass.normalMethod();</span>
				<br />
				<br />(3).静态嵌套类的静态方法，可以用 OutClass.StaticInnerClass.<span style="COLOR: rgb(0,0,0)">staticMethod() 来访问。</span><br /><br />实例代码：<br /></font>
		<div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)">
				<font size="2">
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> OutClass.java</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">class</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> OutClass {<br /><br />    </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> non static inner classes</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">    </span>
						<span style="COLOR: rgb(0,0,255)">class</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> InnerClass{<br />    <br />        </span>
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">void</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> normalMethod(){<br />            System.out.println(</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
						<span style="COLOR: rgb(0,0,0)"> success: non-static innerclass</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">);<br />        }<br />    }<br />    <br />    </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> static inner classes</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">    </span>
						<span style="COLOR: rgb(0,0,255)">static</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">class</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> StaticInnerClass<br />    {<br />        </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)">non static method of static inner classes</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">        </span>
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">void</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> normalMethod()<br />        {<br />            System.out.println(</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
						<span style="COLOR: rgb(0,0,0)"> success: non static method of static innerclas</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">);<br />        }<br />        <br />        </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> static methode of static inner classes</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">        </span>
						<span style="COLOR: rgb(0,0,255)">static</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">void</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> staticMethod()<br />        {<br />            System.out.println(</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
						<span style="COLOR: rgb(0,0,0)"> success: static method of static innerclass</span>
						<span style="COLOR: rgb(0,0,0)">"</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">);<br />        }<br />    }<br />} </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> end of InnerClass</span>
						<br />
				</font>
				<span style="COLOR: rgb(0,0,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> Test.java</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">class</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> Test {<br /><br /></span>
						<span style="COLOR: rgb(0,0,255)">public</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">static</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">void</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> main(String[] args){<br /><br />  OutClass outclass </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">new</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> OutClass();<br />  <br />  </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> innerclass</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">  OutClass.InnerClass innerclass </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> outclass.</span>
						<span style="COLOR: rgb(0,0,255)">new</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> InnerClass();<br />  innerclass.normalMethod();<br />  <br />  </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> non static method of static innerclas</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">  OutClass.StaticInnerClass staticinnerclass </span>
						<span style="COLOR: rgb(0,0,0)">=</span>
						<span style="COLOR: rgb(0,0,0)"> </span>
						<span style="COLOR: rgb(0,0,255)">new</span>
				</font>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)"> OutClass.StaticInnerClass();<br />  staticinnerclass.normalMethod();<br /><br />  </span>
						<span style="COLOR: rgb(0,128,0)">//</span>
						<span style="COLOR: rgb(0,128,0)"> static method of static innerclass</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: rgb(0,0,0)">  OutClass.StaticInnerClass.staticMethod();  <br />  }<br />}</span>
						<span style="COLOR: rgb(0,128,0)">//</span>
				</font>
				<span style="COLOR: rgb(0,128,0)">
						<font size="2"> end of Test<br /></font>
				</span>
		</div>
		<br />
		<font size="2">2. 嵌套类 访问 外部类的成员：<br /><br />(1). 外部类对于内部类（非静态嵌套类）是透明的。包括外部类的private成员，其内部类都可以自由访问。<br /><br />(2). 一个静态嵌套类只能访问外部类的静态方法，它是不能访问任何外部类的实例成员（包括变量和方法）。<br /><br />(3). 内部类中的 this 是指内部类本身。内部类的成员和外部类的成员名冲突，内部类内，冲突的成员名是指内部类成员。<br />要调用外部类的冲突的成员，要以 外部类的类名 指示：对于冲突的变量 OutClass.conflitName  对于冲突的方法 OutClass.conflitName()。<br /></font>
<img src ="http://www.blogjava.net/nikita/aggbug/298294.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-10-14 23:26 <a href="http://www.blogjava.net/nikita/archive/2009/10/14/298294.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Bitwise logical operators in java</title><link>http://www.blogjava.net/nikita/archive/2009/10/08/297414.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Wed, 07 Oct 2009 17:06:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/10/08/297414.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/297414.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/10/08/297414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/297414.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/297414.html</trackback:ping><description><![CDATA[
		<br />1- The AND operator : (&amp;)<br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />x           y                 x&amp;y  <br />-----------------------------<br />0           0                  0<br />0           1                  0<br />1           0                  0<br />1           1                  1<br /></div><br /><br /><span style="FONT-WEIGHT: bold">Example on &amp; operator </span><br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />     byte x = 50;<br />     byte y = 51;<br />     byte result = (byte) (x&amp;y);<br />    System.out.println("Result of x&amp;y= : " + result );<br /><br /></div><br /><br /><span style="FONT-WEIGHT: bold">The result equal = 50</span><br /><br />00110010<br />00110011<br />-------------<br />00110010<br /><br />2- The OR operator : (|)<br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />x           y                 x|y  <br />-----------------------------<br />0           0                  0<br />0           1                  1<br />1           0                  1<br />1           1                  1<br /></div><br /><br /><span style="FONT-WEIGHT: bold">Example on | operator </span><br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />     byte x = 50;<br />     byte y = 51;<br />     byte result = (byte) (x|y);<br />    System.out.println("Result of x|y= : " + result );<br /><br /></div><br /><br /><span style="FONT-WEIGHT: bold">The result equal = 51</span><br /><br />00110010<br />00110011<br />-------------<br />00110011<br /><br />3- The XOR operator : (^)<br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />x           y                 x^y  <br />-----------------------------<br />0           0                  0<br />0           1                  1<br />1           0                  1<br />1           1                  0<br /></div><br /><br /><span style="FONT-WEIGHT: bold">Example on ^ operator </span><br /><br /><div class="codetitle"><b>Code:</b></div><div class="codecontent"><br />     byte x = 50;<br />     byte y = 51;<br />     byte result = (byte) (x^y);<br />    System.out.println("Result of x^y= : " + result );<br /><br /></div><br /><br /><span style="FONT-WEIGHT: bold">The result equal = 1</span><br /><br />00110010<br />00110011<br />-------------<br />00000001<br /><br /><br />4- The Inversion Operator: (~)<br />Invert each bit in the byte . <br /><br />Example : <br /><div class="codetitle"><b>Code:</b></div><div class="codecontent">~00110010 = 11001101<br /></div><br /><br /><br />5- Boolean Inversion Operator (!)<br />invert the value of boolean <br /><script type="text/javascript"><!--
google_ad_client = "pub-1974979321499009";
/* 200x200, created 11/30/08 */
google_ad_slot = "6501703756";
google_ad_width = 200;
google_ad_height = 200;
//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script><script><![CDATA[oogle_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);]]&gt;</script><img src ="http://www.blogjava.net/nikita/aggbug/297414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-10-08 01:06 <a href="http://www.blogjava.net/nikita/archive/2009/10/08/297414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>weak reference </title><link>http://www.blogjava.net/nikita/archive/2009/05/17/271082.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Sat, 16 May 2009 16:12:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/05/17/271082.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/271082.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/05/17/271082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/271082.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/271082.html</trackback:ping><description><![CDATA[A weak reference is one that does not prevent the referenced object from being garbage collected. You might use them to manage a <span class="class">HashMap</span> to look up a cache of objects. A weak reference is a reference that does not keep the object it refers to alive. A weak reference is not counted as a reference in garbage collection. If the object is not referred to elsewhere as well, it will be garbage collected. 
<p>The GC will send some sort of "finalize" message to the object and then set any weakly-referencing variables to null whenever it disposes of the referenced object. This allows "finalization" logic to be run before the object is disposed of (e.g., close a file if still open, commit any open transaction(s), etc.). Java 1.1 does not support weak references other than via an undocumented Ref class that is not supported under Netscape. Weak references arrived officially with JDK 1.2. Java has three kinds of weak references, called soft references, weak references, and phantom references, in order of increasing weakness. 
</p><p><!-- macro Include E:\mindprod\jgloss\include\weakvssoft.html --><!-- generated -->Java has four orders of strength in holding onto Objects. In descending order from strongest to weakest they are: 
</p><ol><li><!-- W A R N I N G _ I N C L U D E D -->The JVM holds onto regular Objects until they are no longer reachable by either clients or any container. In other words Objects are garbage collected when there are no more live references to them. Dead references don’t count. 
<div class="see"><a href="http://mindprod.com/jgloss/garbagecollection.html"><font color="#002c99">garbage collection</font></a></div></li><li>Soft references can be deleted from a container if the clients are no longer referencing them and memory is tight. 
</li><li>Weak references are automatically deleted from a container as soon clients stop referencing them. 
</li><li>Phantom references point to objects that are already dead and have been finalised.</li></ol><img src ="http://www.blogjava.net/nikita/aggbug/271082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-05-17 00:12 <a href="http://www.blogjava.net/nikita/archive/2009/05/17/271082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ClassLoader原理</title><link>http://www.blogjava.net/nikita/archive/2009/05/16/271078.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Sat, 16 May 2009 15:38:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/05/16/271078.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/271078.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/05/16/271078.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/271078.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/271078.html</trackback:ping><description><![CDATA[
		<strong> </strong>JVM规范定义了两种类型的类装载器：<strong>启动内装载器</strong><strong>(bootstrap)</strong><strong>和用户自定义装载器</strong><strong>(user-defined class loader)</strong>。 <br /><br /><strong>一．    ClassLoader基本概念<br />1．ClassLoader分类<br /></strong>类装载器是用来把类(class)装载进JVM的。<br />JVM规范定义了两种类型的类装载器：<strong>启动内装载器</strong><strong>(bootstrap)</strong><strong>和用户自定义装载器</strong><strong>(user-defined class loader)</strong>。 <br /><br /><strong><br /></strong><strong>JVM在运行时会产生三个ClassLoader:Bootstrap ClassLoader、Extension ClassLoader和AppClassLoader.</strong>Bootstrap是用C++编写的，我们在Java中看不到它,是null,是JVM自带的类装载器，用来装载核心类库，如java.lang.*等。<br /><strong>AppClassLoader</strong><strong>的</strong><strong>Parent</strong><strong>是</strong><strong>ExtClassLoader</strong><strong>，而</strong><strong>ExtClassLoader</strong><strong>的</strong><strong>Parent</strong><strong>为</strong><strong>Bootstrap ClassLoader</strong><strong>。</strong><br /> <br /><strong>Java</strong><strong>提供了抽象类</strong><strong>ClassLoader</strong><strong>，所有用户自定义类装载器都实例化自</strong><strong>ClassLoader</strong><strong>的子类。</strong><strong> System Class Loader</strong><strong>是一个特殊的用户自定义类装载器，由</strong><strong>JVM</strong><strong>的实现者提供，在编程者不特别指定装载器的情况下默认装载用户类</strong>。系统类装载器可以通过ClassLoader.getSystemClassLoader() 方法得到。<br /> <br />例1，测试你所使用的JVM的ClassLoader<br />/*LoaderSample1.java*/<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> LoaderSample1 {<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> main(String[] args) {<br />        Class c;<br />        ClassLoader cl;<br />        cl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> ClassLoader.getSystemClassLoader();<br />        System.out.println(cl);<br />        </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> (cl </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">) {<br />            cl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> cl.getParent();<br />            System.out.println(cl);<br />        }<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Class.forName(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">java.lang.Object</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            cl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.getClassLoader();<br />            System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">java.lang.Object's loader is </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> cl);<br />            c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Class.forName(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">LoaderSample1</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            cl </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.getClassLoader();<br />            System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">LoaderSample1's loader is </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> cl);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />}</span></div><p><br />在我的机器上(Sun Java 1.4.2)的运行结果<br /><font color="#0066ff">sun.misc.Launcher$AppClassLoader@1a0c10f<br />sun.misc.Launcher$ExtClassLoader@e2eec8<br />null <br />java.lang.Object's loader is null<br />LoaderSample1's loader is sun.misc.Launcher$AppClassLoader@1a0c10f</font><br />第一行表示，系统类装载器实例化自类sun.misc.Launcher$AppClassLoader <br />第二行表示，系统类装载器的parent实例化自类sun.misc.Launcher$ExtClassLoader <br />第三行表示，系统类装载器parent的parent为bootstrap <br />第四行表示，核心类java.lang.Object是由bootstrap装载的 <br />第五行表示，用户类LoaderSample1是由系统类装载器装载的 <br /> <br /> <br /><strong>二．</strong><strong>parent delegation模型</strong><strong><br /></strong>从1.2版本开始，Java引入了双亲委托模型，从而更好的保证Java平台的安全。<strong>在此模型下，当一个装载器被请求装载某个类时，它首先委托自己的</strong><strong>parent</strong><strong>去装载，若</strong><strong>parent</strong><strong>能装载，则返回这个类所对应的</strong><strong>Class</strong><strong>对象，若</strong><strong>parent</strong><strong>不能装载，则由</strong><strong>parent</strong><strong>的请求者去装载</strong>。<br /><br />图 1 parent delegation模型<br />如图1所示，loader2的parent为loader1，loader1的parent为system class loader。假设loader2被要求装载类MyClass，在parent delegation模型下，loader2首先请求loader1代为装载，loader1再请求系统类装载器去装载MyClass。若系统装载器能成功装载，则将MyClass所对应的Class对象的reference返回给loader1，loader1再将reference返回给loader2，从而成功将类MyClass装载进虚拟机。若系统类装载器不能装载MyClass，loader1会尝试装载MyClass，若loader1也不能成功装载，loader2会尝试装载。若所有的parent及loader2本身都不能装载，则装载失败。<br /> <br />若有一个能成功装载，实际装载的类装载器被称为定义类装载器，所有能成功返回Class对象的装载器（包括定义类装载器）被称为初始类装载器。如图1所示，假设loader1实际装载了MyClass，则loader1为MyClass的定义类装载器，loader2和loader1为MyClass的初始类装载器。<br /> <br />需要指出的是，Class Loader是对象，它的父子关系和类的父子关系没有任何关系。<br /> <br />那么parent delegation模型为什么更安全了？<strong>因为在此模型下用户自定义的类装载器不可能装载应该由父亲装载器装载的可靠类，从而防止不可靠甚至恶意的代码代替由父亲装载器装载的可靠代码。实际上，类装载器的编写者可以自由选择不用把请求委托给</strong><strong>parent</strong><strong>，但正如上所说，会带来安全的问题。</strong><strong><br /></strong><strong> <br /></strong> <br /><strong>三．命名空间及其作用</strong><strong><br /></strong>每个类装载器有自己的命名空间，命名空间由所有以此装载器为创始类装载器的类组成。不同命名空间的两个类是不可见的，但只要得到类所对应的Class对象的reference，还是可以访问另一命名空间的类。<br /> <br />例2演示了一个命名空间的类如何使用另一命名空间的类。在例子中，LoaderSample2由系统类装载器装载，LoaderSample3由自定义的装载器loader负责装载，两个类不在同一命名空间，但LoaderSample2得到了LoaderSample3所对应的Class对象的reference，所以它可以访问LoaderSampl3中公共的成员(如age)。<br />例2不同命名空间的类的访问<br />/*LoaderSample2.java*/<br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.net.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.lang.reflect.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> LoaderSample2 {<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> main(String[] args) {<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"> {<br />            String path </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> System.getProperty(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">user.dir</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            URL[] us </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> {</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> URL(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">file://</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> path </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">/sub/</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">)};<br />            ClassLoader loader </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> URLClassLoader(us);<br />            Class c </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> loader.loadClass(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">LoaderSample3</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            Object o </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.newInstance();<br />            Field f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> c.getField(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">age</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />            </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> age </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> f.getInt(o);<br />            System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">age is </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> age);<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (Exception e) {<br />            e.printStackTrace();<br />        }<br />    }<br />}</span></div><p><br />/*sub/Loadersample3.java*/</p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> LoaderSample3 {<br />    </span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000"> {<br />        System.out.println(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">LoaderSample3 loaded</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />    }<br />    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> age </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">30</span><span style="COLOR: #000000">;<br />}</span></div><p>编译：javac LoaderSample2.java; javac sub/LoaderSample3.java<br />运行：java LoaderSample2<br />LoaderSample3 loaded<br />age is 30<br />从运行结果中可以看出，在类LoaderSample2中可以创建处于另一命名空间的类LoaderSample3中的对象并可以访问其公共成员age。<br />运行时包(runtime package)<br />由同一类装载器定义装载的属于相同包的类组成了运行时包，决定两个类是不是属于同一个运行时包，不仅要看它们的包名是否相同，还要看的定义类装载器是否相同。只有属于同一运行时包的类才能互相访问包可见的类和成员。这样的限制避免了用户自己的代码冒充核心类库的类访问核心类库包可见成员的情况。假设用户自己定义了一个类java.lang.Yes，并用用户自定义的类装载器装载，由于java.lang.Yes和核心类库java.lang.*由不同的装载器装载，它们属于不同的运行时包，所以java.lang.Yes不能访问核心类库java.lang中类的包可见的成员。 <br /> <br /><strong>总结</strong><strong><br /></strong><strong>命名空间并没有完全禁止属于不同空间的类的互相访问，双亲委托模型加强了</strong><strong>Java</strong><strong>的安全，运行时包增加了对包可见成员的保护。</strong><strong><br /></strong><strong> <br /></strong><strong>二．    </strong><strong>扩展ClassLoader</strong><strong>方法</strong><strong><br /></strong>我们目的是从本地文件系统使用我们实现的类装载器装载一个类。<strong>为了创建自己的类装载器我们应该扩展</strong><strong>ClassLoader</strong><strong>类，这是一个抽象类。我们创建一个</strong><strong>FileClassLoader extends ClassLoader</strong><strong>。我们需要覆盖</strong><strong>ClassLoader</strong><strong>中的</strong><strong>findClass(String name)</strong><strong>方法，这个方法通过类的名字而得到一个</strong><strong>Class</strong><strong>对象。</strong><br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> Class findClass(String name)<br />    {<br />        </span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">[] data </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> loadClassData(name);<br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> defineClass(name, data, </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">, data.length);<br />    }</span></div><p><br /><strong>   我们还应该提供一个方法loadClassData(String name)，通过类的名称返回class文件的字</strong><strong><br /></strong><strong>节数组。然后使用ClassLoader提供的defineClass()方法我们就可以返回Class对象了。</strong><br /></p><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">[] loadClassData(String name)<br />    {<br />        FileInputStream fis </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br />        </span><span style="COLOR: #0000ff">byte</span><span style="COLOR: #000000">[] data </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br />        </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000"><br />        {<br />            fis </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> FileInputStream(</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> File(drive </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> name </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> fileType));<br />            ByteArrayOutputStream baos </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ByteArrayOutputStream();<br />            </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> ch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;<br />            </span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> ((ch </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> fis.read()) </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />            {<br />                baos.write(ch);<br />               <br />            }<br />            data </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> baos.toByteArray();<br />        } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (IOException e)<br />        {<br />            e.printStackTrace();<br />        }<br />        <br />        </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> data;<br />    }</span></div><br /><br /><p><font face="Times New Roman" size="3">再说说Package权限。Java语言规定，在同一个包中的class，如果没有修饰符，默认为Package权限，包内的class都可以访问。但是这还不够准确。确切的说，只有由同一个ClassLoader装载的class才具有以上的Package权限。比如启动类装载器装载了java.lang.String，类路径装载器装载了我们自己写的java.lang.Test，它们不能互相访问对方具有Package权限的方法。这样就阻止了恶意代码访问核心类的Package权限方法。</font></p><br /><img src ="http://www.blogjava.net/nikita/aggbug/271078.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-05-16 23:38 <a href="http://www.blogjava.net/nikita/archive/2009/05/16/271078.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Bitwise and Bit Shift Operators</title><link>http://www.blogjava.net/nikita/archive/2009/05/14/270669.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Thu, 14 May 2009 09:43:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/05/14/270669.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/270669.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/05/14/270669.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/270669.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/270669.html</trackback:ping><description><![CDATA[
		<div id="PageTitle">
				<strong>
						<font style="BACKGROUND-COLOR: #ffffff" color="#ff9933">Bitwise and Bit Shift Operators</font>
				</strong>
		</div>
		<blockquote>
				<font face="Arial">
						<em>The Java programming language also provides operators that perform bitwise and bit shift operations on integral types. The operators discussed in this section are less commonly used. Therefore, their coverage is brief; the intent is to simply make you aware that these operators exist. </em>
				</font>
				<p>
						<font face="Arial">
								<em>The unary bitwise complement operator "<code>~</code>" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a <code>byte</code> contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111". </em>
						</font>
				</p>
				<p>
						<font face="Arial">
								<em>The signed left shift operator "<code>&lt;&lt;</code>" shifts a bit pattern to the left, and the signed right shift operator "<code>&gt;&gt;</code>" shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator "<code>&gt;&gt;&gt;</code>" shifts a zero into the leftmost position, while the leftmost position after <code>"&gt;&gt;"</code> depends on sign extension. </em>
						</font>
				</p>
				<p>
						<font face="Arial">
								<em>The bitwise <code>&amp;</code> operator performs a bitwise AND operation. </em>
						</font>
				</p>
				<p>
						<font face="Arial">
								<em>The bitwise <code>^</code> operator performs a bitwise exclusive OR operation. </em>
						</font>
				</p>
				<p>
						<font face="Arial">
								<em>The bitwise <code>|</code> operator performs a bitwise inclusive OR operation. </em>
						</font>
				</p>
				<p>
						<font face="Arial">
								<em>The following program, </em>
						</font>
						<a class="SourceLink" href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/examples/BitDemo.java" target="_blank">
								<code>
										<em>
												<font face="Arial">BitDemo</font>
										</em>
								</code>
						</a>
						<font face="Arial">
								<em>, uses the bitwise AND operator to print the number "2" to standard output. </em>
						</font>
				</p>
				<blockquote>
						<pre>
								<em>class BitDemo {
     public static void main(String[] args) {
          int bitmask = 0x000F;
	  int val = 0x2222;
	  System.out.println(val &amp; bitmask);  // prints "2"
     }
</em>
						</pre>
				</blockquote>
		</blockquote>
<img src ="http://www.blogjava.net/nikita/aggbug/270669.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-05-14 17:43 <a href="http://www.blogjava.net/nikita/archive/2009/05/14/270669.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java 調用 LINUX service ntpd stop </title><link>http://www.blogjava.net/nikita/archive/2009/04/26/267540.html</link><dc:creator>picture talk</dc:creator><author>picture talk</author><pubDate>Sat, 25 Apr 2009 18:38:00 GMT</pubDate><guid>http://www.blogjava.net/nikita/archive/2009/04/26/267540.html</guid><wfw:comment>http://www.blogjava.net/nikita/comments/267540.html</wfw:comment><comments>http://www.blogjava.net/nikita/archive/2009/04/26/267540.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/nikita/comments/commentRss/267540.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/nikita/services/trackbacks/267540.html</trackback:ping><description><![CDATA[
		<span class="javascript" id="text196591">If you read about the Javadoc of the Process class, you will discover something<br />very weird and unexpected. The following is the context of the Javadoc for Process.<br />In effect, you should be very clear of this limitation before you think of using<br />the Process JDK. Ok, here it is:<br /><br /><pre class="codeStyle"><br /><font color="#808080">The methods that create processes may not work well for special processes on certain<br />native platforms, such as native windowing processes, daemon processes, Win16/DOS <br />processes on Microsoft Windows, or shell scripts.<br /><br />The created subprocess does not have its own terminal or console. All its standard <br />io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process<br />through three streams (getOutputStream(), getInputStream(), getErrorStream()). <br />...</font><br /></pre><br />So basically, Process does not guarantee your daemon process to work properly. This<br />is horrifying when I first read about this Javadoc. However, I have changed my<br />mind after using Process for a while. My experience tells me that usually Process<br />cannot be working at the following circumstances: (assuming we are in Linux O.S.)<br /><br />- to execute shell built-in commands e.g. "cd" or "alias"<br />- to redirect output from the executed application,<br />- This method will NOT expand filesystem wildcards e.g. "*".<br /><br />Taking all these things into consideration, let's see what we can do to improve<br />your code.<br /><br />First of all, Process has its standard outputstream, and its standard errorstream<br />(but don't try to redirect them, as it will *not* work)<br /><br /><pre class="codeStyle"><br /><font style="BACKGROUND-COLOR: #000000" color="#808080">Process proc = Runtime.getRuntime().exec(command);<br /><br />// Create thread for reading inputStream (process' stdout)<br />proc.getInputStream();<br /><br />// Create thread for reading errorStream (process' stderr)<br />proc.getErrorStream()<br /></font></pre><br /><br />Another fact I can see why your ntpd has not stopped is maybe you havn't given<br />enough time for the Process to be executed completely.<br /><br />Consider adding the following code into your program:<br /><br /><pre class="codeStyle"><br /><font color="#808080">// this will wait for the process to end!!! I think it is very important<br />// not to interfere with other process in the O.S.<br />int returnCode = proc.waitFor();<br /><br />String msg = "Shell command [" + commandString + "] result [";<br />if (returnCode == 0)<br />{<br />    msg += "SUCCEEDED";<br />}<br />else<br />{<br />    msg = msg + "FAILED - ret code: " + returnCode;<br />}<br />msg += "]";</font><br /></pre><br /><br />Then you can print the msg to the standard output or simply trace it into your log.<br />You shall see the error if occurs.<br /><br />Another thing I can suggest you to do is fully read the Process Javadoc and any related documents about using Process. <br /><br />Hope these help.<br /><br /><br /><span class="javascript" id="text196627"><font color="#808080">import java.io.*;<br />public chass shell{<br />public static void main(String [] args)throws Exception{ <br />try <br /><br />{ <br /><br />process = Runtime.getRuntime().exec ("bin/sh service ntpd stop"); <br /><br />InputStreamReader ir=newInputStreamReader(process.getInputStream()); <br /><br />LineNumberReader input = new LineNumberReader (ir);</font><br /><br /><font color="#808080">String line; <br /><br />while ((line = input.readLine ()) != null) <br /><br />System.out.println(line); <br /><br />} <br /><br />catch (java.io.IOException e){ <br /><br />System.err.println ("IOException " + e.getMessage()); <br /><br />} <br />}<br /></font></span> </span>
		<br />来自 :http://www.cjsdn.net/post/view?bid=1&amp;id=196586<br /><img src ="http://www.blogjava.net/nikita/aggbug/267540.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/nikita/" target="_blank">picture talk</a> 2009-04-26 02:38 <a href="http://www.blogjava.net/nikita/archive/2009/04/26/267540.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>