How do you get the path of the running script in groovy?

I'm writing a groovy script that I want to be controlled via a properties file stored in the same folder. However, I want to be able to call this script from anywhere. When I run the script it always looks for the properties file based on where it is run from, not where the script is.

How can I access the path of the script file from within the script?


You are correct that new File(".").getCanonicalPath() does not work. That returns the working directory.

To get the script directory

scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent

To get the script file path

scriptFile = getClass().protectionDomain.codeSource.location.path

As of Groovy 2.3.0 the @SourceURI annotation can be used to populate a variable with the URI of the script's location. This URI can then be used to get the path to the script:

import groovy.transform.SourceURI
import java.nio.file.Path
import java.nio.file.Paths

@SourceURI
URI sourceUri

Path scriptLocation = Paths.get(sourceUri)

Note that this will only work if the URI is a file: URI (or another URI scheme type with an installed FileSystemProvider), otherwise a FileSystemNotFoundException will be thrown by the Paths.get(URI) call. In particular, certain Groovy runtimes such as groovyshell and nextflow return a data: URI, which will not typically match an installed FileSystemProvider.


This makes sense if you are running the Groovy code as a script, otherwise the whole idea gets a little confusing, IMO. The workaround is here: https://issues.apache.org/jira/browse/GROOVY-1642

Basically this involves changing startGroovy.sh to pass in the location of the Groovy script as an environment variable.

As long as this information is not provided directly by Groovy, it's possible to modify the groovy.(sh|bat) starter script to make this property available as system property: For unix boxes just change $GROOVY_HOME/bin/groovy (the sh script) to do

export JAVA_OPTS="$JAVA_OPTS -Dscript.name=$0"

before calling startGroovy For Windows: In startGroovy.bat add the following 2 lines right after the line with the :init label (just before the parameter slurping starts):

@rem get name of script to launch with full path
set GROOVY_SCRIPT_NAME=%~f1

A bit further down in the batch file after the line that says "set JAVA_OPTS=%JAVA_OPTS% -Dgroovy.starter.conf="%STARTER_CONF%" add the line

set JAVA_OPTS=%JAVA_OPTS% -Dscript.name="%GROOVY_SCRIPT_NAME%"