zjffdu
zjffdu

Reputation: 28924

Read property file under classpath using scala

I am trying to read a property file from classpath using scala. But it looks like it won't work, it is different from java. The following 2 code snippet, one is java (working), another is scala (not working). I don't understand what is the difference.

// working
BufferedReader reader = new BufferedReader(new InputStreamReader(
Test.class.getResourceAsStream("conf/fp.properties")));

// not working 
val reader = new BufferedReader(new InputStreamReader(
getClass.getResourceAsStream("conf/fp.properties")));

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at com.ebay.searchscience.searchmetrics.fp.conf.FPConf$.main(FPConf.scala:31)
at com.ebay.searchscience.searchmetrics.fp.conf.FPConf.main(FPConf.scala)

Upvotes: 7

Views: 28954

Answers (6)

My prefered solution is with com.typesafe.scala-logging. I did put an application.conf file in main\resources folder, with content like:

services { mongo-db { retrieve = """http://xxxxxxxxxxxx""", base = """http://xxxxxx""" } }

and the to use it in a class, first load the config factory from typesafe and then just use it.

val conf = com.typesafe.config.ConfigFactory.load() conf.getString("services.mongo-db.base"))

Hope it helps!

Ps. I bet that every file on resources with .conf as extension will be read.

Upvotes: 3

Roman Nikitchenko
Roman Nikitchenko

Reputation: 13046

This code finally worked for me:

import java.util.Properties
import scala.io.Source

// ... somewhere inside module.

var properties : Properties = null

val url = getClass.getResource("/my.properties")
if (url != null) {
    val source = Source.fromURL(url)

    properties = new Properties()
    properties.load(source.bufferedReader())
}

And now you have plain old java.util.Properties to handle what my legacy code actually needed to receive.

Upvotes: 10

harschware
harschware

Reputation: 13424

The Null Pointer Exception you are getting is from getResourceAsStream returning null. The following junit.scala snippet shows how there is a difference in class vs classloader. see What is the difference between Class.getResource() and ClassLoader.getResource()?. Here I assume fileName is the name of a file residing in the class path, but not a file next to the class running the test.

assertTrue(getClass.getClassLoader().getResourceAsStream(fileName) != null)
assertTrue(getClass.getClassLoader().getResourceAsStream("/" + fileName) == null)
assertTrue(getClass.getResourceAsStream(fileName) == null)
assertTrue(getClass.getResourceAsStream("/" + fileName) != null)

Upvotes: 0

iwein
iwein

Reputation: 26161

The NullPointerException you are seeing is caused by a bug in the underlying Java code. It could be caused by a mistyped file name.

Sometimes you get this error also if you're trying to load the resource with the wrong classloader.

  1. Check the resource url carefully against your classpath.
  2. Try Source.fromInputStream(getClass.getResourceAsStream(...))
  3. Try Source.fromInputStream(getClass.getClassLoader.getResourceAsStream())
  4. Maybe you are using other classloaders you can try?

The same story goes for Source.fromUrl(...)

If you're trying to load configuration files and you control their format, you should have a look at Typesafe's Config utility.

Upvotes: 1

Chirlo
Chirlo

Reputation: 6132

For reading a Properties file i'd recommend to use java.util.ResourceBundle.getBundle("conf/fp"), it makes life a little easier.

Upvotes: 1

suls
suls

Reputation: 208

I am guessing that your BufferedReader is a java.io.BufferedReader

In that case you could simply do the following:

import scala.io.Source.fromUrl
val reader = fromURL(getClass.getResource("conf/fp.properties")).bufferedReader()

However, this leaves the question open as to what you are planning to do with the reader afterwards. scala.io.Source already has some useful methods that might make lots of your code superfluous .. see ScalaDoc

Upvotes: 8

Related Questions