0

I have an eclipse scala project which I am assembling with sbt. I want to add a properties file which I have tried placing in both the src and the target/scala/classes folders. I am not sure how to load this file in my program. This is my code:

val reader=this.getClass().getClassLoader().getResourceAsStream("ccm.properties") //Reading the properties file
val p=new Properties();  
p.load(reader);  
val maxDimension = p.getProperty("maxDimension").toInt 

This is the exception I am getting:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.musigma.ind.invictus.ConvergentCrossMapping.main(ConvergentCrossMapping.scala)
Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at com.musigma.ind.invictus.ConvergentCrossMapping$.<init>(ConvergentCrossMapping.scala:35)
    at com.musigma.ind.invictus.ConvergentCrossMapping$.<clinit>(ConvergentCrossMapping.scala)
    ... 1 more

UPDATE: This question is not a duplicate of this as I am writing this code in a Scala object and I cannot use this.class.getResourceAsStream(...)

Community
  • 1
  • 1
Piyush Shrivastava
  • 1,046
  • 2
  • 16
  • 43

1 Answers1

1

Instead of java properties class, you can do it with ConfigFactory. File must be located under resources directory.

var b = getClass.getResource("/test.properties").getPath
val externalConfig1 = ConfigFactory.parseFile(new File(b))

println(externalConfig1.getInt("maxDimension"))
Uttam Kasundara
  • 237
  • 3
  • 13