Need help with strange Class#getResource() issue

前端 未结 4 1792
太阳男子
太阳男子 2021-01-20 01:26

I have some legacy code that reads a configuration file from an existing jar, like:

URL url = SomeClass.class.getResource(\"/configuration.properties\");
//          


        
4条回答
  •  梦毁少年i
    2021-01-20 02:13

    A typical implementation of ClassLoader.getResourceAsStream is:

    public InputStream getResourceAsStream(String name) {
        URL url = getResource(name);
        try {
            return url != null ? url.openStream() : null;
        } catch (IOException e) {
            return null;
        }
    }
    

    Class.getResource and getResourceAsStream behave the same as each other.

    So it looks like either you are using a strange and broken subclass of ClassLoader, or there is some mistake in the tests on your side.

提交回复
热议问题