当我尝试使用以下代码读取JAR内的目录时:
getClass().getResourceAsStream('/some-dir').text
我使用以下stacktrace获得NPE:
Exception in thread "main" java.lang.NullPointerException
at java.io.FilterInputStream.close(FilterInputStream.java:181)
at sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.close(JarURLConnection.java:108)
at sun.nio.cs.StreamDecoder.implClose(StreamDecoder.java:378)
at sun.nio.cs.StreamDecoder.close(StreamDecoder.java:193)
at java.io.InputStreamReader.close(InputStreamReader.java:199)
at java.io.BufferedReader.close(BufferedReader.java:525)
at org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport.closeWithWarning(DefaultGroovyMethodsSupport.java:90)
at org.codehaus.groovy.runtime.IOGroovyMethods.getText(IOGroovyMethods.java:892)
at org.codehaus.groovy.runtime.IOGroovyMethods.getText(IOGroovyMethods.java:835)
at org.codehaus.groovy.runtime.dgm$833.doMethodInvoke(Unknown Source)
at org.codehaus.groovy.reflection.GeneratedMetaMethod$Proxy.doMethodInvoke(GeneratedMetaMethod.java:73)
at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty.java:76)
at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:64)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:296)
at com.blabla.Main.test(Main.groovy:68)
at com.blabla.Main.main(Main.groovy:78)
奇怪的是,只有在执行JAR时才抛出异常。从IDE(IntelliJ IDEA)执行它时,它可以正常工作。