开发者

InputStream is = App.class.getResourceAsStream("test.properties");

the returned InputStream is null. What will be the poss开发者_Go百科ible cause?


App.class.getResourceAsStream(resource) searches for the resource in the same hierarchy as the class (so if your App class is in package com.yourcompany, this would search for the resource com/yourcompany/test.properties). In your situation you either have to prepend a slash or use the classloader instead of the class:

a)

App.class.getResourceAsStream("/test.properties")

b)

App.class.getClassLoader().getResourceAsStream("test.properties")


The "actual dir" is in the folder/package your class is in. Correct usage:

InputStream is = App.class.getResourceAsStream(“../../test.properties”);

Or maybe:

InputStream is = App.class.getResourceAsStream(“/test.properties”);


The file test.properties was not found.


The cause is usually that the file could not be found. (Rarely, it is that your app does not have read permission.) Typical problems here are that you need a leading path indicator (perhaps just /). I believe that the default search is based on the class loader for the App class and is probably not what you want.


how to make sure ./test.properties is on the classpath?

I assume that you actually know what the classpath is, and how it is set. And what a classloader it. If so, read on ...

The class path defines a namespace of resources with hierarchical names like "/a/b/c/d", or "/some/pkg/SomeClass.class". I'll call this the classloader namespace. When you call the getResourceAsStream(path) method, you are telling the classloader to locate the resource whose path is given by path in the classloader namespace ... and return an bytestream for reading the resource.

There are two kinds of path that you can give: absolute paths start with a "/", and relative paths start with some other character than "/".

  • If you use an absolute path (e.g. "/a/b/test.properties", then you will get the resource whose absolute path in the classloader namespace is the same as the path that you gave.

  • If you use a relative path (e.g. "test.properties"), then the relative path is resolved relative to the context in which you are loading it. In this case you are using

    import some.pkg.App;  // for instance
    
    App.class.getResourceAsStream("test.properties");
    

    The context for resolving the pathname will be the pathname for the App classes package; i.e. "/some/pkg". Hence, the classloader will look for a resource whose absolute name in the namespace is "/some/pkg/test.properties". (Note that the mapping from a package name to a pathname in the classloader namespace involves replacing the "." separators in the package FQN with "/" separators.)

    On the other handler if you use a classloader handle directly to load a resource; e.g.

    App.class.getClassLoader().getResourceAsStream("test.properties");
    

    the classloader uses "/" as its context, so the above will resolve to "/test.properties" ... in the namespace.

This should tell you how to work out where to put the "test.properties" file so that it can be located by the call you are using. Alternatively, you could use an absolute name.


To avoid such thing should make practice to put all your properties file under root context. So that you can access them from any child folder by

App.class.getResourceAsStream("/test.properties")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜