程序找不到properties文件

程序通过读取配置spark.properties文件获取相关属性信息,文件所在目录resources下

 

 执行程序报错如下:

java.io.FileNotFoundException: spark.properties (系统找不到指定的文件。)

at java.io.FileInputStream.open0(Native Method)

at java.io.FileInputStream.open(FileInputStream.java:195)

at java.io.FileInputStream.<init>(FileInputStream.java:138)

at java.io.FileInputStream.<init>(FileInputStream.java:93)

at com.cnpc.common.LoadProperties.load(LoadProperties.scala:13)

at com.cnpc.test$.main(test.scala:11)

at com.cnpc.test.main(test.scala)

 

 程序代码

classLoadProperties {
  def load(propertiesFile:String)={
    val config =
      try {
        val prop = new Properties()
         prop.load(new FileInputStream(propertiesFile))
        prop
      } catch {
        case e: Exception =>
          e.printStackTrace()
          sys.exit(1)
      }
    config
  }
}

 

解决办法

替换上如程序中文件读取部分

 

//        解决办法1
val path =

Thread.currentThread().getContextClassLoader.getResource(propertiesFile).getPath
       prop.load(new FileInputStream(path))

//        解决方法2
prop.load(new FileInputStream(getClass.getClassLoader.getResource(propertiesFile).getFile()))

 

转载于:https://www.cnblogs.com/OS-BigData/p/8526914.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值