朱尼特getResourceAsStream 返回 Null

发布于 2024-10-21 01:54:53 字数 384 浏览 1 评论 0原文

不知道这怎么可能。我重新阅读了 getResourceAsStream,它总是返回 null。

InputStream source = this.getClass().getResourceAsStream("test.xml");

Finder(使用 OS X 和 Eclipse)中的 test.java 旁边是 test.xml,

我可以在 TextWrangler 中打开它,并查看它是否存在,其中包含数据。

如果有什么区别的话,这是一个 Junit 测试。我查看了我们系统上现有的 Junit 测试,并以与工作示例完全相同的方式使用它(如文件所在位置和代码本身)。

有什么细微的差别会阻止我假设 getClass() 返回正确的路径?

Not sure how this is possible. I re-read up on getResourceAsStream and it's always returning null.

InputStream source = this.getClass().getResourceAsStream("test.xml");

Right next to test.java in the Finder (using OS X and Eclipse) is test.xml

I can open it in TextWrangler and view it as existing with data inside.

This is a Junit test if it makes any difference. I went and looked at existing Junit tests on our system and I'm using it in the exactly same manner as a working example (as in where the file is located and the code itself).

What small difference could there be preventing I assume getClass() from returning the right path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

别念他 2024-10-28 01:54:53

它没有在类路径上找到资源。如果您使用 junit 和 maven,请确保通过在 部分添加 文件指令将资源复制到目标/测试类上,

您可以 来找出类在文件系统中的位置

this.getClass().getResource(".")

还可以通过使用和检查资源是否存在

It's not finding the resource on the classpath. If you are using junit and maven make sure the resources are copied on the target/test-classes by adding <include> file directive on <testResource> section

You can also find out the location of your class in the file system by using

this.getClass().getResource(".")

and checking to see if the resource is there

饮惑 2024-10-28 01:54:53

如果您使用 Maven,请将此部分添加到您的 pom.xml

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
    </testResources>
</build>

您的 test.xml 和其他资源文件必须位于 src/test/resources< /代码>

In case you are using Maven, add this part to your pom.xml

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
    </testResources>
</build>

Your test.xml and other resource files must be located in src/test/resources

爱她像谁 2024-10-28 01:54:53

getResourceAsStream() 使用 CLASSPATH,因此它将从无论您的类在哪里加载,而不是从源文件加载。

我怀疑您需要将 XML 复制到 .class 文件所在的目录中。

getResourceAsStream() is using the CLASSPATH, and as such it will load from wherever your classes are, not your source files.

I suspect you need to copy your XML to the same directory as your .class file.

嗼ふ静 2024-10-28 01:54:53

我总是对这个方法有问题。这里有两个链接,可能有用:

我总是尝试在开头添加“/”或“./”。

根据我的经验,最好的方法是使用 FileInputStream。只需记住一件事(在 Eclipse 中使用 FileInputStream 时),使用默认设置,您的工作目录设置为项目根目录。您始终可以使用这段来检查当前目录在哪里(以及您需要什么相对路径)代码

I always have problem with this method. Here are 2 links, which might be useful:

I always experiment with adding "/" at the beginning or "./".

From my experience the best method is using FileInputStream. There is only one thing to remember (while using FileInputStream with Eclipse), with default settings, your working directory is set to projects root. You can always check where is your current directory (and what relative paths you need)using this piece of code.

漆黑的白昼 2024-10-28 01:54:53

假设 test.xml 位于您的 test 根源文件夹下,请执行以下操作:-

InputStream source = this.getClass().getClassLoader().getResourceAsStream("test.xml");

Assuming test.xml is located right under your test root source folder, do this:-

InputStream source = this.getClass().getClassLoader().getResourceAsStream("test.xml");
唱一曲作罢 2024-10-28 01:54:53

尝试使用类加载器

InputStream source = this.getClass().getClassLoader().getResourceAsStream("test.xml");

try using classloader

InputStream source = this.getClass().getClassLoader().getResourceAsStream("test.xml");
空城缀染半城烟沙 2024-10-28 01:54:53

我在这个问题上花了很多时间,但缺少以下配置:
右键单击 Eclipse 项目并选择属性 -> Java 构建路径 ->来源并编辑包含行并添加 *.properties (*.fileExtension)

I spent lot of time in this problem and it was missing me the following configuration:
Right-click on an eclipse project and select Properties -> Java Build Path -> Source and edit Included row and add *.properties (*.fileExtension)

棒棒糖 2024-10-28 01:54:53

将 test.xml 放在 src/main/resources (或 src/test/resources)中。

File file = ResourceUtils.getFile("classpath:test.xml");
String test = new String(Files.readAllBytes(file.toPath()));

Put test.xml in the src/main/resources (or src/test/resources).

File file = ResourceUtils.getFile("classpath:test.xml");
String test = new String(Files.readAllBytes(file.toPath()));
做个ˇ局外人 2024-10-28 01:54:53

尝试 MyClass.getResourceAsStream()。

还可以尝试将 test.xml 放入类路径中。例如,在 Eclipse Web 项目中将 text.xml 放在 webcontent/WEB-INF/classes 中

Try MyClass.getResourceAsStream().

Also try putting the test.xml in your classpath. For instance, in an Eclipse web project put text.xml in webcontent/WEB-INF/classes

沙与沫 2024-10-28 01:54:53

来自 Java API:

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemResource(java.lang.String)

从用于加载类的搜索路径。此方法通过系统类加载器定位资源,

因此语法如下:
ClassLoader.getSystemResource("test.xml").toString();

效果就像一个魅力!

From Java API:

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemResource(java.lang.String)

Find a resource of the specified name from the search path used to load classes. This method locates the resource through the system class loader

So the syntax will for instance be:
ClassLoader.getSystemResource("test.xml").toString();

Works like a charm!

×眷恋的温暖 2024-10-28 01:54:53

似乎有很多可能的原因,但这里没有找到我的原因。我发现我的测试不再被 maven 检测/运行,并且 this baeldung帖子还提供了很多可能的原因。我的问题最终是在某个时候我更改了错误的 pom 并将我的打包设置为 pom

测试编译步骤被完全跳过,并且类路径中没有新资源。因此,请检查您的 target/test-classes 目录,并确保您的测试实际上正在编译。

Seems there are a lot of possible causes to this and mine was not found here. I figured out that my tests were no longer being detected/run by maven and this baeldung post also provided a lot of possible causes. My problem ended up being that at some point I altered the wrong pom and set my packaging to pom.

The test-compile step was being skipped completely and no new resources were ending up in the classpath. So, check your target/test-classes directory and make sure that your tests are actually compiling in the first place.

白云悠悠 2024-10-28 01:54:53

我遇到了类似的问题。我的例子中的问题是 getResourceAsStream() 对于主应用程序代码工作正常,但在单元测试中给出 Null 。问题是,对于主应用程序代码,它在 target/classes 文件夹中查找文件,但对于单元测试,它在 target/test-classes 文件夹中查找>。我通过将文件添加到 test/resources 文件夹中解决了该问题。

I got a Similar issue. The problem in my case was that getResourceAsStream() was working fine for the main Application code but giving Null in case of unit-test. The problem was that for the main Application code it was looking in the target/classes folder for the file but for the unit-test it was looking in the target/test-classes. I fixed the problem by adding my file in the test/resources folder.

老子叫无熙 2024-10-28 01:54:53

将您的资源文件所在的文件夹添加到 Eclipse 的源文件夹中。然后该文件应该会自动放入 bin 目录中。

Add the folder that your having your resource files in to the source folders of eclipse. Then the file should be automatically put in the bin directory.

王权女流氓 2024-10-28 01:54:53

您需要将资源文件的副本放入测试资源,在我的示例中它是字体文件:

输入图片此处描述

然后从 jUnit 测试中调用 next:

 public static InputStream getFontAsStream() throws IOException {
        return Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("fonts/" + "FreeSans.ttf");
    }

You need to put the copy of your resource file to the test resources, in my example it is font file:

enter image description here

Then call next from your jUnit test:

 public static InputStream getFontAsStream() throws IOException {
        return Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("fonts/" + "FreeSans.ttf");
    }
哭泣的笑容 2024-10-28 01:54:53

我在 Spring 课程中遇到了这个问题。 System.class.getResourceAsStream(...) 在单元测试中有效,但在 Tomcat 中无效,Thread.currentThread().getContextClassLoader().getResourceAsStream(...) 有效在 Tomcat 中但不在单元测试中。

最终我选择了:

ClassUtils.getDefaultClassLoader()
    .getResourceAsStream("com/example/mail/templates/invoice-past-due.html"))

我还发现,一旦我这样做了,我就不需要以斜线开头的路径了。

I had this problem with a Spring class. System.class.getResourceAsStream(...) worked in unit tests but not in Tomcat, Thread.currentThread().getContextClassLoader().getResourceAsStream(...) worked in Tomcat but not in unit tests.

Ulitimately I went with:

ClassUtils.getDefaultClassLoader()
    .getResourceAsStream("com/example/mail/templates/invoice-past-due.html"))

I also found that once I did it this way, I did not need to have the path starting with a slash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文