Liberty Server - ClassLoader.getSystemResource返回null

时间:2017-11-15 01:14:43

标签: java classloader websphere-liberty

开发了一个使用第三方罐子的休息服务。查看他们的代码我发现他们正在使用ClassLoader.getSystemResource(" log4j.properties")来检索数据。此代码返回null,从此我总是得到空指针异常。如果我在websphere 8.5.5.7中部署并设置服务器类路径,它工作正常。 由于我在本地机器上使用Liberty,因此不会检索路径。

我也尝试在server.xml中设置classloader但没有用。

<library id="config">
  <folder dir="/properties/dev/" includes="*.properties" scanInterval="5s"/>
</library>

<enterpriseApplication id="AbcEAR"location="AbcEAR.ear" name="AbcEAR">
  <classloader privateLibraryRef="config"/>
</enterpriseApplication>

1 个答案:

答案 0 :(得分:0)

使用ClassLoader.getSystemResource()将使用系统类加载器,它只能查看JDK中的内容以及类路径中的内容。由于Liberty不使用传统的类路径而是使用OSGi,因此从应用程序的角度来看,系统加载器在Liberty中几乎没用。

相反,请使用线程上下文类加载器中的ClassLoader.getResource(),它将考虑Liberty类加载器。