Java File.setWritable() 在 JDK 6u18 之后停止正常工作
我们有一个带有特定模块的 Java 应用程序,该模块在执行其功能之前检查临时目录是否“可写”。为了测试这一点,我们有一个 JUnit 测试,它创建一个新目录,使用 Java File
类方法 setWritable(false)
使该目录“不可写”,然后通过该目录指向正在测试的模块,并期望返回一个 IllegalArgumentException
。这在 JDK 6u18 下已经运行良好很长时间了。
今天,我已将 JDK 版本更新为 JDK 6u24(Sun 网站今天发布的最新版本)。新 JDK 的单元测试刚刚开始失败。我在团队中四处询问,发现其他人不久前曾尝试在他们的计算机上运行 JDK 6u23,并且在相同的 JUnit 测试中遇到了同样的问题(并且只有该测试 - 其他一切都正常)。
有其他人在更新到较新的 JDK 后遇到过 setWritable()
方法的问题吗?知道如何解决这个问题吗?
在你提问之前,我也尝试过使用 setReadonly()
方法作为替代方法,但我得到了相同的结果。
We have a Java application with a particular module that checks if a temporary directory is 'writable' before executing its function. To test this, we have a JUnit test that creates a new directory, uses the Java File
class method setWritable(false)
to make the directory "not writable", then passes that directory to the module being tested and expects to get an IllegalArgumentException
back. This had all been working fine for a long time under JDK 6u18.
Today I have updated the JDK version to JDK 6u24 (the current release from the Sun site as of today). That unit test just started failing with the new JDK. I asked around within my team and found out that someone else had tried to run JDK 6u23 on their machine a while back and had the same problem with the same JUnit test (and only with that test - everything else works fine).
Has anyone else experienced problems with the setWritable()
method after updating to a newer JDK? Any idea how to resolve this?
Before you ask, I have also tried using the setReadonly()
method as an alternative, but I get the same result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 Windows,则可能与以下错误有关:https://bugs。 java.com/bugdatabase/view_bug?bug_id=6728842
显然
setWritable(false)
从来没有真正对 Windows 上的目录起作用,因为 readonly 标志实际上并没有使目录变为只读。If you're on Windows, it's probably related to this bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=6728842
Apparently
setWritable(false)
never really worked for directories on Windows, because the readonly flag doesn't actually make a directory readonly.