16

How can I get the current directory (working directory) of the executing program in Scala?

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
Neil
  • 24,551
  • 15
  • 60
  • 81
  • 3
    All of the answers of a similar question re Java will work for Scala as well: https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – Tzach Zohar Apr 16 '18 at 14:34
  • [os-lib](https://github.com/lihaoyi/os-lib) is the best modern solution, see my answer for more details. – Powers Dec 16 '20 at 02:28

6 Answers6

22

Use this System.getProperty("user.dir")

Edit: A list of other standard properties can be found here. https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

  • If the response is correct, please accept the answer, so other can benefit from it. (A moderator) – AlainD Nov 06 '18 at 15:24
  • 1
    This seems the "most" correct answer of the answers provided, as it's a standard java property and therefore doesn't require the developer know what "." means (although all developers probably do). The java docs provide a definition for the property here: https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html – Jake Mar 09 '20 at 02:45
  • updated the answer with the link provided by @jake. Thanks. – Pubudu Sitinamaluwa Aug 31 '20 at 15:00
16

Use java.nio:

import java.nio.file.Paths
println(Paths.get(".").toAbsolutePath)

Remark on scala.reflect.io.File: I don't see any reason to look into scala.reflect packages for such mundane tasks. Getting the current working directory has usually nothing to do with Scala's reflection capabilities. Here are more reasons not to use scala.reflect.io.File: link.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
5

Use this:

import scala.reflect.io.File
File(".").toAbsolute
Neil
  • 24,551
  • 15
  • 60
  • 81
  • Hi Neil, How can I get rid of the "./" part in the absolute path? – ruxtain Jul 09 '22 at 08:46
  • Can you give an example? Intuitively I would just use string pattern search replace to fix that. Maybe there’s a function for it, though. I’m not sure. – Neil Jul 09 '22 at 09:07
  • @ruxtain use `File(".").toCanonical` instead. – sbrk Oct 12 '22 at 18:41
5

I use new java.io.File(".").getAbsolutePath, but all the other answers work too. You don't really gain anything by using Scala-specific API here over regular Java APIs

Alex Savitsky
  • 2,306
  • 5
  • 24
  • 30
4
import scala.sys.process._
val cwd = "pwd".!!  //*nix OS
jwvh
  • 50,871
  • 7
  • 38
  • 64
  • When I tried this in Spark Scala in an EMR Notebook, I got `cwd: String = "/ "` which means it's not helping me see the absolute path... – Sam Mefford May 18 '21 at 22:41
1

os-lib makes it easy to get the current working directory and perform other filesystem operations. Here's how to get the current directory:

os.pwd

It's easy to build other path objects from os.pwd, for example:

os.pwd/"src"/"test"/"resources"/"people.csv"

The syntax is intuitive and easy to remember, unlike the other options. See here for more details on how to use the os-lib project.

Powers
  • 18,150
  • 10
  • 103
  • 108
  • have not managed to get the import part working. Added the lib to build.sbt and os is visible in external libraries but importing that to code is not working. How do you do it? – uxke Sep 19 '21 at 18:00
  • 2
    @juske - you shouldn't need to import anything. The code is in the `os` package, so `os.pwd` is the full path. You might just need to reload IntelliJ as you would whenever a dependency is added to the `build.sbt` file. – Powers Sep 19 '21 at 18:35
  • thanks for quick reply! still getting an `error: not found: value os` although there is `sbt: com.lihaoyi:os-lib_2.13:0.78.jar` under external libraries – uxke Sep 20 '21 at 06:17