Stay organized with collections Save and categorize content based on your preferences. SecureClassLoader
public class SecureClassLoader
extends ClassLoader
Known direct subclasses | URLClassLoader | This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. | |
This class extends ClassLoader with additional support for defining classes with an associated code source and permissions which are retrieved by the system policy by default.
Summary
Protected constructors |
SecureClassLoader() Creates a new SecureClassLoader using the default parent class loader for delegation. |
SecureClassLoader(ClassLoader parent) Creates a new SecureClassLoader using the specified parent class loader for delegation. |
Inherited methods |
From class java.lang.ClassLoader void | clearAssertionStatus() Sets the default assertion status for this class loader to false and discards any package defaults or class assertion status settings associated with the class loader. | final Class<?> | defineClass(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) Converts an array of bytes into an instance of class Class, with a given ProtectionDomain. | final Class<?> | defineClass(String name, ByteBuffer b, ProtectionDomain protectionDomain) Converts a ByteBuffer into an instance of class Class, with the given ProtectionDomain. | final Class<?> | defineClass(byte[] b, int off, int len) This method was deprecated in API level 15. Replaced by defineClass(String, byte[], int, int) | final Class<?> | defineClass(String name, byte[] b, int off, int len) Converts an array of bytes into an instance of class Class. | Package | definePackage(String name, String specTitle, String specVersion, String specVendor, String implTitle, String implVersion, String implVendor, URL sealBase) Defines a package by name in this ClassLoader. | Class<?> | findClass(String name) Finds the class with the specified binary name. | String | findLibrary(String libname) Returns the absolute path name of a native library. | final Class<?> | findLoadedClass(String name) Returns the class with the given binary name if this loader has been recorded by the Java virtual machine as an initiating loader of a class with that binary name. | URL | findResource(String name) Finds the resource with the given name. | Enumeration<URL> | findResources(String name) Returns an enumeration of URL objects representing all the resources with the given name. | final Class<?> | findSystemClass(String name) Finds a class with the specified binary name, loading it if necessary. | Package | getPackage(String name) This method was deprecated in API level 35. If multiple class loaders delegate to each other and define classes with the same package name, and one such loader relies on the lookup behavior of getPackage to return a Package from a parent loader, then the properties exposed by the Package may not be as expected in the rest of the program. For example, the Package will only expose annotations from the package-info.class file defined by the parent loader, even if annotations exist in a package-info.class file defined by a child loader. | Package[] | getPackages() Returns all of the Packages that have been defined by this class loader and its ancestors. | final ClassLoader | getParent() Returns the parent class loader for delegation. | URL | getResource(String name) Finds the resource with the given name. | InputStream | getResourceAsStream(String name) Returns an input stream for reading the specified resource. | Enumeration<URL> | getResources(String name) Finds all the resources with the given name. | static ClassLoader | getSystemClassLoader() Returns the system class loader. | static URL | getSystemResource(String name) Find a resource of the specified name from the search path used to load classes. | static InputStream | getSystemResourceAsStream(String name) Open for reading, a resource of the specified name from the search path used to load classes. | static Enumeration<URL> | getSystemResources(String name) Finds all resources of the specified name from the search path used to load classes. | Class<?> | loadClass(String name, boolean resolve) Loads the class with the specified binary name. | Class<?> | loadClass(String name) Loads the class with the specified binary name. | static boolean | registerAsParallelCapable() Registers the caller as #isRegisteredAsParallelCapable() parallel capable. | final void | resolveClass(Class<?> c) Links the specified class. | void | setClassAssertionStatus(String className, boolean enabled) Sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein. | void | setDefaultAssertionStatus(boolean enabled) Sets the default assertion status for this class loader. | void | setPackageAssertionStatus(String packageName, boolean enabled) Sets the package default assertion status for the named package. | final void | setSigners(Class<?> c, Object[] signers) Sets the signers of a class. | |
From class java.lang.Object Object | clone() Creates and returns a copy of this object. | boolean | equals(Object obj) Indicates whether some other object is "equal to" this one. | void | finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | final Class<?> | getClass() Returns the runtime class of this Object. | int | hashCode() Returns a hash code value for the object. | final void | notify() Wakes up a single thread that is waiting on this object's monitor. | final void | notifyAll() Wakes up all threads that are waiting on this object's monitor. | String | toString() Returns a string representation of the object. | final void | wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. | final void | wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. | |
Protected constructors
SecureClassLoader
protected SecureClassLoader ()
Creates a new SecureClassLoader using the default parent class loader for delegation.
If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.
| Throws |
SecurityException | if a security manager exists and its checkCreateClassLoader method doesn't allow creation of a class loader. |
SecureClassLoader
protected SecureClassLoader (ClassLoader parent)
Creates a new SecureClassLoader using the specified parent class loader for delegation.
If there is a security manager, this method first calls the security manager's checkCreateClassLoader method to ensure creation of a class loader is allowed.
| Parameters |
parent | ClassLoader: the parent ClassLoader |
| Throws |
SecurityException | if a security manager exists and its checkCreateClassLoader method doesn't allow creation of a class loader. |
Protected methods
defineClass
protected final Class<?> defineClass (String name, byte[] b, int off, int len, CodeSource cs)
Converts an array of bytes into an instance of class Class, with an optional CodeSource. Before the class can be used it must be resolved.
If a non-null CodeSource is supplied a ProtectionDomain is constructed and associated with the class being defined.
| Parameters |
name | String: the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix. |
b | byte: the bytes that make up the class data. The bytes in positions off through off+len-1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification. |
off | int: the start offset in b of the class data |
len | int: the length of the class data |
cs | CodeSource: the associated CodeSource, or null if none |
| Returns |
Class<?> | the Class object created from the data, and optional CodeSource. |
| Throws |
ClassFormatError | if the data did not contain a valid class |
IndexOutOfBoundsException | if either off or len is negative, or if off+len is greater than b.length. |
SecurityException | if an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if the class name begins with "java.". |
defineClass
protected final Class<?> defineClass (String name, ByteBuffer b, CodeSource cs)
Converts a ByteBuffer into an instance of class Class, with an optional CodeSource. Before the class can be used it must be resolved.
If a non-null CodeSource is supplied a ProtectionDomain is constructed and associated with the class being defined.
| Parameters |
name | String: the expected name of the class, or null if not known, using '.' and not '/' as the separator and without a trailing ".class" suffix. |
b | ByteBuffer: the bytes that make up the class data. The bytes from positions b.position() through b.position() + b.limit() -1 should have the format of a valid class file as defined by The Java™ Virtual Machine Specification. |
cs | CodeSource: the associated CodeSource, or null if none |
| Returns |
Class<?> | the Class object created from the data, and optional CodeSource. |
| Throws |
ClassFormatError | if the data did not contain a valid class |
SecurityException | if an attempt is made to add this class to a package that contains classes that were signed by a different set of certificates than this class, or if the class name begins with "java.". |
getPermissions
protected PermissionCollection getPermissions (CodeSource codesource)
Returns the permissions for the given CodeSource object.
This method is invoked by the defineClass method which takes a CodeSource as an argument when it is constructing the ProtectionDomain for the class being defined.
| Parameters |
codesource | CodeSource: the codesource. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]