Eclipse 中的 System.getProperty("user.dir") 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/722003/
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-09-19 13:38:55  来源:igfitidea点击:

System.getProperty("user.dir") anomaly in Eclipse

eclipse

提问by Dinuk

In my current project settings, I have configured Eclipse to place the compiled .class files in the /bin directory.

在我当前的项目设置中,我已将 Eclipse 配置为将编译后的 .class 文件放在 /bin 目录中。

My puzzle is that, when I run the application via the IDE and get it to print the current working directory (System.out.println(System.getProperty("user.dir")) I expect the console output to be /bin - instead the value printed is the projectRoot folder (without the /bin suffix).

我的困惑是,当我通过 IDE 运行应用程序并让它打印当前工作目录 (System.out.println(System.getProperty("user.dir")) 时,我希望控制台输出为 /bin -相反,打印的值是 projectRoot 文件夹(没有 /bin 后缀)。

If I were to navigate to the /bin folder directly, and execute my java class, the current working directory is printed as I expect it to be. What I would like is the Eclipse IDE to behave similarly.

如果我直接导​​航到 /bin 文件夹,并执行我的 java 类,当前工作目录将按照我的预期打印。我想要的是 Eclipse IDE 的行为类似。

Any ideas as to why I am observing this disparity? I have gone through many project build settings, but cannot seem to find anything that would cause this anomaly.

关于为什么我观察到这种差异的任何想法?我经历了许多项目构建设置,但似乎找不到任何会导致这种异常的东西。

Kind regards, Dinuk

亲切的问候, 迪努克

回答by Michael Myers

In Eclipse, the working directorydefaults to the project directory.
You can change this in the run properties
(Project->Properties->Run/DebugSettings->Edit->Argumentstab, at the bottom of the page).

在 Eclipse 中,工作目录默认为项目目录。
您可以在运行属性
Project->Properties->Run/DebugSettings->Edit->Arguments页面底部的选项卡)中更改此设置

Edit:Actually, the easiest way to get to it is Run->Open Run Dialog->Argumentstab.
It's been a while since I used Eclipse.

编辑:实际上,最简单的方法是Run->Open Run Dialog->Argumentstab。
自从我使用 Eclipse 以来已经有一段时间了。

From an example of Eclipse Setup New Project

来自Eclipse Setup New Project的示例

alt text http://apps.sourceforge.net/mediawiki/sprite2d/nfs/project/s/sp/sprite2d/a/ae/EclipseNewProjectDebugArguments.jpg

替代文字 http://apps.sourceforge.net/mediawiki/sprite2d/nfs/project/s/sp/sprite2d/a/ae/EclipseNewProjectDebugArguments.jpg