public class ServerProperties {
public  String url;
public static Properties props = new Properties();
public ServerProperties(){
try{
props.load(this.getClass().getResourceAsStream("Db.properties"));
url=props.getProperty("driverurl"); 

    
}catch(Exception e){
System.out.println(e);
Log.writelog("300",e.toString());
}
}
这是我写的类,如果建main函数的类,可以加载。但如果用tomcat不用jdk起动时props.load(this.getClass().getResourceAsStream("Db.properties"));
就无法加载了,在这步时出现空指针异常!请问谁遇见过这个问题!

解决方案 »

  1.   

    是你的Db.properties文件放的位置不对吧
      

  2.   

    放在这个ServerProperties类的一个包下了,如果不对也不会jdk方式能运行,tomcat不成吧
      

  3.   

    properties.load(Config.class.getClassLoader().getResourceAsStream(FILENAME));
    将文件放在classes目录下就行了
      

  4.   

    有这个文件class下,我从重覆盖了一下还是原来的问题
      

  5.   

    props.load(ServerProperties.class.getClassLoader().getResourceAsStream("Db.properties"));
    写成这样也是到了这句就报异常
      

  6.   

    为什么不这样写?
    props.load(new FileInputStream("Db.properties"));