<?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/%E5%8A%A8%E6%80%81/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:11:26 +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动态添加一个目录到classpath中</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 10 Apr 2014 06:43:03 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[classpath]]></category>
		<category><![CDATA[getResourceAsStream]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[动态]]></category>
		<category><![CDATA[动态添加]]></category>
		<category><![CDATA[添加目录到classpath]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8104</guid>

					<description><![CDATA[<p>
通常我们写程序，会用到一些配置文件，这些文件与程序在同一个父目录下，配置文件可能放在config之类的目录下，程序中会使用这些配置文件。<br />
我遇到了这么件事情：因为一个特殊需求，需要把配置文件放到jar包中，最终部署出去的只能是一个jar包（所有东西都在jar包里，包括程序和所需的资源），然后在程序中使用&#160;<span style="color:#0000ff;">getClass().getResourceAsStream(文件在jar包中的路径)</span> 的方式来读取配置文件。这也倒没什么不爽的，但是在单元测试中怎么办？<br />
<span id="more-8104"></span><br />
单元测试需要在classpath中能找到同样目录结构下的文件，就需要把你的配置文件路径加入classpath，这里你有几种方法：<br />
<span style="background-color:#00ff00;">『1』</span>修改build.xml<br />
在build.xml中可以指定run test时的classpath，你只要把那个目录添加进去就可以了。这样，nightly build（例如Jenkins）就不会失败，但是这种方法是有局限性的，即：你在命令行运行 ant test没有任何问题，但是你如果你的Java工程使用的是某IDE来开发（例如，IntelliJ 或 Eclipse 等），它们在没有特殊设置的情况下，在classpath中可能是找不到那个配置文件路径的，这时，你可以像<a href="http://www.codelast.com/?p=5744" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章一样设置一下，单元测试就没问题了。<br />
不过显然，第一次从代码库checkout出项目之后，还必须要设置一下总是让人不爽的，尤其是不熟悉项目的人，怎么会知道该添加哪个路径到classpath呢？<br />
所以这种方法就不详述了，我觉得不好。</p>
<p><span style="background-color:#00ff00;">『2』</span>修改IDE的项目设置<br />
如上所述，修改IDE的项目设置可以使得在IDE中run test的时候通过，但是对诸如Jenkins之类的工具执行的nightly build（最终是通过build.xml实现的），仅仅修改IDE设置是没用的，nightly build依然会挂掉。所以这种方法只能保证&#8220;临时用用&#8221;。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>动态添加目录到classpath中<br />
本文的主角登场了。<br />
如果我们能在单元测试中，添加一些代码，动态地将需要的目录添加到classpath中，那么问题不就从最根本上解决了吗？不仅不用修改IDE里的项目设置，也不用修改build.xml，好多啊。是的，你只需要在单元测试中的setUp()里添加下面的代码：</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-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Before
</span>
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setUp</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&#160;Exception&#160;</span>{
&#160;&#160;&#160;&#160;File&#160;programRootDir&#160;=&#160;<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>&#160;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;">&#34;./&#34;</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
通常我们写程序，会用到一些配置文件，这些文件与程序在同一个父目录下，配置文件可能放在config之类的目录下，程序中会使用这些配置文件。<br />
我遇到了这么件事情：因为一个特殊需求，需要把配置文件放到jar包中，最终部署出去的只能是一个jar包（所有东西都在jar包里，包括程序和所需的资源），然后在程序中使用&nbsp;<span style="color:#0000ff;">getClass().getResourceAsStream(文件在jar包中的路径)</span> 的方式来读取配置文件。这也倒没什么不爽的，但是在单元测试中怎么办？<br />
<span id="more-8104"></span><br />
单元测试需要在classpath中能找到同样目录结构下的文件，就需要把你的配置文件路径加入classpath，这里你有几种方法：<br />
<span style="background-color:#00ff00;">『1』</span>修改build.xml<br />
在build.xml中可以指定run test时的classpath，你只要把那个目录添加进去就可以了。这样，nightly build（例如Jenkins）就不会失败，但是这种方法是有局限性的，即：你在命令行运行 ant test没有任何问题，但是你如果你的Java工程使用的是某IDE来开发（例如，IntelliJ 或 Eclipse 等），它们在没有特殊设置的情况下，在classpath中可能是找不到那个配置文件路径的，这时，你可以像<a href="http://www.codelast.com/?p=5744" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章一样设置一下，单元测试就没问题了。<br />
不过显然，第一次从代码库checkout出项目之后，还必须要设置一下总是让人不爽的，尤其是不熟悉项目的人，怎么会知道该添加哪个路径到classpath呢？<br />
所以这种方法就不详述了，我觉得不好。</p>
<p><span style="background-color:#00ff00;">『2』</span>修改IDE的项目设置<br />
如上所述，修改IDE的项目设置可以使得在IDE中run test的时候通过，但是对诸如Jenkins之类的工具执行的nightly build（最终是通过build.xml实现的），仅仅修改IDE设置是没用的，nightly build依然会挂掉。所以这种方法只能保证&ldquo;临时用用&rdquo;。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>动态添加目录到classpath中<br />
本文的主角登场了。<br />
如果我们能在单元测试中，添加一些代码，动态地将需要的目录添加到classpath中，那么问题不就从最根本上解决了吗？不仅不用修改IDE里的项目设置，也不用修改build.xml，好多啊。是的，你只需要在单元测试中的setUp()里添加下面的代码：</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-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">@Before
</span>
<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">public</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">void</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">setUp</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">throws</span>&nbsp;Exception&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;programRootDir&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;./&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;URLClassLoader&nbsp;classLoader&nbsp;=&nbsp;(URLClassLoader)&nbsp;ClassLoader.getSystemClassLoader();
&nbsp;&nbsp;&nbsp;&nbsp;Method&nbsp;add&nbsp;=&nbsp;URLClassLoader.class.getDeclaredMethod(<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;addURL&quot;</span>,&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;Class[]{URL.class});
&nbsp;&nbsp;&nbsp;&nbsp;add.setAccessible(<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;">true</span>);
&nbsp;&nbsp;&nbsp;&nbsp;add.invoke(classLoader,&nbsp;programRootDir.toURI().toURL());
}
</code></pre>
</section>
<p>就可以把目录动态添加到classpath中了。在这里，代码中的&ldquo;./&rdquo;表示我把项目的根目录添加到了classpath中。<br />
修改之后，无论是在IDE中run unit test，还是在命令行ant test，都完全没问题了。</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;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-java%e5%8a%a8%e6%80%81%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e7%9b%ae%e5%bd%95%e5%88%b0classpath%e4%b8%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
