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!

4
  • Post your code. Otherwise, we're just guessing as to what the program might be doing. Commented Oct 22, 2018 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 Commented Oct 22, 2018 at 13:22
  • Edit your question to include the code, please. Commented Oct 22, 2018 at 13:25
  • thanks for your time. I got the solution from another suggestion! Commented Oct 22, 2018 at 13:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 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.
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?
@Asrinivas I adapted my answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.