正在加载资源?
我确信这是一个愚蠢的问题,但我已经尝试了很多方法,但我似乎无法理解。这是我所拥有的。
我在 eclipse 中有一个项目。结构如下。
/main/java(指定src文件夹) /test/java(指定src文件夹) /test/java 分支中的 /test/resources
我拥有所有 JUnit,当然,它们测试 /main/java 中找到的类。资源文件夹用于存储单元测试所需的项目。我正在使用 JAXB,因此它包含示例 XSD 和 XML 文件。现在我在那里有一个文件..
/test/resources/schema/sample.xsd
现在,我创建了一个类,其中包含一个实例化类的 main 方法,然后执行唯一的方法。我正在尝试执行以下操作:
URL xsdFile = getClass().getClassLoader().getResource( "/resources/schema/sample.xsd");
但我得到 null 作为返回值。我也尝试过/schema/sample.xsd,但也没有运气。我修改了项目构建设置,确保 /test/resources/schema 位于构建路径中,但仍然没有乐趣。当我运行主类(从 Eclipse 中)时,它无法获取该文件。
有人可以帮忙吗?
I'm sure this is a silly question, but I've tried a multitude of things and I just can't seem to get it. Here is what I have.
I have a project in eclipse. The structure is as follows.
/main/java (designated src folder)
/test/java (designated src folder)
/test/resources
in the /test/java branch I have all of my JUnits which, of course, test the classes found in the /main/java. The resources folder is used to store items that I need for my unit tests. I'm working with JAXB, so it contains sample XSD and XML files. Right now I have one file in there..
/test/resources/schema/sample.xsd
Now, I have created a class with a main method that instantiates a class, and then executes the only method. I am trying to do:
URL xsdFile = getClass().getClassLoader().getResource( "/resources/schema/sample.xsd");
but I am getting null as a returned value. I've also tried /schema/sample.xsd, but no luck there either. I'm modified the project build settings ensuring that /test/resources/schema is in the build path, but still no joy. When I run the main class (from within eclipse), it just can't pick up the file.
Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将资源作为源文件夹(右键单击->构建路径->用作源文件夹)并使用 ...getResource( "schema/sample.xsd");
Make resources as a source folder (Right click-> Build Path-> Use as source folder) and use ...getResource( "schema/sample.xsd");