I'm trying to write a Java application running on a Linux environment on an NFS filesystem.
I noticed that when I call java.io.File.exists()
, it returns false for both ESTALE
(Stale NFS file handle) and ENOENT
(No such file or directory). For my application, I need to be able to differentiate between the two.
Currently I am considering implementing the stat()
call using JNA, but that seems to be overkill, what with having to implement the whole stat structure and all the __xstat64
stuff which seems so platform dependent.
Is there a simple way to simply obtain the underlying errno
after a Java call like File.exists()
or any other ideas to solve this problem?
With JNA, you don't actually have to implement the stat
structure; you just need to allocate memory for it sufficiently large to represent the native structure.
You can then call Native.getLastError()
to retrieve the errno
value.
Pointer fake_stat = new Memory(2048); // big enough
if (clib.stat(filename, fake_stat) != 0) {
int errno = Native.getLastError();
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With