2

In the java 25 move, we're removing some code that uses reflection in a soon-to-be-disallowed way. Instead, we're looking at using JNA. I need to access a file descriptor (the unix kind) and I thought I'd open("some_path",O_CREAT|O_RDWR) , do whatever I need to do with the file descriptor and then open the same path with File instead. Problem is I get errno 2 (no such file or directory).
If I create the file first with through

var f = new File("some_path");
f.createNewFile();
int fd = JNALib.open("some_path", O_CREAT|O_RDWR);

Then that works perfectly. I do not understand why the first option fails. Mac OS X, ARM Mac. JNA 5.18.1

Code:

 @Test
 public void testSOOpenFile() throws Exception {
 String tmpFile = "dummy-file.tst";
 var relPath = Paths.get("").toAbsolutePath();
 Path file = Path.of(relPath + "/" + tmpFile);
 System.out.println("Creating " + file);
 int fd = JNALibMinimal.open(file.toString(), O_CREAT | O_RDWR);
 if (fd < 0 ) {
 System.out.println("JNALib failed to open with error code " + JNALib.getLastError());
 } else {
 System.out.println("JNALib successfully opened " + fd);
 }
 }
public class JNALibMinimal {
 static {
 Native.register(Platform.C_LIBRARY_NAME);
 }
 public static native int open(String pathName, int flags);
 public static native int open(String pathName, int flags, int mode);
}

UPDATE I (finally) figured out the problem and it was (not uncommon) CBtK (crap behind the keyboard). I've been too long away from c programming. The problem was that linux and Darwin actually has different values for the symbols O_RDWR & O_CREAT. Once I used Darwin values, the file was created ok.

asked Nov 4, 2025 at 15:27
12
  • If I create the file first with through... That's because a File is really more of a wrapper around the business of manipulating paths on the file system. Creating an object of type File doesn't create anything on the file system Commented Nov 4, 2025 at 15:43
  • 6
    When you’re migrating to Java 25, you may consider switching to the built-in facility for invoking native functions instead. Commented Nov 4, 2025 at 16:09
  • 1
    Is java.nio.file.Files.setPosixFilePermissions() not enough for your needs? Commented Nov 4, 2025 at 16:59
  • Also please add a minimal reproducible example to the question. Commented Nov 4, 2025 at 18:01
  • 1
    So, fileChannel.position(intendedSize - 1).write(ByteBuffer.allocate(1)) does not work for you? Commented Nov 5, 2025 at 8:36

1 Answer 1

2

You forget the third argument: the file mode (permissions) are required when creating a file

import java.io.File;
File file = new File("some/complex/path/to/my_file.dat");
File parentDir = file.getParentFile();
if (parentDir != null && !parentDir.exists()) {
 parentDir.mkdirs();
}
int fd = JNALib.INSTANCE.open(
 file.getAbsolutePath(),
 JNALib.O_CREAT | JNALib.O_RDWR, 
 JNALib.DEFAULT_MODE
);
Daniel Widdis
9,35513 gold badges50 silver badges70 bronze badges
answered Nov 4, 2025 at 15:53
Sign up to request clarification or add additional context in comments.

5 Comments

For the libc open() call, file permissions are optional.
The permissions are optional unless you're creating a new file. From the man page: "The oflag argument may indicate that the file is to be created if it does not exist (by specifying the O_CREAT flag). In this case, open requires a third argument mode_t mode". Max is correct here (and this answer could be improved by quoting this).
Since the c code works without using perms, no.
Want to clarify here, using O_CREAT without mode will grab the next 4 bytes in the frame as mode bytes so you'll get what is a random mode. I didn't use explicit mode because I wanted to simplify things.
So is it possible the "random" mode was 0 = no file access to even the user, thus resulting in the "no such file" error? In any case, I would expect "working" here means "giving nondeterministic results". :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.