1

I am working in Java 11.0.11 with Amazon Corretto. This code is running in a beanshell file as part of an identity governance system.

I am trying to use java.nio methods with Windows network paths.

I am able to create the path object using any method (path.of, Paths.get, File.toPath(), FileSystem.getPath, etc) and a sun.nio.fs.WindowsPath object is always returned.

Then when I try to use the WindowsPath object with any java.nio methods I get the error: methodName(sun.nio.fs.WindowsPath) not found in class'java.nio.etc.etc

This happens with Files.exists(path), Files.createDirectory(path), Files.getOwner(path), etc, etc

I am failing to find related results in searching and I am even having issues when I go as far as to use the FileSystems.getDefault().getPath() as well as sun.nio.fs.WindowsFileSystem getPath and sun.nio.fs.WindowsPath toWindowsPath which all respond the same way. It can reach the method but there is no version of the method that supports an argument of type sun.nio.fs.WindowsPath.

If I fall back to java.io methods everything works fine but I am intending on supporting symbolicLinks which are much easier in nio not to mention the better error handling so I'd prefer if I can get this working with nio.

Is there any preexisting packages that handle managing windows network folders already?

Alternatively, if I can't find a solution to get this working natively in java my plan is to just write a powershell script that will do the heavy lifting but I'm facing more then a little bit of sunk cost fallacy with the logic I already fleshed out in 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);
}

This fails at Files.exists(fnew, LinkOption.NOFOLLOW_LINKS) with exception:

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

If I use java.io (new File("//idg-bridge/C$/TEMP/HomeDriveTest/testusername")).exists() it makes it past the exists check without issue and then fails in exactly the same way at Files.createDirectory(fnew) with exception:

Error in method invocation: Static method createDirectory( sun.nio.fs.WindowsPath ) not found in class'java.nio.file.Files'
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
Chris
  • 11
  • 2
  • Are you sure the code you’ve shown us (`Path fnew = …`) is your real code? Files.exists(Path, LinkOption...) exists, and as long as the type of fnew is `Path`, the code should work. However, there is no exists method (or any other method) which takes an argument of type WindowsPath, so if you declared your variable as `WindowsPath fnew = (WindowsPath) Path.of(Folder);` then the error you’re seeing would make sense. – VGR Aug 17 '23 at 19:33
  • 1
    Do you mean this beanshell: https://github.com/beanshell/beanshell ? This looks like a custom Java source interpreter. It looks like the wrong method is being looked up by that interpreter. – Jorn Vernee Aug 17 '23 at 19:37
  • 2
    The old problem of using `getClass()` on the result of `Path.of` instead of using the return type of `Path.of` for further lookups. – Johannes Kuhn Aug 17 '23 at 21:10
  • 2
    @JohannesKuhn but taken up to eleven as there even is a formal declaration (`Path fnew …`) but then ignored and wrongly replaced but the runtime type. – Holger Aug 21 '23 at 17:23

0 Answers0