Java SecurityManager checkPackageDefinition()方法与示例
SecurityManager類的checkPackageDefinition()方法 (SecurityManager Class checkPackageDefinition() method)
checkPackageDefinition() method is available in java.lang package.
checkPackageDefinition()方法在java.lang包中可用。
We call getProperty("package.definition") to get a list of restricted packages and it checks when our pkg_name starts with or similar to any of the list of restricted packages and when it matches then it calls checkPermission with the RuntimePermission("defineClassInPackage."+pkg_name).
我們調(diào)用getProperty(“ package.definition”)來獲取受限程序包列表,它會檢查何時(shí)pkg_name以任何受限程序包列表開頭或相似,并且何時(shí)匹配,然后使用RuntimePermission(“ defineClassInPackage)調(diào)用checkPermission。 “ + pkg_name)。
checkPackageDefinition() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
checkPackageDefinition()方法是一種非靜態(tài)方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯(cuò)誤消息。
checkPackageDefinition() method may throw an exception at the time of defining the class in the given package.
在給定包中定義類時(shí), checkPackageDefinition()方法可能會引發(fā)異常。
SecurityException – This exception may throw when the calling thread does not have the right to define classes in the given package.
SecurityException-當(dāng)調(diào)用線程無權(quán)在給定包中定義類時(shí),可能引發(fā)此異常。
Syntax:
句法:
public void checkPackageDefinition(String pkg_def);Parameter(s):
參數(shù):
String pkg_def – represents the name of the package.
字符串pkg_def –表示軟件包的名稱。
Return value:
返回值:
The return type of this method is void, it returns nothing.
此方法的返回類型為void ,不返回任何內(nèi)容。
Example:
例:
// Java program to demonstrate the example // of void checkPackageDefinition(String pkg_def) // method of SecurityManager public class checkPackageDefinition extends SecurityManager {// override checkPackageDefinition() method of SecurityManagerpublic void checkPackageDefinition(String pkg_def) {throw new SecurityException("Restricted...");}public static void main(String[] args) throws Exception {// By using setProperty() method is to set the policy property // with security managerSystem.setProperty("java.security.policy", "file:/C:/java.policy");// Instantiating a checkPackageDefinition objectcheckPackageDefinition cpd = new checkPackageDefinition();// By using setSecurityManager() method is to set the// security managerSystem.setSecurityManager(cpd);// By using checkPackageDefinition(pkg_def) method is to check // that package is defined or notcpd.checkPackageDefinition("java.lang");// Display the messageSystem.out.println("Not Restricted..");} }Output
輸出量
Exception in thread "main" java.lang.SecurityException: Restricted...at checkPackageDefinition.checkPackageDefinition(checkPackageDefinition.java:8)at checkPackageDefinition.main(checkPackageDefinition.java:25)翻譯自: https://www.includehelp.com/java/securitymanager-checkpackagedefinition-method-with-example.aspx
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Java SecurityManager checkPackageDefinition()方法与示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++ scanf读取_使用scanf(
- 下一篇: Java Collections unm