getResources(String): This method is available in the java.lang.ClassLoader class of Java.
Syntax:
Enumeration<URL> java.lang.ClassLoader.getResources(String name) throws IOException
This method takes one argument. This method finds all the resources with the given name.
A resource is some data(images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.
The name of a resource is a /-separate path name that identifies the resource.
Parameters: One parameter is required for this method.
name: The resource name.
Returns: An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.
Throws:
1. IOException - If I/O errors occur.
2. NullPointerException - If the name is null.
Approach 1: When no exception
Java
package com.ClassLoader;import java.io.IOException;public class ClassLoadergetResources {public static void main(String[] args) throws IOException {ClassLoader classLoader =ClassLoader.getSystemClassLoader();String name = "String";System.out.println(classLoader.getResources(name));}}
Output:
java.lang.CompoundEnumeration@7b3300e5
Approach 2: NullPointerException
Java
package com.ClassLoader;import java.io.IOException;public class ClassLoadergetResources {public static void main(String[] args) throws IOException {ClassLoader classLoader =ClassLoader.getSystemClassLoader();String name = null;System.out.println(classLoader.getResources(name));}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.lang.ClassLoader.getResources(ClassLoader.java:1468) at com.ClassLoader.ClassLoadergetResources.main(ClassLoadergetResources.java:12)
Some other methods of ClassLoader class
clearAssertionStatus(): This method 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.
getDefinedPackage(String): This method returns a Package of the given name that has been defined by this class loader.
getDefinedPackages(): This method returns all of the Packages that have been defined by this class loader.
getName(): This method returns the name of this class loader or null if this class loader is not named.
getParent(): This method returns the parent class loader for delegation. Some implementations may use null to represent the bootstrap class loader.
ClassLoader.getPlatformClassLoader(): This method returns the platform class loader. All platform classes are visible to the platform class loader.
getResource(String): This method finds the resource with the given name.
getResourceAsStream(String): This method returns an input stream for reading the specified resource.
getResources(String): This method finds all the resources with the given name.
ClassLoader.getSystemClassLoader(): This method returns the system class loader.
ClassLoader.getSystemResource(String): This method finds a resource of the specified name from the search path used to load classes.
ClassLoader.getSystemResourceAsStream(String): This method is open for reading, a resource of the specified name from the search path used to load classes.
ClassLoader.getSystemResources(String): This method finds all resources of the specified name from the search path used to load classes.
getUnnamedModule(): This method returns the unnamed Module for this class loader.
isRegisteredAsParallelCapable(): This method returns true if this class loader is registered as parallel capable, otherwise false.
loadClass(String): This method loads the class with the specified binary name.
resources(String): This method returns a stream whose elements are the URLs of all the resources with the given name.
setClassAssertionStatus(String, boolean): This method sets the desired assertion status for the named top-level class in this class loader and any nested classes contained therein.
setDefaultAssertionStatus(boolean): This method sets the default assertion status for this class loader.
setPackageAssertionStatus(String, boolean): This method sets the package default assertion status for the named package.
No comments:
Post a Comment