静态方法存在( sun.nio.fs.WindowsPath, java.nio.file.LinkOption ) 在类中找不到 java.nio.file.files'

Static method exists( sun.nio.fs.WindowsPath, java.nio.file.LinkOption ) not found in class'java.nio.file.Files'

提问人:Chris 提问时间:8/18/2023 最后编辑:Jorn VerneeChris 更新时间:8/18/2023 访问量:88

问:

我正在使用 Amazon Corretto 使用 Java 11.0.11。 此代码作为身份治理系统的一部分在 beanshell 文件中运行。

我正在尝试将java.nio方法与Windows网络路径一起使用。

我可以使用任何方法(path.of、Paths.get、File.toPath()、FileSystem.getPath 等)创建路径对象,并且始终返回一个 sun.nio.fs.WindowsPath 对象。

然后,当我尝试将 WindowsPath 对象与任何 java.nio 方法一起使用时,我收到错误:methodName(sun.nio.fs.WindowsPath) not found in class'java.nio。等等

Files.exists(path)、Files.createDirectory(path)、Files.getOwner(path都会发生这种情况

我在搜索中找不到相关结果,甚至在使用 FileSystems.getDefault().getPath() 以及 sun.nio.fs.WindowsFileSystem getPath 和 sun.nio.fs.WindowsPath toWindowsPath 时都遇到了问题,它们都以相同的方式响应。它可以访问该方法,但没有支持 sun.nio.fs.WindowsPath 类型的参数的方法版本。

如果我回退到 java.io 方法,一切正常,但我打算支持symbolicLinks,这在nio中要容易得多,更不用说更好的错误处理了,所以我更希望我能把它与nio一起工作。

是否有任何预先存在的包已经处理管理 Windows 网络文件夹?

或者,如果我找不到一个解决方案来让它在 java 中原生工作,我的计划是只编写一个 powershell 脚本来完成繁重的工作,但我面临着更多的沉没成本谬误,我已经在 java 中充实了逻辑。

import lava.lang.*;
import java.nio.*;
import java.io.*;
String Folder = "//idg-bridge/C$/TEMP/HomeDriveTest/testusername";
Path fnew = Path.of(Folder,new String[0]);
if(!Files.exists(fnew, LinkOption.NOFOLLOW_LINKS))
{
    Files.createDirectory(fnew);
}

这在 Files.exists(fnew, LinkOption.NOFOLLOW_LINKS) 处失败,并出现异常:

Error in method invocation: Static method exists( sun.nio.fs.WindowsPath, java.nio.file.LinkOption ) not found in class'java.nio.file.Files'

如果我使用 java.io (new File(“//idg-bridge/C$/TEMP/HomeDriveTest/testusername”)).exists(),它会毫无问题地通过存在检查,然后在 Files.createDirectory(fnew) 以完全相同的方式失败,并出现异常:

Error in method invocation: Static method createDirectory( sun.nio.fs.WindowsPath ) not found in class'java.nio.file.Files'
java-11 java-io beanshell java.nio.file

评论

0赞 VGR 8/18/2023
您确定您向我们展示的代码 () 是您的真实代码吗?Files.exists(Path, LinkOption...) 存在,只要 fnew 的类型是 ,代码就应该有效。但是,没有 exists 方法(或任何其他方法)采用 WindowsPath 类型的参数,因此,如果您将变量声明为 ,那么您看到的错误将是有意义的。Path fnew = …PathWindowsPath fnew = (WindowsPath) Path.of(Folder);
1赞 Jorn Vernee 8/18/2023
你是说这个豆子:github.com/beanshell/beanshell?这看起来像一个自定义的 Java 源代码解释器。看起来该解释器正在查找错误的方法。
2赞 Johannes Kuhn 8/18/2023
对结果使用而不是使用返回类型进行进一步查找的旧问题。getClass()Path.ofPath.of
2赞 Holger 8/22/2023
@JohannesKuhn但最多占用了 11 个,因为甚至有一个正式声明 (),但随后被忽略并错误地替换了运行时类型。Path fnew …

答: 暂无答案