getResourceAsStream在一种情况下返回null,而不是另一种情况,具有相同的类路径(getResourceAsStream returns null in one case but n

编程入门 行业动态 更新时间:2024-10-21 13:35:28
getResourceAsStream在一种情况下返回null,而不是另一种情况,具有相同的类路径(getResourceAsStream returns null in one case but not another, with same classpaths)

我有两个相同应用的衍生物,比如版本(A)和(B)。 它们各自包含项目:(1)test-data-war,其具有target/test-classes/log4j.properties ,以及(2)test-kernel,其具有target/test-classes/log4j.properties和target/test-classes/test.properties 。

当我在(1)中运行特定的jUnit测试时,它调用(2)中的方法调用Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); 在(A)中, resourceName为“log4j.properties”,结果不为null,路径为(1),但resourceName为“test.properties”,则为null。 在(B)中, resourceName为“log4j.properties”,它不是null,路径在(1)中,而resourceName是“test.properties”,它不是null,路径在(2)中。

为什么是Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"); 在(A)中为null? 起初,我认为类路径可能不同,但它们对于(1)和(2)都是相同的。

编辑:这是.classpath文件的样子(1):

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" pat h="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath>

这是(2)的.classpath文件的样子:

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <attribute name="org.eclipse.jst.component.nondependency" value=""/> </attributes> </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath>

I have two derivatives of the same application, say versions (A) and (B). They each contain projects: (1) test-data-war, which has target/test-classes/log4j.properties, and (2) test-kernel, which has target/test-classes/log4j.properties and target/test-classes/test.properties.

When I run a specific jUnit test in (1), it calls a method in (2) which calls Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName); In (A), with resourceName as "log4j.properties", the result is not null with a path in (1), but with resourceName as "test.properties" it is null. In (B), with resourceName as "log4j.properties" it is not null with a path in (1) and with resourceName as "test.properties" it is not null with a path in (2).

Why is Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties"); null in (A)? At first, I thought the classpaths might be different, but they are the same for both (1) and (2).

EDIT: Here is what the .classpath file looks like for (1):

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" pat h="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> </attributes> </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath>

Here is what the .classpath file looks like for (2):

<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" output="target/classes" path="src/main/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="src" output="target/test-classes" path="src/test/java"> <attributes> <attribute name="optional" value="true"/> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <attributes> <attribute name="maven.pomderived" value="true"/> <attribute name="org.eclipse.jst.component.nondependency" value=""/> </attributes> </classpathentry> <classpathentry kind="output" path="target/classes"/> </classpath>

最满意答案

模块test-kernel中的测试类路径的资源对于模块test-data-war是不可见的。 当您将它们作为依赖项添加到另一个模块时,模块仅导出src/main/resources resources下的src/main/resources 。

令人困惑的是Eclipse和Maven在这里不同意。 在Eclipse中,模块的整个类路径是可见的(也是测试资源)。 但是当你在Maven中运行相同的测试时,测试资源会突然消失。 这是因为Eclipse没有“test classpath”的概念。

如果mvn dependency:tree显示差异,那么你需要检查模块的文件pom.xml 。

In (B), there was had an additional dependency in eclipse on (2) that was causing Thread.currentThread().getContextClassLoader().getResourceAsStream("test.properties") to NOT return null. It was actually supposed to return null (i.e. not find test.properties in (2)).

更多推荐

本文发布于:2023-08-04 10:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   而不是   情况下   情况   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!