How to Find Current Base Execution Directory in Groovy (Or Java)? [Duplicate]

I've got a little script I'm using a paramter to pass in the current execution directory, but would like to make it a little more robust.

How does one find out the base execution directory?

2

Try this:

System.getProperty("user.dir");
3

Depending on the security model, if the System.getProperty(String) is not allowed, you can use

String currentDir = new File(".").getAbsolutePath()
2

For reference:

The accepted answer on the question here is what I was looking for.

As an example, when calling c:\scripts\MyScript.groovy from c:\users\Scott\ I wanted to know c:\scripts\.

This is done via this:

def scriptDir = getClass().protectionDomain.codeSource.location.path

Where scriptDir is assigned something like:

/c:/scripts/MyScript.groovy

1
Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest