I am trying to learn about Java's permission model. I tried this sample code:
public static void main(String[] args) {
File file = new File("/etc/passwd");
try (BufferedReader reader = new BufferedReader(new FileReader(file));) {
reader.lines().forEach(s -> System.out.println(s));
} catch (IOException e) {
e.printStackTrace();
}
FilePermission perm = new FilePermission("/etc/passwd", "read");
AccessController.checkPermission(perm); // throws Exception
}
This prints the contents of /etc/passwd
fine, but throws an exception in the end:
Exception in thread "main" java.security.AccessControlException: access denied ("java.io.FilePermission" "" "read")
Why is it that the file read works fine, but a check for the permission gives a negative result?