如何在groovy(或java)中找到当前的基本执行目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6492240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find current base execution directory in groovy (or java)?
提问by Scott Bennett-McLeish
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?
如何找出基本执行目录?
采纳答案by Scott Bennett-McLeish
For reference:
以供参考:
The accepted answer on the question hereis what I was looking for.
关于这里问题的公认答案是我正在寻找的。
As an example, when calling c:\scripts\MyScript.groovy
from c:\users\Scott\
I wanted to know c:\scripts\
.
举个例子,当调用c:\scripts\MyScript.groovy
从c:\users\Scott\
我想知道c:\scripts\
。
This is done via this:
这是通过以下方式完成的:
def scriptDir = getClass().protectionDomain.codeSource.location.path
def scriptDir = getClass().protectionDomain.codeSource.location.path
Where scriptDir is assigned something like:
其中 scriptDir 被分配如下:
/c:/scripts/MyScript.groovy
/c:/scripts/MyScript.groovy
回答by rodion
Try this:
尝试这个:
System.getProperty("user.dir");
回答by Aleks G
Depending on the security model, if the System.getProperty(String)
is not allowed, you can use
根据安全模型,如果System.getProperty(String)
不允许,您可以使用
String currentDir = new File(".").getAbsolutePath()