在Eclipse中运行时加载资源时遇到问题 [英] Problem loading resources while running in Eclipse

查看:393
本文介绍了在Eclipse中运行时加载资源时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个秋千项目,使用maven2(从命令行)和eclipse(没有maven集成)。所以,我通过maven eclipse插件( mvn eclipse:eclipse )生成eclipse项目,在eclipse中导入它,并做所有的工作。 / p>

我的问题是:当我在eclipse(作为Java应用程序)中运行我的应用程序时,找不到我的src目录中的任何资源。挖掘有关我的问题的信息,我进入另一个问题的答案。所以,我比较了以下说明的输出:

  MyClass.class.getResource(/)。 
MyClass.class.getProtectionDomain()。getCodeSource()。getLocation()。toString();

分别给出以下输出:



/ $ {工作空间_ /} / $ {$ _ >

检查上述位置,我可以看到前者是空的,而另一个包含我编译的所有类和资源。所以,我得出结论,类加载器正在寻找我的资源在错误的地方。所以,我想我有三个问题:


  1. 我的理解是否正确?

  2. 如果是,如何找到正在加载的课程?

  3. 我该如何解决?

更新:我已经改变了我的代码,所以代替调用 MyClass.class.getResource(...) MyClass.class.getResourceAsStream ...),我现在使用 ClassLoader.getSystemResource(...) ClassLoader.getSystemResourceAsStream(。 ..)。以这种方式,一切都在日食中正常工作。我只是不知道为什么。任何提示?

解决方案

两个可能的情况为我:




  • 您正在使用eclipse来编译项目。然后,eclipse配置为在src文件夹中排除(或不包括)资源。您可以在Project / Properties / Java Build Path / Source中进行设置。然后你花费你的src文件夹,并确保你有一些类似全部包含,排除无。


  • 你使用maven运行你的maven应用程序来编译而不是eclipse,即使你使用eclipse作为你的IDE。那么默认情况下,maven将不会将资源从源文件夹复制到输出文件夹...因为它不是标准的maven做事情的方式。因此,类路径中缺少资源,您没有找到它们。只需更改您的maven配置即可包含源文件夹中的资源。



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:

  1. Is my understanding correct?
  2. If so, how it does to find the classes it is loading?
  3. 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?

解决方案

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.

这篇关于在Eclipse中运行时加载资源时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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