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

后端 未结 6 1732
悲哀的现实
悲哀的现实 2020-12-02 11:54

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. Wh

6条回答
  •  旧时难觅i
    2020-12-02 12:47

    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
    

提交回复
热议问题