file path in WebApplication using GlassFish

后端 未结 1 546
执笔经年
执笔经年 2021-01-21 15:26

I\'m using a class to get a property file under the source folder. But it doesn\'t work! After checking, I found that the default path by using

File f = new File         


        
1条回答
  •  执念已碎
    2021-01-21 15:52

    You should use getResourceAsStream, or similar. See this post for how to access resources. (This is independent of Glassfish - it applies to all Java EE app servers.)

    See also this JavaWorld article.

    Update: If your file is in the location src/ss.properties, check that it has been copied to WEB-INF/classes. Then, you should be able to access it with the following code:

    InputStream propStream = ClassLoader.getResourceAsStream("ss.properties");
    

    or (note leading slash if using the method in java.lang.Class)

    InputStream propStream = Class.getResourceAsStream("/ss.properties");
    

    Note that the full file name (including the .properties extension) needs to be used.

    If neither of these work, please replace the getResourceAsStream call with getResource(...).openStream() and post details of the exception which should be thrown.

    0 讨论(0)
提交回复
热议问题