开发者社区 问答 正文

开发时候正常,打成Jar包读取properties就出错:报错

下面的代码在开发时运行正常,打成jar包就出错,。。。

private Properties loadProperty() { if (this.prop == null) { try { System.out.println("loading property:" + path); InputStream in = new BufferedInputStream(new FileInputStream(new File(path))); System.out.println(in); prop = new Properties(); System.out.println("prop:" + prop); prop.load(in); System.out.println("数据源配置文件:" + path + "读取成功"); } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } return this.prop; }

展开
收起
kun坤 2020-06-07 15:56:50 561 分享 版权
1 条回答
写回答
取消 提交回答
  • Properties 是在jar包里,还是jar包外,如果是jar包里面,new File()应该是取不到的吧.######是在jar包外,传递给File的路径是绝对路径。。很奇怪,不过我检查发现不是jar包的问题,谢谢指点######根据某个类的ClassLoader去获取配置文件的流######要用流读取的######上面的上面说得很对,jar包里面你得用你类.getClass().getClassLoader().get。。。。

    2020-06-07 15:56:54
    赞同 展开评论
问答分类:
问答地址: