Java system.getProperty("user.dir") 在 ubuntu 中给出错误的结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1554488/
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
Java system.getProperty("user.dir") gives wrong result in ubuntu
提问by winsontan520
I develop a Java application which is able to run cross-platform with condition in JRE version at least 1.6.0.14. Everything is work fine on a Windows machine (JRE1.6.0.14) but unwanted result in Ubuntu 8.04 with JRE1.6.0.14.
我开发了一个 Java 应用程序,它能够在 JRE 版本至少为 1.6.0.14 的条件下跨平台运行。在 Windows 机器 (JRE1.6.0.14) 上一切正常,但在带有 JRE1.6.0.14 的 Ubuntu 8.04 中出现不需要的结果。
I found the errors here:
我在这里发现了错误:
Document doc = docBuilder.parse (new File("webservices.xml"));
On the Windows machine, everything works ok, the docBuilder will refer the file at which my application located at. Example: if my application located at C:\myApp\start.jar , it will refer webservives.xml at C:\myApp\webservices.xml (this mean it will always refer correct directory no matter where i move my application folder)
在 Windows 机器上,一切正常,docBuilder 将引用我的应用程序所在的文件。示例:如果我的应用程序位于 C:\myApp\start.jar ,它将引用位于 C:\myApp\webservices.xml 的 webservives.xml(这意味着无论我将应用程序文件夹移到哪里,它都将始终引用正确的目录)
But in Ubuntu 8.04 it doesnt work.
但是在 Ubuntu 8.04 中它不起作用。
I am able to figure out the problem by using this in application:
我可以通过在应用程序中使用它来找出问题:
String curDir = System.getProperty("user.dir");
System.out.println(curDir);
No matter where I place my application folder, the curDiralways return "/home/user". Document doc = docBuilder.parse (new File("webservices.xml"))doesn't work until I place the webservices.xml in directory /home/user/webservices.xml.
无论我将应用程序文件夹放在哪里,curDir总是返回“/home/user”。Document doc = docBuilder.parse (new File("webservices.xml"))直到我将 webservices.xml 放在目录 /home/user/webservices.xml 中才起作用。
Running my application using Netbean 6.5.1 in Ubuntu return correct curDir but running my application standalone return wrong curDir (i am using JDK1.6.0.14 and JRE1.6.0.14 same as window machine)
在 Ubuntu 中使用 Netbean 6.5.1 运行我的应用程序返回正确的 curDir 但独立运行我的应用程序返回错误的 curDir(我使用 JDK1.6.0.14 和 JRE1.6.0.14 与窗口机器相同)
Why Document doc = docBuilder.parse (new File("webservices.xml"))cant work properly in ubuntu JRE1.6.0.14?
为什么Document doc = docBuilder.parse (new File("webservices.xml"))在 ubuntu JRE1.6.0.14 中不能正常工作?
Any idea to make my application work standalone in Ubuntu 8.04 just like in window machine?
有什么想法可以让我的应用程序像在窗口机器中一样在 Ubuntu 8.04 中独立工作吗?
回答by Michael Borgwardt
Do not rely on the current directory to read files that come with the application. Instead, use ClassLoader.getResource()(to access file relative to the classpath) or Class.getResouce()to access files inside packages.
不要依赖当前目录来读取应用程序附带的文件。相反,使用ClassLoader.getResource()(访问相对于类路径的文件)或Class.getResouce()访问包内的文件。
Edit:The above is only good for readonly access. Files that will be modified should be stored in the user's home directory (System property user.home), not the application directory, because the latter causes many problems:
编辑:以上仅适用于只读访问。将被修改的文件应该存放在用户的主目录(系统属性user.home)中,而不是应用程序目录中,因为后者会导致很多问题:
- multiple users running the application at the same time might overwrite each other's changes
- Users cannot backup or migrate their data easily
- It is incompatible with a properly secure system of user privileges, where normal users do not have write access to application directories to prevent viruses from infecting applications.
- 多个用户同时运行应用程序可能会覆盖彼此的更改
- 用户无法轻松备份或迁移他们的数据
- 它与适当安全的用户权限系统不兼容,在这种系统中,普通用户没有对应用程序目录的写访问权限以防止病毒感染应用程序。
For small amounts of data, you can use the Java preferences API.
对于少量数据,您可以使用Java 首选项 API。
Edit2: For that particular requirement, this should work (needs the file to be in the classpath):
Edit2:对于该特定要求,这应该可以工作(需要文件位于类路径中):
Document doc = docBuilder.parse (
new File(getClass().getClassLoader().getResource("webservices.xml").toURI()););
回答by notnoop
System.getProperty("user.dir")returns the current directory the user is executing the program, rather than where the program is located.
System.getProperty("user.dir")返回用户正在执行程序的当前目录,而不是程序所在的位置。
回答by user151019
user.dir is the current working directory of your shell so you cd to it first Sun Java tutorial definition of user.dir
user.dir 是你的 shell 的当前工作目录,所以你 cd 到它第一个user.dir 的 Sun Java 教程定义
回答by Ian Kemp
Try
尝试
Document doc = docBuilder.parse(new File("./webservices.xml"));
Document doc = docBuilder.parse(new File("./webservices.xml"));

