java.lang.SecurityException: Prohibited package name: java.com.xx

Using the idea to create a new project, and then build a program called "java" folder, when you start inside the main function, appear as an exception:

According exception information, navigate to java.lang.ClassLoader.preDefineClass investigation, find the following code fragment:

/* Determine protection domain, and check that: 
        - not define java.* class, 
        - signer of this class matches signers for the rest of the classes in package. 
*/  
    private ProtectionDomain preDefineClass(String name,  
    ProtectionDomain protectionDomain)  
    {  
    if (!checkName(name))  
        throw new NoClassDefFoundError("IllegalName: " + name);  
    if ((name != null) && [color=red]name.startsWith("java.")[/color]) {  
        throw new SecurityException("Prohibited package name: " +  
            name.substring(0, name.lastIndexOf('.')));  
    }  
    if (protectionDomain == null) {  
        protectionDomain = getDefaultDomain();  
    }  
  
    if (name != null)  
        checkCerts(name, protectionDomain.getCodeSource());  
  
    return protectionDomain;  
    }  
  
......  
  
// true if the name is null or has the potential to be a valid binary name  
    private boolean checkName(String name) {  
    if ((name == null) || (name.length() == 0))  
        return true;  
    if ((name.indexOf('/') != -1)  
        || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))  
        return false;  
    return true;  
    }  

Firstly, it can be seen preDefineClass class names were checked and found to java as a package name, then throw a security exception: Do not use the package name!

This security exception is a Java class loading "parent delegation model" (see here ) caused. In the parent delegation model, by the parent class loader to load a class, the lower loader is not loaded. In this embodiment the top of the loader loads the java BootstrapClassLoader defined under the classpath. * Classes in the package, and the package can not be loaded java.research by the lower loader AppClassLoader BootstrapClassLoader. It is also protective measures java security mechanism for malicious code taken.

Published 467 original articles · won praise 215 · views 190 000 +

Guess you like

Origin blog.csdn.net/qq_44868502/article/details/104842483