javaeclipsemavenjunitcontextclassloader

getResourceAsStream returns null in one case but not another, with same classpaths


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>

Solution

  • 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)).