How to read a file from classpath without external dependencies?

If the file is in the resource folder (then it will be in the root of the class path), you should use the Loader class that it is too in the root of the class path.

This is the code line if you want to get the content (in scala 2.11):

val content: String  = scala.io.Source.fromInputStream(getClass.getClassLoader.getResourceAsStream("file.xml")).mkString

In other versions of Scala, Source class could be in other classpath

If you only want to get the Resource:

val resource  = getClass.getClassLoader.getResource("file.xml")

val text = io.Source.fromInputStream(getClass.getResourceAsStream("file.xml")).mkString

If you want to ensure that the file is closed:

val source = io.Source.fromInputStream(getClass.getResourceAsStream("file.xml"))
val text = try source.mkString finally source.close()

Just an update, with Scala 2.13 it is possible to do something like this:

import scala.io.Source
import scala.util.Using

Using.resource(getClass.getResourceAsStream("file.xml")) { stream =>
  Source.fromInputStream(stream).mkString
}

Hope it might help someone.