Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

java - getSystemResourceAsStream() returns null

Hiii... I want to get the content of properties file into InputStream class object using getSystemResourceAsStream(). I have built the sample code. It works well using main() method,but when i deploy the project and run on the server, properties file path cannot obtained ... so inputstream object store null value.

Sample code is here..

public class ReadPropertyFromFile {

    public static Logger logger = Logger.getLogger(ReadPropertyFromFile.class);

    public static String readProperty(String fileName, String propertyName) {
        String value = null;
        try {
            //fileName = "api.properties";
            //propertyName = "api_loginid";

            System.out.println("11111111...In the read proprty file.....");


            //  ClassLoader loader = ClassLoader.getSystemClassLoader();

            InputStream inStream = ClassLoader.getSystemResourceAsStream(fileName);

            System.out.println("In the read proprty file.....");
            System.out.println("File Name :" + fileName);
            System.out.println("instream = "+inStream);

            Properties prop = new Properties();

            try {
                prop.load(inStream);
                value = prop.getProperty(propertyName);
            } catch (Exception e) {
                logger.warn("Error occured while reading property " + propertyName + " = ", e);
                return null;
            }
        } catch (Exception e) {
            System.out.println("Exception = " + e);
        }
        return value;
    }

    public static void main(String args[]) {

      System.out.println("prop value = " + ReadPropertyFromFile.readProperty("api.properties", "api_loginid"));
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

i deploy the project and run on the server,

This sounds like a JSP/Servlet webapplication. In that case, you need to use the ClassLoader which is obtained as follows:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

This one has access to the all classpath paths tied to the webapplication in question and you're not anymore dependent on which parent classloader (a webapp has more than one!) has loaded your class.

Then, on this classloader, you need to just call getResourceAsStream() to get a classpath resource as stream, not the getSystemResourceAsStream() which is dependent on how the webapplication is started. You don't want to be dependent on that as well since you have no control over it at external hosting:

InputStream input = classLoader.getResourceAsStream("filename.extension");

This is finally more robust than your initial getSystemResourceAsStream() approach and the Class#getResourceAsStream() as suggested by others.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...