1

将 sun 应用程序服务器 8.2 升级到新的补丁级别时发生异常,我不知道为什么。遵循来自Servlet的代码片段:

public void init() throws ServletException {
    Properties reqProperties = new Properties();
    try {
        reqProperties.load(this.getClass().getResourceAsStream(
                "/someFile.properties"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    ...
}

该文件确实存在于类路径中,并且在以前的补丁版本中它工作得很好。但是现在在部署此结果时会出现异常。堆栈跟踪:

[#|2010-04-14T16:43:48.208+0200|WARNING|sun-appserver-ee8.2|javax.enterprise.system.core.classloading|_ThreadID=11;|loader.InputStreams with no valid reference is closed
java.lang.Throwable
    at com.sun.enterprise.loader.EJBClassLoader$SentinelInputStream.<init>(EJBClassLoader.java:1172)
    at com.sun.enterprise.loader.EJBClassLoader.getResourceAsStream(EJBClassLoader.java:858)
    at java.lang.Class.getResourceAsStream(Class.java:1998)
    at a.package.TestServlet.init(TestServlet.java:44)
    at javax.servlet.GenericServlet.init(GenericServlet.java:261)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:592)
    at org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:249)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
    at org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:282)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:165)
    at org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:118)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1093)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:931)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4183)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4535)
    at com.sun.enterprise.web.WebModule.start(WebModule.java:241)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1086)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:847)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1086)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:483)
    at org.apache.catalina.startup.Embedded.start(Embedded.java:894)
    at com.sun.enterprise.web.WebContainer.start(WebContainer.java:741)
    at com.sun.enterprise.web.HttpServiceWebContainer.startInstance(HttpServiceWebContainer.java:963)
    at com.sun.enterprise.web.HttpServiceWebContainerLifecycle.onStartup(HttpServiceWebContainerLifecycle.java:50)
    at com.sun.enterprise.server.ApplicationServer.onStartup(ApplicationServer.java:300)
    at com.sun.enterprise.server.PEMain.run(PEMain.java:308)
    at com.sun.enterprise.server.PEMain.main(PEMain.java:221)
|#]

我不知道任何人有什么想法可能是什么问题?(请注意,我在代码和堆栈跟踪中更改了一些名称)

4

2 回答 2

1

你确定它会抛出异常吗?我们一直在 Glassfish 中收到这样的警告。EJBClassLoader 使用 throwable 来转储堆栈跟踪,因此它可能看起来像是一个异常。

EJBClassLoader 用标记包装所有流。这个警告只是告诉你你的流没有关闭。您可以放心地忽略它。要消除警告,您必须在使用后关闭流。

于 2010-06-11T13:33:05.200 回答
1

使用后应始终关闭输入流:

public void init() throws ServletException {
    InputStream str = null;
    Properties reqProperties = new Properties();
    try {
        str = this.getClass().getResourceAsStream("/someFile.properties");
        reqProperties.load(str);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (str != null) {
            try {
                str.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

顺便说一句,finally 子句可以使用 apache commons / io 变得更简单:

finally {
    IOUtils.closeQuietly(str);
}
于 2010-06-11T14:04:10.060 回答