I'm trying to run this snippet:
import org.apache.commons.io.IOUtils;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
var home = System.getProperty("user.home");
var location = home + "/Library/Application Support/MobileSync/Backup";
var pb = new ProcessBuilder("ls", "-al", location);
var p = pb.start();
System.out.println(p.waitFor());
IOUtils.copy(p.getInputStream(), System.out);
IOUtils.copy(p.getErrorStream(), System.err);
}
}
It should list a folder where iPhone's backups are stored. I'm getting the following output:
1 # exit code
total 0
ls: /Users/zjor/Library/Application Support/MobileSync/Backup: Operation not permitted
However, if I run the following command in the terminal under the same user it works well:
ls -al "/Users/zjor/Library/Application Support/MobileSync/Backup"
total 0
drwxr-xr-x 3 zjor staff 96 Oct 23 15:01 .
drwxr-xr-x 3 zjor staff 96 Jan 17 2023 ..
drwxr-xr-x@ 262 zjor staff 8384 Oct 23 14:55 00003101-123456780CC3XXXX
As far as I understand, the app should request special permissions to access some folders. But how to do it?
Any advice is appreciated.
to access restricted folders on Mac OS, you can also use the
Filesclass from thejava.nio.filepackage and you can useFiles.list()method to list the contents of the specified directory,so if the directory is restricted, it will throw anIOException!