Can't open file on macOS - FileNotFoundException, Operation not permitted

4.8k views Asked by At

It's a simple program:

new FileInputStream("/Users/me/Desktop/foo.png")

I'm trying to run it from IntelliJ and I get:

Exception in thread "main" java.io.FileNotFoundException: /Users/me/Desktop/foo.png (Operation not permitted)

The unix perms are the usual rw-r--r--. It's something to do with macOS's extra security.

How can I open files in Java programs on macOS now?

This is macOS 11.0 beta (Big Sur). Maybe it was happening before 11.0 too; I don't know.

1

There are 1 answers

1
MarsAtomic On

You're running into a MacOS specific issue. Checking classic POSIX permissions isn't quite enough with MacOS, since the operating system introduced a number of mechanisms to provide fine-grained control as well as security.

Access Control Lists

POSIX permissions provide for three groupings to which access may be controlled: owner, group and everyone else. ACLs, on the other hand, allow for the creation of Access Control Entities, which maybe defined in a number of ways. This isn't a *NIX primer, so let's just get to the meaty bits. To check ACL, pull up a terminal session, navigate to the directory containing your target file and type:

ls -le foo.png

You should see POSIX permissions, followed by a numbered list of ACL rules. If your account is affected by one of those rules, you can remove the rule as follows:

sudo chmod -a# num foo.png

where num is the numerical identifier for the offending rule.

File Flags

File flags are another parallel permissions system. Using terminal again, try:

ls -lO foo.png

NOTE: the flag is lowercase letter L followed up uppercase letter O. Con't confuse one or the other for numerals 1 or 0.

You'll again see POSIX info, followed by a string describing any flags pertaining to the file in question. The flags you care about are uchg and schg (denoting user immutability flag and system immutability flag, respectively).

You can get rid of these flags as follows:

sudo chflags nouchg foo.png
sudo chflags noschg foo.png

Extended Attributes

Check your file for extended attributes like so:

xattr -l $foo.png

Remove all extended attributes like so:

sudo xattr -c $foo.png

System Integrity Protection

System Integrity Protection may have designated your file as "rootless." Check the status at the terminal with:

ls -l@ foo.png

Clearing the rootless flag is a bit more painful. Boot via recovery disk, pull up a terminal, navigate to your file and type:

sudo xattr -d com.apple.rootless foo.png

I'm no sysadmin, but at least one of these things should cover your issue, assuming you've verified that your account has POSIX permissions and the file isn't just simply locked via Finder (Command-I, look for the little checkbox in the corner).