如何将目录添加到 eclipse 类路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25162773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:22:52  来源:igfitidea点击:

How do I add a directory to the eclipse classpath?

eclipseclasspath

提问by user2197116

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

我正在尝试运行由另一个人创建的现有 eclipse 项目。
将其导入 eclipse 并尝试运行 As->Java Application 后,它失败了,因为它在 bin/resources 中找不到 .properties 文件

I printed out the classpath eclipse was using

我打印了 eclipse 正在使用的类路径

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.

果然,它包括 bin 和所有 lib/*.jars,但不包括 bin/resources。将 .properties 文件复制到 bin 使程序工作,但我想了解如何将目录添加到 eclipse 类路径。

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)

这没有用。我知道这不是解决问题的理想方法,但我曾认为它可以解决问题(我有更多的 Windows 而不是 linux 背景,所以也许我错过了 linux 中系统变量的一些细微差别)

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

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

-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:

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

<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.

这也是不成功的。这让我更加困惑,所以我把整个路径元素都注释掉了,记录器打印出来的类路径没有改变,所以很明显,无论eclipse使用的是什么类路径,它肯定不是这个。这对我来说似乎是最好的解决方案,如果它有效:可以使用正确的添加项检入 build.xml 文件,以防止将来的用户遇到问题。

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.

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

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.

您如何找到 eclipse 用于其类路径的实际定义?我原以为这将是 build.xml 类路径定义,但似乎根本不是这种情况。

回答by E-Riz

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 binbecause Eclipse can delete its contents when doing a clean build. What you need to do is add a resourcesfolder in your project to contain any non-Java files that you want included in your build output.

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

To include the contents of this resourcesfolder in the build output (bin), right-click the project and select Properties. In the Project Properties, select the Java Build Pathsection, then the Sourcetab.

resources在构建输出 ( bin) 中包含此文件夹的内容,请右键单击该项目并选择 Properties。在 Project Properties 中,选择Java Build Path部分,然后选择Source选项卡。

enter image description here

在此处输入图片说明

Use the Add Folder...button to select the resourcesfolder from your project, then OKto save the changes. At that point, Eclipse will automatically copy everything from resourcesinto binwhen it builds.

使用Add Folder...按钮resources从您的项目中选择文件夹,然后单击 OK保存更改。那时,Eclipse 会在构建时自动将所有内容复制resourcesbin其中。

回答by user3324410

This is for a maven project:

这是一个 Maven 项目:

  1. Right click on project
  2. click on run configurations
  3. click on the classpath tab (Oxygen Eclipse)
  4. click on user entries
  5. click on Advanced
  6. first radio selection default should be 'Add Folders'
  7. click OK
  1. 右键单击项目
  2. 点击运行配置
  3. 单击类路径选项卡(Oxygen Eclipse)
  4. 单击用户条目
  5. 点击高级
  6. 第一个单选选项默认应该是“添加文件夹”
  7. 单击确定

回答by Rajesh Guna

Follow these steps to get this issue fixed:

请按照以下步骤修复此问题:

  1. Right click on Project
  2. Click on Run As and select Run Configurations
  3. Click on the classpath tab (Oxygen Eclipse)
  4. Click on user entries
  5. Click on Add External JARs.. and choose the downloaded JAR file
  6. Click Apply and run your project...
  1. 右键单击项目
  2. 单击运行方式并选择运行配置
  3. 单击类路径选项卡(Oxygen Eclipse)
  4. 单击用户条目
  5. 单击 Add External JARs.. 并选择下载的 JAR 文件
  6. 单击应用并运行您的项目...

回答by Vivek Vardhan

Right Click on the project-namein Package Explorer, select Properties, select Java Build Pathon the left, select Sourcetab on the right, click on Add Folder, browse through the project's directories to select the resources folder or whatever you need to add to the eclipse classpath, hit OK, again hit OK. Done.

右键点击项目名称包资源管理器,选择属性,选择Java构建路径左边,选择来源右侧选项卡,点击添加文件夹,浏览项目的目录选择,或是你需要添加文件夹中的资源到 eclipse 类路径,点击OK,再次点击OK。完毕。