Slavik  Muz
Slavik Muz

Reputation: 1217

Can not read file from resource

I have a project on scala and sbt. I try to get file from resource.

  val filename = getClass.getResource("/emptyClickReports.csv").getFile
  log.debug("get empty report {} from resource folder {} ", filePath, filename)
  val file = new File(filename)
  log.debug("file exists: {}", file.exists())
  log.debug("file getAbsolutePath: {}", file.getAbsolutePath())
  log.debug("file getCanonicalPath: {}", file.getCanonicalPath())
  log.debug("file getPath: {}", file.getPath())

  file

When I start project with sbt run - it is ok, file exists

[DEBUG] - 2017-12-07 14:25:09,469 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - get empty report /emptyClickReports.csv from resource folder /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv 
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file exists: true
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getAbsolutePath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getCanonicalPath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getPath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[INFO ] - 2017-12-07 14:25:09,471 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.scenarios.controllers.ScenarioController - file downloaded /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv, file length: 97

but when I do sbt stage, and run with bin file I have next:

[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file exists: false
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getAbsolutePath: /home/slava/projects/ds-selenium/file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getCanonicalPath: /home/slava/projects/ds-selenium/file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getPath: file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[

file not exits and path is:

file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv

How can I get file from resource when i run project with bin file?

Upvotes: 1

Views: 849

Answers (1)

Mike Slinn
Mike Slinn

Reputation: 8403

You need a different incantation when reading a resource from a file than when reading a resource from a jar. As you know, the stage task builds a jar. Many people have asked similar questions on StackOverflow.

I am repeating that answer by @iny here, but with a link to the Java 8 runtime library because that is currently supported by Scala:

To summarinze, you should use java.lang.Class.getResourceAsStream(String), see https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-

This is another good answer for this question.

Upvotes: 2

Related Questions