Closed yshupletsov closed 5 months ago
Good day,
Any updates on this task?
Sorry for the late response!
Yes, this is most likely due to the module restrictions and the warnings you received. You should be able to expose the necessary classes using the following JVM flag:
--add-opens java.base/sun.nio.ch=org.jnrproject.posix --add-opens java.base/java.io=org.jnrproject.posix
If that doesn't work post an example I can run and I'll help you get it going!
Good day,
Looks like "--add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED" solving the problem.
If you would rather not open those up to all unnamed modules, you can put the jnr-posix and other jnr jars in --module-path instead and use something like the line I provided above.
I would still very much like to see a reproducible example! I want to make a nicer error than the one the JDK produces and add some documentation for this. Can you provide a simple example that triggers the warning/error for you?
The code should be like this with some jars in cp, but I cannot run it successfully on java8.
import jnr.posix.FileStat;
import jnr.posix.POSIX;
import java.io.File;
import java.io.FileInputStream;
import jnr.posix.POSIXFactory;
import static org.apache.commons.io.FileUtils.openInputStream;
public class PosixTest {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = openInputStream(new File("test"));
POSIX posix = POSIXFactory.getPOSIX();
FileStat fstat = posix.fstat(fileInputStream.getFD());
}
}
@yshupletsov Thank you!
Thank you for the example. I have pushed changes in #192 that will make it clearer what is necessary to avoid the error, as well as code to remove the warnings when running on Java versions earlier than 17.
This is the output from your example now, when no --add-opens
flags are provided:
Some JDK modules may not be open to jnr-posix, which will break file descriptor and process APIs. See https://github.com/jnr/jnr-posix/wiki/Using-POSIX-with-Java-Modules
Exception in thread "main" java.lang.RuntimeException: native error calling fstat: Bad file descriptor -1
at jnr.posix.util.DefaultPOSIXHandler.error(DefaultPOSIXHandler.java:24)
at jnr.posix.BaseNativePOSIX.fstat(BaseNativePOSIX.java:139)
at jnr.posix.CheckedPOSIX.fstat(CheckedPOSIX.java:135)
at jnr.posix.LazyPOSIX.fstat(LazyPOSIX.java:132)
at PosixTest.main(PosixTest.java:12)
See https://github.com/jnr/jnr-posix/wiki/Using-POSIX-with-Java-Modules.
Moving to Java 17 I could not get fstat of the file descriptor:
The code:
Could it be related to the reflection warnings I got on Java 11?
Running the code with --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED has no success as well.