How to get the current (working) directory in Scala?
Asked Answered
A

6

17

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

Audrey answered 16/4, 2018 at 14:32 Comment(2)
All of the answers of a similar question re Java will work for Scala as well: #4871551Digress
os-lib is the best modern solution, see my answer for more details.Government
G
24

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

Grits answered 6/11, 2018 at 11:43 Comment(3)
If the response is correct, please accept the answer, so other can benefit from it. (A moderator)Chronology
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: docs.oracle.com/javase/tutorial/essential/environment/…Hothead
updated the answer with the link provided by @jake. Thanks.Grits
Z
17

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.

Zealot answered 16/4, 2018 at 15:2 Comment(0)
P
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

Pfaff answered 16/4, 2018 at 17:54 Comment(0)
A
4

Use this:

import scala.reflect.io.File
File(".").toAbsolute
Audrey answered 16/4, 2018 at 14:38 Comment(3)
Hi Neil, How can I get rid of the "./" part in the absolute path?Roam
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.Audrey
@Roam use File(".").toCanonical instead.Shelby
S
4
import scala.sys.process._
val cwd = "pwd".!!  //*nix OS
Shrug answered 16/4, 2018 at 16:2 Comment(1)
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...Belshazzar
G
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.

Government answered 16/12, 2020 at 2:27 Comment(3)
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?Townscape
@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.Government
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 librariesTownscape

© 2022 - 2024 — McMap. All rights reserved.