<?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>添加到classpath &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%B7%BB%E5%8A%A0%E5%88%B0classpath/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:23:03 +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>[原创]如何添加一个文件/目录到IntelliJ项目的classpath中</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 03 Mar 2013 13:10:40 +0000</pubDate>
				<category><![CDATA[未分类]]></category>
		<category><![CDATA[classpath]]></category>
		<category><![CDATA[IntelliJ]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[添加到classpath]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5744</guid>

					<description><![CDATA[<p>
以下内容的背景：IntelliJ，Java项目。<br />
在单元测试中，我们经常会使用与生产环境里不同的一些配置文件来为提供参数，而你的程序又是以类似下面的方法来读取配置文件：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
String configFileName = &#34;config.properties&#34;;
Properties properties = new Properties();
properties.load(MyClass.class.getClassLoader().getResourceAsStream(configFileName));
</pre>
<p>上面的代码中仅使用了配置文件名，而不是其完整路径，这就导致了可能在IntelliJ中运行一个单独的单元测试的时候，找不到该配置文件，因为你只提供文件名的话，该文件必须要在classpath中，否则是找不到它的。<br />
<span id="more-5744"></span><br />
所以，我们可以把该配置文件所在的目录添加到IntelliJ项目里的classpath中，让它可以在运行单元测试的时候找得到。方法如下（一图胜千言，直接看下面的演示图就明白了）：<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
点击菜单File&#8594;Project Structure，进入&#8220;Project Structure&#8221;对话框，点击左侧的&#8220;Modules&#8221;，然后点击中部的&#8220;Dependencies&#8221;tab，然后再点击右部的&#8220;+&#8221;号添加你指定的目录，这里要注意了，在弹出的&#8220;Attach Files&#8221;对话框中，你必须要选择&#8220;classes&#8221;（如图所示）。最后，把目录添加进来之后，再把它们对应的&#8220;Scope&#8221;设置为&#8220;Test&#8221;，表示在测试中会依赖这个目录，即可。<br />
<img decoding="async" alt="add a file or directory to the classpath of an IntelliJ Java project" src="http://www.codelast.com/wp-content/uploads/2013/03/project_structure_with_demo_final.png" style="width: 576px; height: 218px; " /><br />
<strong><span style="color:#ff0000;">图片可保存下来放大查看。</span></strong></p>
<p>以上方法在IntelliJ 12.0.4中亲测通过，对于其他版本的IntelliJ也应该类似。</p>
<p><span style="background-color:#00ff00;">【补充】</span><br />
12.1.4版的IntelliJ与上面的图略有不同。在点击了&#8220;+&#8221;号之后，会弹出一个菜单：</p>
<div style="text-align: center;">
	<img decoding="async" alt="add menu" src="http://www.codelast.com/wp-content/uploads/2013/03/add_menu.png" style="width: 158px; height: 89px;" /></div>
<p>选择&#8220;Jars or directories...&#8221;，然后会弹出对话框让你选择目录或文件，选择好之后，和上面一样，要选择&#8220;classes&#8221;，后面的操作也一样了。</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>&#160;版权声明&#160;<span style="color: rgb(255, 0, 0);">➤➤</span>&#160;<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>&#160;<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>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']</script>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
以下内容的背景：IntelliJ，Java项目。<br />
在单元测试中，我们经常会使用与生产环境里不同的一些配置文件来为提供参数，而你的程序又是以类似下面的方法来读取配置文件：</p>
<pre class="brush:java;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
String configFileName = &quot;config.properties&quot;;
Properties properties = new Properties();
properties.load(MyClass.class.getClassLoader().getResourceAsStream(configFileName));
</pre>
<p>上面的代码中仅使用了配置文件名，而不是其完整路径，这就导致了可能在IntelliJ中运行一个单独的单元测试的时候，找不到该配置文件，因为你只提供文件名的话，该文件必须要在classpath中，否则是找不到它的。<br />
<span id="more-5744"></span><br />
所以，我们可以把该配置文件所在的目录添加到IntelliJ项目里的classpath中，让它可以在运行单元测试的时候找得到。方法如下（一图胜千言，直接看下面的演示图就明白了）：<br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
点击菜单File&rarr;Project Structure，进入&ldquo;Project Structure&rdquo;对话框，点击左侧的&ldquo;Modules&rdquo;，然后点击中部的&ldquo;Dependencies&rdquo;tab，然后再点击右部的&ldquo;+&rdquo;号添加你指定的目录，这里要注意了，在弹出的&ldquo;Attach Files&rdquo;对话框中，你必须要选择&ldquo;classes&rdquo;（如图所示）。最后，把目录添加进来之后，再把它们对应的&ldquo;Scope&rdquo;设置为&ldquo;Test&rdquo;，表示在测试中会依赖这个目录，即可。<br />
<img decoding="async" alt="add a file or directory to the classpath of an IntelliJ Java project" src="http://www.codelast.com/wp-content/uploads/2013/03/project_structure_with_demo_final.png" style="width: 576px; height: 218px; " /><br />
<strong><span style="color:#ff0000;">图片可保存下来放大查看。</span></strong></p>
<p>以上方法在IntelliJ 12.0.4中亲测通过，对于其他版本的IntelliJ也应该类似。</p>
<p><span style="background-color:#00ff00;">【补充】</span><br />
12.1.4版的IntelliJ与上面的图略有不同。在点击了&ldquo;+&rdquo;号之后，会弹出一个菜单：</p>
<div style="text-align: center;">
	<img decoding="async" alt="add menu" src="http://www.codelast.com/wp-content/uploads/2013/03/add_menu.png" style="width: 158px; height: 89px;" /></div>
<p>选择&ldquo;Jars or directories...&rdquo;，然后会弹出对话框让你选择目录或文件，选择好之后，和上面一样，要选择&ldquo;classes&rdquo;，后面的操作也一样了。</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>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushJava.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6%e7%9b%ae%e5%bd%95%e5%88%b0intellij%e9%a1%b9%e7%9b%ae%e7%9a%84classpath%e4%b8%ad/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
