如何将目录添加到eclipse类路径? [英] How do I add a directory to the eclipse classpath?

查看:225
本文介绍了如何将目录添加到eclipse类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一个已经存在的eclipse项目,由另一个人创建。

导入到eclipse并尝试运行As-> Java应用程序后,它失败,因为它找不到。 bin / resources中的属性文件



我打印出类路径eclipse正在使用

  logger.info(System.getProperty( java.class.path)); 

确实,它包括bin和所有的lib / *。jar,但不是bin /资源。将.properties文件复制到bin可使程序工作,但我想了解如何向eclipse类路径添加目录。



我尝试了几件事情,其中​​没有一项工作

  export CLASSPATH = $ CLASSPATH:/ home / me / programdir / bin / resources 

这没有工作。我理解这不是一个理想的方法来解决这个问题,但是我以为会解决这个问题(我有更多的Windows,而不是linux的背景,所以也许我在linux中缺少一些细微的系统变量)



接下来,我尝试在Eclipse中的Run-> Configurations对话框中修改VM参数。

  -classpath/ home / me / programdir / bin / resources

困惑我,因为我确信它会工作,似乎是一个合理的解决方案,一个特定的程序需要一个额外的文件夹添加到类路径。



接下来我尝试直接修改build.xml。我找到了定义类路径的部分,并为bin / resources添加了自己的行,如下所示:

 < path id = 类路径 > 
< fileset dir =./ bin / resourcesincludes =** / *。properties/>
< fileset dir =./ libincludes =** / *。jar/>
< / path>

这也是不成功的。这让我感到困惑,所以我评论了整个路径元素,并且记录器打印出来的类路径没有改变,所以很明显,无论是classpath eclipse都在使用,这当然不是这样的。这对我来说似乎是最好的解决方案,它的工作原理是:可以使用正确的添加来检查build.xml文件,以防止未来的用户遇到问题。



接下来我尝试了IDE方法。运行 - >配置 - >类路径 - >用户条目 - >高级,并简单地添加bin / resources文件夹。这样很好,程序找到属性文件,一切都很好。但是,我不满意我以前的努力失败,没有我真正理解为什么。似乎每个人都应该工作。



此外,我想确保我以这样一种方式解决这个问题,这是由我签入的代码所捕获的,以便随后的用户不必经过相同的步骤。因此,我的解决方案不是很令人满意,因为我不知道实际的代码段是否改变,因此无法验证是否检查了修复。



找到eclipse正在为其类路径使用的实际定义?我以为这将是build.xml类路径定义,但根本看来并不是这样。

解决方案

在Eclipse中,有一个构建类路径和一个运行时类路径。还有构建输出位置,默认情况下是 bin 。您不希望将资源直接添加到 bin ,因为Eclipse在执行干净的构建时可以删除其内容。您需要做的是在项目中添加一个资源文件夹,以包含您希望在构建输出中包含的任何非Java文件。



要在构建输出( bin )中包含此资源文件夹的内容,右 - 点击项目并选择属性。在项目属性中,选择 Java构建路径部分,然后选择选项卡。





使用添加文件夹.. 。按钮从项目中选择资源文件夹,然后选择确定保存更改。在这一点上,Eclipse将自动将所有内容从资源复制到 bin 中。


I'm attempting to run an already existing eclipse project created by another person.
After importing it to eclipse, and attempting to Run As->Java Application, it fails because it cannot find a .properties file in bin/resources

I printed out the classpath eclipse was using

logger.info(System.getProperty("java.class.path"));

and sure enough, it includes bin, and all the lib/*.jars, but not bin/resources. Copying the .properties file into bin makes the program work, but I wanted to understand how to add a directory to the eclipse classpath.

I tried several things, none of which worked

export CLASSPATH=$CLASSPATH:/home/me/programdir/bin/resources

This didn't work. I understand it's not a desirable way to approach the issue, but I had thought it would fix the problem (I have more of a windows than linux background so perhaps I am missing some nuances of system variables in linux)

Next up, I tried modifying the VM arguments in the Run->Configurations dialog in Eclipse

-classpath "/home/me/programdir/bin/resources"

No luck here either, which confused me, as I was sure it would work and seemed like a reasonable solution to a specific program needing one additional folder added to the classpath.

Next I tried modifying build.xml directly. I found the part that defines the classpath and added my own line for bin/resources, as follows:

<path id="classpath">
    <fileset dir="./bin/resources" includes="**/*.properties"/>
    <fileset dir="./lib" includes="**/*.jar" /> 
</path>

this too was unsuccessful. This perplexed me even more, so I commented out the entire path element, and the classpath printed out by the logger was unchanged, so it is apparent that whatever classpath eclipse was using, it certainly wasn't this one. This seemed to me the best solution, had it worked: the build.xml file could be checked in with the correct additions to prevent future users from experiencing the problem.

Next I tried the IDE approach. Run->Configurations->Classpath-> User Entries->Advanced and simply added the bin/resources folder. That worked perfectly, the program finds the properties file, all is well. However, I am dissatisfied that my previous efforts failed without me really understanding why. It seems that each one should have worked.

Additionally, I want to ensure that I fix this problem in such a way that it is captured by the code I check in so that subsequent users do not have to go through the same steps. My solution is thus not very satisfactory as I am not sure what actual piece of code changed, and thus cannot verify that the 'fix' is checked in.

How do you find the actual definition that eclipse is using for its classpath? I had thought it would be the build.xml classpath definition, but that did not seem to be the case at all.

解决方案

In Eclipse, there is a build classpath and a runtime classpath. There is also the build output location, which by default is bin. You don't want to add resources directly to bin because Eclipse can delete its contents when doing a clean build. What you need to do is add a resources folder in your project to contain any non-Java files that you want included in your build output.

To include the contents of this resources folder in the build output (bin), right-click the project and select Properties. In the Project Properties, select the Java Build Path section, then the Source tab.

Use the Add Folder... button to select the resources folder from your project, then OK to save the changes. At that point, Eclipse will automatically copy everything from resources into bin when it builds.

这篇关于如何将目录添加到eclipse类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆