在 Eclipse 中运行时加载资源时出现问题
我正在开发一个 swing 项目,使用 maven2 (从命令行)和 eclipse (没有 Maven 集成)。因此,我通过 maven eclipse 插件 (mvn eclipse:eclipse
) 生成 eclipse 项目,将其导入到 eclipse 中,然后完成所有工作。
我的问题是:当我在 eclipse 中运行我的应用程序(作为 Java 应用程序)时,我找不到 src 目录中的任何资源。挖掘有关我的问题的信息,我进入 这个答案来自另一个问题。因此,我比较了以下指令的输出:
MyClass.class.getResource("/").getPath();
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toString();
这些指令分别给出了以下输出:
${workspace_loc}/${my_project}/target/test-classes/ 文件:/${workspace_loc}/${my_project}/target/classes/
检查上述位置,我可以看到前者是空的,而另一个包含我编译的所有类和资源。所以,我得出的结论是类加载器正在错误的位置寻找我的资源。所以,我想我有三个问题:
- 我的理解是否正确?
- 如果是这样,它如何找到正在加载的类?
- 我该如何解决这个问题?
更新:我更改了代码,因此我没有调用 MyClass.class.getResource(...)
或 MyClass.class.getResourceAsStream(...)
,而是调用现在使用 ClassLoader.getSystemResource(...)
和 ClassLoader.getSystemResourceAsStream(...)
。这样,在eclipse中就一切正常了。我只是不知道到底为什么。有什么提示吗?
I'm working on a swing project, using maven2 (from command-line) and eclipse (without maven integration). So, I generate the eclipse project through maven eclipse plugin (mvn eclipse:eclipse
), import it inside eclipse, and do all my work.
My problem is: when I run my app in eclipse (as a Java Application), I can't find none of the resources that are in my src directory. Digging for information on my problem, I get into this answer from another question. So, I compared the output from the following instructions:
MyClass.class.getResource("/").getPath();
MyClass.class.getProtectionDomain().getCodeSource().getLocation().toString();
Those gave me the following outputs, respectively:
${workspace_loc}/${my_project}/target/test-classes/
file:/${workspace_loc}/${my_project}/target/classes/
Checking the above locations, I could see that the former is empty, while the other one contained all my compiled classes and resources. So, I came to the conclusion that the classloader is looking for my resources in the wrong place. So, I think I have three questions:
- Is my understanding correct?
- If so, how it does to find the classes it is loading?
- How do I solve this?
UPDATE: I've changed my code, so instead of invoking MyClass.class.getResource(...)
or MyClass.class.getResourceAsStream(...)
, I'm now using ClassLoader.getSystemResource(...)
and ClassLoader.getSystemResourceAsStream(...)
. In this way, everything is working fine in eclipse. I just don't know exactly why. Any hint on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对我来说有两种可能的情况:
您正在使用 eclipse 来编译您的项目。然后将 eclipse 配置为排除(或不包含)src 文件夹中的资源。您可以在 Project/Properties/Java Build Path/Source 中设置它。然后展开 src 文件夹,并确保有类似“包含所有”、“排除无”之类的内容。
即使您使用 eclipse 作为 IDE,您也可以使用 maven 来编译而不是 eclipse 来运行您的 Maven 应用程序。那么默认情况下,maven 不会将资源从源文件夹复制到输出文件夹...因为它不是标准的 maven 处理方式。因此,类路径中缺少该资源,并且您找不到它们。只需更改您的 Maven 配置以包含源文件夹中的资源即可。
Two possibles cases for me :
You are using eclipse to compile your project. Then eclipse is configured to exclude (or not include) resources in the src folder. You can set it in Project/Properties/Java Build Path/Source. Then you expend your src folder, and ensure you have something like "Included All", "Excluded None".
You run your maven application using maven to compile and not eclipse, even though you are using eclipse as your IDE. Then by default maven will not copy resources from the source folder to the output folder... Because it is not the standard maven way of doing things. Thus the resource are missing from the classpath and you don't find them. Just change your maven configuration to also include resources from your source folder.
我认为您必须将 src/main/resources 添加到构建路径。这是在
Project Properties > 中完成的构建路径>来源
。以下是标准 Maven 项目的外观:将来使用 Eclipse 时,我建议使用 m2eclipse 插件并创建使用它的项目。这将自动确保所有这些文件夹都位于构建路径中。
I think you have to add
src/main/resources
to the build path. This is done inProject Properties > Build Path > Source
. Here is how the standard maven project looks:In future when using Eclipse I suggest to use m2eclipse plugin and create project using it. This will automatically make sure that all these folders are in the build path.
试试这个:
运行配置...->类路径->用户条目 ->高级...->添加文件夹
Try this:
Run configurations... -> Classpath -> User Entries -> Advanced... -> Add Folders
您可以尝试使用下面的方法加载资源吗?
Can you try loading the resources using below?
这可能是有用的信息
Eclipse Maven 插件有自己的类路径容器,在启用时与生成的类路径冲突。
This might be useful information
Eclipse Maven plugin has its own Classpath Container that conflicts with generated class paths when enabled.
我建议停止使用 eclipse:eclipse (抱歉 - 我知道很严厉)。我使用了大约 8 个月,不敢相信我花了这么长时间,并使用了 M2Eclipse。 M2Eclipse 是一个 Eclipse 插件,您可以在其中执行以下操作。
看起来似乎并非如此,但这是一种更简单、更轻松的方法。
之前,用 eclipse:eclipse。您必须运行它,然后点击刷新并希望一切都已配置 - 如果您有一个多模块 pom,事情可能真的会出错。
What I would suggest is stop using eclipse:eclipse (sorry - harsh I know). I used it for about 8 months, cant believe it took me that long, and used M2Eclipse. M2Eclipse is an eclipse plugin in which you do the following.
It may not seem it, but its a much easier and less frustrating way of doing it.
Before, with eclipse:eclipse. You would have to run it then hit refresh and hope that everything is configured - if you have a multi-module pom things can really go wrong.
对我来说,WEB-INF 中的文件没有被包含在内。因此,我将它们添加到 Buildpath(项目 -> 构建路径 -> 配置构建路径 -> 添加文件夹(project/src/main/webapp)。这解决了问题。
For me the Files within the WEB-INF were not getting included. Hence I added them to Buildpath (Project -> Build Path -> Configure Build Path -> Add folder (project/src/main/webapp). This resolved the issue.