<?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>红色 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E7%BA%A2%E8%89%B2/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 07 Jun 2022 03:29:50 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]Java基础知识记录（2）</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9bjava%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e8%ae%b0%e5%bd%95%ef%bc%882%ef%bc%89/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9bjava%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e8%ae%b0%e5%bd%95%ef%bc%882%ef%bc%89/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 10 Apr 2013 03:12:44 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>
		<category><![CDATA[Java基础]]></category>
		<category><![CDATA[红色]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5780</guid>

					<description><![CDATA[<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">前言：本文『很傻很天真』，任何稍有Java编程经验的都不用看了。</span><br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color:#ff0000;"><span style="background-color:#00ff00;">▶</span></span>&#160;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">创建一个Set时，如何初始化其中的元素<br />
如果在创建一个Set之后，再用add()方法向Set中添加元素，就显得比较麻烦了。通过使用<a href="https://code.google.com/p/guava-libraries/" rel="noopener noreferrer" target="_blank">Google Guava</a>库，可以在初始化的时候就向其添加若干元素：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">Set&#60;String&#62;&#160;s&#160;=&#160;Sets.newHashSet(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;99&#34;</span>,&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;100&#34;</span>)
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">这样创建出的s就会包含99和100这两个元素。</span><br />
<span id="more-5780"></span><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);">▶</span></span>&#160;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">jar包中的.class文件，为什么有的文件名类似于 ***$1.class</span></p>
<div>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size: 14px;">$后面跟数字是匿名类编译出来的，$后面跟文字是内部类编译出来的。</span></span></div>
<p><span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);">▶</span></span>&#160;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">java.lang.OutOfMemoryError: GC overhead limit exceeded错误</span><br />
<span style="font-size: 14.399999618530273px;">出现这个错误的时候，程序可能接着就挂掉了。要关掉该功能，可以在程序启动的时候传入参数：</span></p>
<blockquote>
<p>
		java -XX:-UseGCOverheadLimit (后面省略)</p>
</blockquote>
<p>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);">▶</span></span>&#160;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">使用Log4j打印格式化的字符串时的高效率写法<br />
如果你使用Log4j来打印日志，字符串中需要format一些变量，例如：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&#160;countNum&#160;=&#160;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">9999</span>;
LOG.debug(String.format(</code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9bjava%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e8%ae%b0%e5%bd%95%ef%bc%882%ef%bc%89/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">前言：本文『很傻很天真』，任何稍有Java编程经验的都不用看了。</span><br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color:#ff0000;"><span style="background-color:#00ff00;"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">创建一个Set时，如何初始化其中的元素<br />
如果在创建一个Set之后，再用add()方法向Set中添加元素，就显得比较麻烦了。通过使用<a href="https://code.google.com/p/guava-libraries/" rel="noopener noreferrer" target="_blank">Google Guava</a>库，可以在初始化的时候就向其添加若干元素：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">Set&lt;String&gt;&nbsp;s&nbsp;=&nbsp;Sets.newHashSet(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;99&quot;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;100&quot;</span>)
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">这样创建出的s就会包含99和100这两个元素。</span><br />
<span id="more-5780"></span><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">jar包中的.class文件，为什么有的文件名类似于 ***$1.class</span></p>
<div>
	<span style="font-family:arial,helvetica,sans-serif;"><span style="font-size: 14px;">$后面跟数字是匿名类编译出来的，$后面跟文字是内部类编译出来的。</span></span></div>
<p><span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">java.lang.OutOfMemoryError: GC overhead limit exceeded错误</span><br />
<span style="font-size: 14.399999618530273px;">出现这个错误的时候，程序可能接着就挂掉了。要关掉该功能，可以在程序启动的时候传入参数：</span></p>
<blockquote>
<p>
		java -XX:-UseGCOverheadLimit (后面省略)</p>
</blockquote>
<p>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">使用Log4j打印格式化的字符串时的高效率写法<br />
如果你使用Log4j来打印日志，字符串中需要format一些变量，例如：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;countNum&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">9999</span>;
LOG.debug(String.format(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Count:&nbsp;%d&quot;</span>,&nbsp;countNum));
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">这样并不是最有效率的写法，如下写法更高效：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">int</span>&nbsp;countNum&nbsp;=&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">9999</span>;
LOG.debug(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Count:&nbsp;{}&quot;</span>,&nbsp;countNum);
</code></pre>
</section>
<p>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">单元测试时，提示&ldquo;</span>java.lang.SecurityException: Prohibited package name: java.com.xxx<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">&rdquo;错误的原因<br />
这是因为package名不能以java开头，修改后再重新运行单元测试即可解决此问题。</span><br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">同一个jar包中的多个client程序，有一个client能连上Zookeeper server的解决办法<br />
我最后还是没找到原因，但是解决了。可以尝试如下解决办法：</span><br />
①用 ps -ef | grep zookeeper 查看当前有几个Zookeeper进程，如果可以的话，把它们全部kill掉，再用Zookeeper安装目录下的 bin/zkServer.sh 脚本来启动。在启动之前，修改 conf/zoo.cfg 配置文件，添加如下一行：</p>
<blockquote>
<p>
		maxClientCnxns=300</p>
</blockquote>
<p>此参数含义为：<br />
单个客户端与单台服务器之间的连接数的限制，是IP级别的，默认是60，如果设置为0，那么表明不作任何限制。请注意这个限制的使用范围，仅仅是单台客户端机器与单台Zookeeper服务器之间的连接数限制，不是针对指定客户端IP，也不是ZK集群的连接数限制，也不是单台ZK对所有客户端的连接数限制。<br />
②用&nbsp;<span style="font-family: 文泉驿等宽微米黑;">bin/zkServer.sh 脚本来启动 Zookeeper server：</span></p>
<blockquote>
<p>
		./zkServer.sh start</p>
</blockquote>
<p>③查看server是否已经启动了，如果是，则重新用client程序连接Zookeeper server。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">Java正则截取书名号里的内容<br />
假设有如下字符串：<br />
</span></p>
<blockquote>
<p>
		<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">今天去看好评如潮的《变形金刚》这部电影</span></p>
</blockquote>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">那么要获取书名号里的内容&ldquo;变形金刚&rdquo;，可以用Java这样干：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">Pattern&nbsp;MY_PATTERN&nbsp;=&nbsp;Pattern.compile(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;.*《(.+)》.*&quot;</span>);

String&nbsp;str&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;今天去看好评如潮的《变形金刚》这部电影&quot;</span>;
String&nbsp;result&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;</span>;

Matcher&nbsp;matcher&nbsp;=&nbsp;MY_PATTERN.matcher(str);
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(matcher.matches())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;result&nbsp;=&nbsp;matcher.group(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>);
}
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">结果就保存在了result中。</span><br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">Java计算一个字符串的MD5值</span><br />
<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">用</span>org.apache.commons.codec.digest<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">包（jar包名类似于</span>commons-codec-1.6.jar<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">）中的</span><span style="color:#0000ff;">DigestUtils.md5Hex()</span>方法来计算是最方便的，不要自己去费时间地写了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">String&nbsp;md5&nbsp;=&nbsp;DigestUtils.md5Hex(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;abcdefg&quot;</span>);
System.out.println(md5);
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">结果：</span>7ac66c0f148de9519b8bd264312c4d64</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">Maven下载回来的依赖jar包的默认路径<br />
一个Java项目用Maven编译的话，会自动下载回来依赖的jar包，这些jar包如果不在当前路径下，可能在哪里呢？<br />
如果你没配置过的话，应该是在默认路径下：<br />
<span style="color:#0000ff;">/home/you_user/.m2/</span><br />
到这个隐藏路径下搜索那些jar包吧。</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;Class.forName 和 ClassLoader.loadClass的区别<br />
这两个方法都可以动态地加载一个类。<br />
之所以要&ldquo;动态&rdquo;地加载，是因为有时我们无法预先知道要加载的类是什么，例如，某文件中写了一个类名com.codelast.MyClassA，在读取这个文件之后，我才知道要加载的是类<span style="font-family: 文泉驿等宽微米黑;">MyClassA</span>，因此，我无法在程序中写诸如&nbsp;<span style="font-family: 文泉驿等宽微米黑;">MyClassA</span>&nbsp;a = new&nbsp;<span style="font-family: 文泉驿等宽微米黑;">MyClassA</span>(); 这样的代码来创建一个类<span style="font-family: 文泉驿等宽微米黑;">MyClassA</span>的对象，因能&ldquo;动态&rdquo;地加载类<span style="font-family: 文泉驿等宽微米黑;">MyClassA</span>，然后创建一个对象。<br />
但是，这两种加载类的方法的区别是什么呢？<br />
区别在于：Class.forName除了加载类到JVM中，还会执行类的静态初始化；而ClassLoader.loadClass不会执行类的初始化，只是在类被第一次使用时才会进行类的初始化。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: 文泉驿等宽微米黑;">一个Linux文件的</span>toURI()和toURL()的结果有什么不同<br />
如下代码：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">File&nbsp;f&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;File(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/opt/python3.3/bin/python3.3&quot;</span>);
System.out.println(f.toURI());
System.out.println(f.toURI().toURL());
</code></pre>
</section>
<p>输出结果为：</p>
<blockquote>
<div>
		file:/opt/python3.3/bin/python3.3</div>
<div>
		file:/opt/python3.3/bin/python3.3</div>
</blockquote>
<p>可见没有不同。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: 文泉驿等宽微米黑;">为什么要用</span>URLClassLoader来加载类<br />
在某些代码中，会看到用URLClassLoader来加载类，这货与用Class.forName加载类有何区别？<br />
<span style="font-family: 文泉驿等宽微米黑;">URLClassLoader可以从根据指定的路径搜索到的.class文件，或者根据.jar包中的class来加载类。它就不像</span>Class.forName那样要按包名的方式（例如 <span style="color:#0000ff;">Class t = Class.forName(&quot;java.lang.Thread&quot;)</span>）才能加载一个类了。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: 文泉驿等宽微米黑;">IntelliJ的控制台/console 里的中文显示乱码问题<br />
很久很久以前的IntelliJ是没有这个问题的，后来不记得从哪个版本开始有这个问题了：运行单元测试或main方法时，在控制台输出的中文是乱码的。<br />
解决办法：<br />
菜单&ldquo;Run&rdquo;&rarr;&ldquo;Configurations&rdquo;，在对应的类的&ldquo;VM options&rdquo;中添加：</span></p>
<blockquote>
<p>
		-Dfile.encoding=GB2312</p>
</blockquote>
<p>会发现乱码问题就解决了。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;用Ant打包文件时，要打包一个目录下的所有文件的表示方法 / 排除指定文件的表示方法<br />
假设你想把一个目录下的所有文件打进一个jar包里，在build.xml中可以这样写：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">jar</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">jarfile</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;package.jar&quot;</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">fileset</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">dir</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;.&quot;</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">includes</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;conf/**/*&quot;</span>/&gt;
</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">jar</span>&gt;</span></code></pre>
</section>
<p>这样就会把当前目录下的&ldquo;conf&rdquo;目录下的目录（包括子目录）和文件打进jar包中。<br />
如果要把conf目录下的一个特定文件&nbsp;log4j.properties 排除掉（不打进jar包里）呢？可以这样：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">jar</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">jarfile</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;package.jar&quot;</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">fileset</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">dir</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;.&quot;</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">includes</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;conf/**/*&quot;</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">exclude</span>&nbsp;<span class="hljs-attr" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">name</span>=<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;conf/log4j.*&quot;</span>/&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">fileset</span>&gt;
</span>
<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">jar</span>&gt;</span></code></pre>
</section>
<p>其中，exclude 就是用来排除的，这里把所有符合 log4j.* 这个pattern的文件都排除掉了。<br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;IntelliJ IDEA 13.1不支持subversion 1.8的问题<br />
Ubuntu 14.04用apt-get install安装上的svn已经是1.8.8的版本，如果你没有做过特殊设置，并且使用IntelliJ IDEA中的subversion相关功能（例如compare with latest repository version），会发现提示错误：</p>
<blockquote>
<p>
		Error:svn: E155021: This client is too old to work with the working copy at &#39;/home/codelast/xxx&#39; (format &#39;31&#39;).</p>
</blockquote>
<p>解决这个问题并不需要重新安装低版本的subversion（那样实在太二了），而是在IntelliJ中设置一下就好了：<br />
<span style="color:#0000ff;">Settings&rarr;Version Control&rarr;Subversion&rarr;勾选上&ldquo;Use command line client: svn&rdquo;</span><br />
这样就会告诉IntelliJ使用命令行客户端svn（也就是你安装的subversion 1.8.x）来执行subversion相关功能，这样就没有任何问题了。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;如何转换long到byte数组<br />
别折腾了，直接用<a href="https://code.google.com/p/guava-libraries/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Google Guava</span></a>库中的方法来转，一句话就可以解决：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">byte</span>[]&nbsp;converted&nbsp;=&nbsp;Longs.toByteArray(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">123L</span>);
</code></pre>
</section>
<p>简单得不能更简单了。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;求两个Set的交集<br />
用<a href="https://code.google.com/p/guava-libraries/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Google Guava</span></a>库，这个问题就变得无比简单了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">Set&lt;Integer&gt;&nbsp;set1&nbsp;=&nbsp;Sets.newHashSet(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>);
Set&lt;Integer&gt;&nbsp;set2&nbsp;=&nbsp;Sets.newHashSet(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">3</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">4</span>);
Set&lt;Integer&gt;&nbsp;intersectedSet&nbsp;=&nbsp;Sets.intersection(set1,&nbsp;set2);
System.out.println(intersectedSet);
</code></pre>
</section>
<p>输出：</p>
<blockquote>
<p>
		[2, 3]</p>
</blockquote>
<p>
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;启动Zookeeper时指定配置文件<br />
只需要简单地使用 bin 目录下的&nbsp;zkServer.sh 脚本就可以了：</p>
<blockquote>
<p>
		./zkServer.sh start ../conf/zoo.cfg</p>
</blockquote>
<p>这表示用上一级目录下的 conf 目录下的 zoo.cfg 文件作为配置文件来启动Zookeeper。<br />
<span style="font-size: 14.399999618530273px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.399999618530273px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">如何让IntelliJ识别JSP代码里的request<br />
如果你发现用IntelliJ idea无法识别JSP代码中的request对象（因此也就无法识别其方法），例如：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">String&nbsp;id&nbsp;=&nbsp;request.getParameter(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;oneId&quot;</span>);
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.399999618530273px;">这个request对象是HttpServletRequest类的实例，它实现了ServletRequest接口。确实，就算IntelliJ不能识别它，你可以把这个JSP文件部署到Tomcat中让它正确运行，但是写代码的时候多难受啊！其实，你只需要下载一个javax.servlet的jar包，添加到IntelliJ的项目的lib中，就可以了，例如javax.servlet-3.0.jar</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">ant test如何单独运行一个UT class？<br />
在一个Java工程下，ant test命令会运行全部的test case，但是如果我们只想运行一个UT class的话，可以这样：</span></p>
<blockquote>
<p>
		ant test -Dtestcase=MyTest</p>
</blockquote>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">这就会单独运行MyTest这个类名的UT。</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">Maven编译时跳过单元测试</span></p>
<blockquote>
<p>
		mvn clean install -DskipTests</p>
</blockquote>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">注意大小写。</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">JDK8下的ScriptEngine返回 null 的问题<br />
如果你在Java中执行JavaScript脚本／代码，那么你可能会写类似于下面的代码：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">ScriptEngineManager&nbsp;scriptEngineMgr&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;ScriptEngineManager();
ScriptEngine&nbsp;engine&nbsp;=&nbsp;scriptEngineMgr.getEngineByName(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;JavaScript&quot;</span>);
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">这些代码在JDK6下运行得很好，但是在JDK8下，你就会发现 engine 变量为null。<br />
首先，从JDK6升级到JDK8，engine name已经不是&ldquo;JavaScript&rdquo;而是&ldquo;nashorn&rdquo;了，其次，上面的代码还要做一些修改，如下：</span></p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">ScriptEngine&nbsp;engine&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;ScriptEngineManager(<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">null</span>).getEngineByName(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;nashorn&quot;</span>);
</code></pre>
</section>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">具体请看<a href="http://stackoverflow.com/questions/25332640/getenginebynamenashorn-returns-null" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个</span></a>链接。<br />
这样修改之后就OK了。</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;<span style="font-family: arial, helvetica, sans-serif; font-size: 14.4px;">Zookeeper客户端脚本zkCli.sh删除Zookeeper中一个节点（node）的方法</span><br />
<span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">旧版的zkCli.sh只能删除里面没有下级node的节点，用下面的方法：</span></p>
<blockquote>
<p>
		[zk: zk.codelast.com:2191(CONNECTED) 1] delete node_to_del</p>
</blockquote>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">如果<span style="color:#0000ff;">node_to_del</span>下面还有其他节点（即非空），会提示一个错误，说里面非空，不允许删除。要递归删除，只能自己写个程序了。</span><br />
<span style="font-size: 14.4px; color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" style="font-size: 14.4px;" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">而新版的zkCli.sh可以删除里面非空的节点，用下面的方法：</span></p>
<blockquote>
<p>
		[zk: zk.codelast.com:2191(CONNECTED) 1] rmr node_to_del</p>
</blockquote>
<p><span style="font-family: arial, helvetica, sans-serif; font-size: 14.3999996185303px;">其中，<span style="color:#0000ff;">node_to_del</span>节点里面可以非空。</span></p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;IntelliJ IDEA里，一个项目的所有外部依赖包都无法识别（全部红色），怎么办？<br />
这是一个非常恼人的问题，我在网上搜了很多办法都没用，包括清除缓存，重新import&nbsp;maven项目，等等，能用的招都试了一遍，但是对自己电脑上的某些特定项目，它就是有这个问题。IntelliJ IDEA的论坛里那些讨论帖子里也有很多人反映一样的问题。<br />
最后，是添加了一个 ~/.m2/settings.xml&nbsp;文件，在里面添加了某些内容之后治好的，其中有一段关键的代码是：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">mirror</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">id</span>&gt;</span>maven-central<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">id</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">mirrorOf</span>&gt;</span>central<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">mirrorOf</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">url</span>&gt;</span>http://xxx.codelast.com/repositories/releases<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">url</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">mirror</span>&gt;</span></code></pre>
</section>
<p>其中，上面的&ldquo;url&rdquo;字段要视情况改成适合你用的。</p>
<p><span style="color: rgb(255, 0, 0);"><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span></span>&nbsp;对一个list中的元素排序，排序依据是元素对象的一个double字段(从大到小)</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#bbb529;">@Test
</span><span style="color:#cc7832;">public void </span><span style="color:#ffc66d;">test</span>() <span style="color:#cc7832;">throws </span>Exception {
  <span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">测试对一个</span><span style="color:#808080;">list</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">中的元素排序</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">，排序依据是元素对象的一个</span><span style="color:#808080;">double</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">字段</span>(<span style="font-size: 12pt; color: rgb(128, 128, 128); font-family: &quot;DejaVu Sans Mono&quot;, monospace;">从小到大</span><span style="font-size: 12pt; color: rgb(128, 128, 128);">)</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">
</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">  </span>CustomObject object1 = <span style="color:#cc7832;">new </span>CustomObject().setField(<span style="color:#6897bb;">2.0</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>CustomObject object2 = <span style="color:#cc7832;">new </span>CustomObject().setField(<span style="color:#6897bb;">1.0</span>)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>CustomObject object3 = <span style="color:#cc7832;">new </span>CustomObject().setField(<span style="color:#6897bb;">3.0</span>)<span style="color:#cc7832;">;
</span>
<span style="color:#cc7832;">  </span>List&lt;CustomObject&gt; list = Lists.<span style="font-style:italic;">newArrayList</span>(object1<span style="color:#cc7832;">, </span>object2<span style="color:#cc7832;">, </span>object3)<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>list.sort(Comparator.<span style="font-style:italic;">comparingDouble</span>(CustomObject::getField))<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span><span style="color:#808080;">//Collections.reverse(list);  // </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">如果需要的话，再把排好序的</span><span style="color:#808080;">list</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">逆序
</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">  </span><span style="color:#cc7832;">for </span>(CustomObject object : list) {
    System.<span style="color:#9876aa;font-style:italic;">out</span>.println(object.getField())<span style="color:#cc7832;">;
</span><span style="color:#cc7832;">  </span>}
}

<span style="color:#bbb529;">@Data
</span><span style="color:#bbb529;">@Accessors</span>(chain = <span style="color:#cc7832;">true</span>)
<span style="color:#cc7832;">private static class </span>CustomObject {
  <span style="color:#cc7832;">private double </span><span style="color:#9876aa;">field</span><span style="color:#cc7832;">;
</span>}</pre>
<p>输出：</p>
<blockquote>
<div>
		1.0</div>
<div>
		2.0</div>
<div>
		3.0</div>
</blockquote>
<p>上面的代码是在UT中写的，CustomObject的两个annotation：@Data 和 @Accessors(chain = true) 都来自于 lombok 库。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /><br />
	&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9bjava%e5%9f%ba%e7%a1%80%e7%9f%a5%e8%af%86%e8%ae%b0%e5%bd%95%ef%bc%882%ef%bc%89/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
