User.dir property not working on OSX jdk 1.8.0_111? what about other OS versions?

I only have this mac, and I am wondering if this problem exists for other OS or, possibly, for jdks. This test failed when the second line in the last verifies that the absolute path refers to the real file (and matches the first path of the paragraph in the file). BUT calling f2.exists returns false as if it did not exist.

I am curious on windows and linux if this also fails?

@Test public void testUserDirProp() { File f = new File("src/test/resources/logback-test.xml"); //assert absolute path is correct Assert.assertEquals("/Library/Workflow/webpieces/core/core-util/src/test/resources/logback-test.xml", f.getAbsolutePath()); Assert.assertTrue(f.exists()); //NOW, change user.dir System.setProperty("user.dir", "/Library/Workflow/webpieces/core/core-util/src"); //Now, f2 is relative to NEW user.dir property File f2 = new File("test/resources/logback-test.xml"); //verify absolute path is still the full correct path for f2 and it is Assert.assertEquals("/Library/Workflow/webpieces/core/core-util/src/test/resources/logback-test.xml", f2.getAbsolutePath()); //since absolute path was correct, it should exist Assert.assertTrue(f2.exists()); } 
0
java
Jul 16 '17 at 16:06
source share
1 answer

user.dir property is not broken. Customization is not supported or guaranteed in any way. See http://bugs.java.com/bugdatabase/view_bug.do?bug_id=4117557 and related tickets:

After careful consideration, we still do not believe that current behavior can / should be changed. We never guaranteed that “user.dir” would be consulted at any given time, and unfortunately jdk suggested that this property does not change. Ideally, we introduce the concept of a read-only system property to protect against unsupported changes to this and other system properties.

"user.dir", which is initialized during jvm startup, should be used as an informative / readonly system property, try setting it using the command line -Duser.dir = xyz will end when dependency / undefined behavior is implemented. The current implementation of FileSystem is heavily dependent on the assumption that we do not have a "chdir" or "chdir" function (for example, use -Duser.dir = xyz) that changes the "current user directory", rather than a single jvm run. However, the inconsistent behavior of FileIn / OutputStream is really buggy behavior (FileIn / OutputStream open impl comes down to naive opening directly without consulting java File / FileSystem due to the assumption mentioned above).

To change the current implementation to support the "custom" user.dir, a large number, many classes / lines of change, just do it if we think it's really worth doing.

+2
Jul 16 '17 at 16:53 on
source share



All Articles