SecurityManager secMgr = System.getSecurityManager(); if(secMgr != null) { secMgr.checkRead("/path/to/file"); // check if the application could read from the given file path } else { // security manager is not set }
SecurityManager secMgr = System.getSecurityManager(); if(secMgr != null) { secMgr.checkPermission(new SocketPermission("localhost:8080", "connect")); // check if the application could connect to the given host and port } else { // security manager is not set }This code gets the current security manager for the application and checks if the application has permission to connect to a specific socket. If the security manager is not set, the code will not proceed with the security check. Package/Library: java.util