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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:48:07  来源:igfitidea点击:

System.getProperty("user.dir") does not work

java

提问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.dirproperty is the current working directory where you are running Java. You're starting java in /home/user/workspace/Myprojectwhen 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)吗?