java System.getProperty("user.dir") 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12413952/
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
System.getProperty("user.dir") does not work
提问by Danilo M.
I need to get relative path from my project to upload photos, however when I run System.getProperty("user.dir");
in my test, it returns /home/user/workspace/Myproject
,
but if I run it from my spring controller, it just returns /home/user
.
我需要从我的项目中获取相对路径来上传照片,但是当我System.getProperty("user.dir");
在我的测试中运行时,它返回/home/user/workspace/Myproject
,但如果我从我的弹簧控制器运行它,它只会返回/home/user
.
Can anyone provide a insight that probably is happening?
任何人都可以提供可能正在发生的见解吗?
回答by Jon Lin
The user.dir
property is the current working directory where you are running Java. You're starting java in /home/user/workspace/Myproject
when you run your project but when Spring runs it, it's not guaranteed to be the same working directory. Are you looking for user.home
(which should always be /home/user
) instead?
该user.dir
属性是您运行 Java 的当前工作目录。/home/user/workspace/Myproject
当你运行你的项目时,你正在启动 java,但是当 Spring 运行它时,它不能保证是相同的工作目录。你在寻找user.home
(应该总是/home/user
)吗?