Spark job in Java: how to access files from 'resources' when run on a cluster

It appears that running Scala (2.11) code on Spark does not support accessing resources in shaded jars.

Executing this code:

var path = getClass.getResource(fileName)
println("#### Resource: " + path.getPath())

prints the expected string when run outside of Spark.

When run inside Spark, a java.lang.NullPointerException is raised because path is null.


I have accessed my resource file like below in spark-scala. I've share my code please check.

val fs=this.getClass().getClassLoader().getResourceAsStream("smoke_test/loadhadoop.txt")

val dataString=scala.io.Source.fromInputStream(fs).mkString

Your existing code is referencing them as files which are not packaged up and shipped to the Spark nodes. But, since they're inside your jar file you should be able to reference them via Foo.getClass().getResourceAsStream("/templates/my_template_ftl"). More info on Java resource streams here: http://www.javaworld.com/article/2077352/java-se/smartly-load-your-properties.html