2

I am getting NullpointerException when I try to read the file present in src/main/resources location. It occurs when I run in jar format. But when I compile the code and run it , its running fine in Intellij.

Note: I am using sbt package to build the jar and running it.

Please help out. thanks!

MJeremy
  • 1,102
  • 17
  • 27
A srinivas
  • 791
  • 2
  • 9
  • 25
  • Post your code. Otherwise, we're just guessing as to what the program might be doing. – The Archetypal Paul Oct 22 '18 at 13:16
  • configFileName = CustomParams.dbName + "_" + CustomParams.tableName + "_config.conf" println("configFileName "+configFileName) //check in resource and get the path of it //println("ssdfs " +Thread.currentThread.getContextClassLoader.getResource(s"/$configFileName").getFile) val filePath = DqDriver.getClass.getClassLoader.getResource(s"$configFileName").getFile – A srinivas Oct 22 '18 at 13:22
  • Edit your question to include the code, please. – The Archetypal Paul Oct 22 '18 at 13:25
  • thanks for your time. I got the solution from another suggestion! – A srinivas Oct 22 '18 at 13:47

1 Answers1

2

Files that have been packed into the JAR are not accessible from the file system anymore. This can be seen when looking at the URL returned from myClass.getResource("file.txt"), e.g.:

/home/sria/cde-spark-assembly-1.0.jar!/file.txt

Note the ! denoting that the file is packaged into the JAR.


This means you always have to access resource files using the following pattern:

For a file in src/main/resources/file.txt:

myClass.getResourceAsStream("file.txt")

There is two reasons why you might not want to do that:

  1. Adding files to the resources directory will increase the JAR file size.
  2. The file cannot be accessed using standard file system operations.

As an alternative you can load the file from a (configurable) path in the file system, using for example:

val inputStream = new BufferedInputStream(new FileInputStream(myPath))

(reference)

This way you can load a file, e.g. relative to the JAR file path or the execution directory.


I hope this helps.

Note: Both sbt package and sbt assembly will package resource files into the JAR.

Markus Appel
  • 3,138
  • 1
  • 17
  • 46
  • I tried running with assembly too. I am getting the path like this /home/sria/cde-spark-assembly-1.0.jar!/config.conf . there is an exclamatory coming from no where. – A srinivas Oct 22 '18 at 13:12
  • @A srinivas that's correct. This path denotes that the given file is packed into the JAR. It cannot be treated as a normal file, and should be retrieved using `getResourceAsStream`. – Markus Appel Oct 22 '18 at 13:25
  • thanks.. its working fine now. but do you know any alternative for fat jar, as its not effective to transfer the fat jar from local to remote everytime? – A srinivas Oct 22 '18 at 13:46
  • @Asrinivas I adapted my answer accordingly. – Markus Appel Oct 22 '18 at 14:14