简体   繁体   English

属性文件InputStream在Websphere中可以,但在Tomcat中为null

[英]Property file InputStream ok in Websphere but null in Tomcat

Though I am using Websphere majorly but I want to use properties file in a way that works in Tomcat as well. 虽然我主要使用Websphere,但我想以一种在Tomcat中也可以使用的方式使用属性文件。 I am initializing properties object in a static block for I want to use the properties object in static methods. 我要在静态块中初始化properties对象,因为我想在静态方法中使用properties对象。

public class Utils {
    public static Properties properties = null;

    static {
        try{
            properties = new Properties();
            String propertiesFile = "../../../../WebContent/path.properties";
            InputStream in = Utils.class.getResourceAsStream(propertiesFile);
            properties.load(in);
        }
        catch(IOException e){
             e.printStackTrace(System.err);
        }
    }
}

Above code gives following exception in Tomcat, but working like charm in Websphere. 上面的代码在Tomcat中给出了以下异常,但在Websphere中却像魅力一样工作。

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at carey.services.utils.Utils.<clinit>(Utils.java:50)

Tips or alterations to suit the need is appreciated. 可以根据需要提供提示或更改。 Thanks. 谢谢。

I suspect that path.properties isn't actually in the "classpath" (compiled into a JAR or located with class files), which is where resources are supposed to live. 我怀疑path.properties实际上不在“类路径”(编译为JAR或与类文件一起位于)中,而该类应该是资源所在的地方。 It's probably working in Websphere due to side effect of how its class loading works. 由于类加载方式的副作用,它可能在Websphere中工作。 This doesn't mean it's correct, it's a side effect. 这并不意味着它是正确的,而是副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM