getPackages(): This method is available in the java.lang.Package class of Java.
Syntax:
Package[] java.lang.Package.getPackages()
This method returns all of the Packages defined by the caller's class loader and its ancestors. The returned array may contain more than one Package object of the same package name, each defined by a different class loader in the class loader hierarchy.
Parameters: NA
Returns: The array of Package objects defined by this class loader and its ancestors.
Exceptions: NA
Approach
Java
package com.Package;import java.util.Arrays;public class PackagegetPackages {public static void main(String[] args)throws ClassNotFoundException {System.out.println(Arrays.toString(Package.getPackages()));}}
Output:
[package java.util.stream, package sun.util.locale, package sun.security.action, package java.lang.reflect, package java.lang.constant, package sun.net.util, package sun.security.util, package sun.net.www.protocol.file, package sun.io, package sun.net.www, package java.nio.charset, package java.util.concurrent.atomic, package java.util, package sun.nio.cs, package java.security, package java.util.jar, package sun.nio, package sun.invoke.util, package sun.invoke.empty, package java.util.function, package java.lang, package jdk.internal.org.objectweb.asm, package jdk.internal.util, package java.util.concurrent.locks, package java.lang.ref, package java.nio.charset.spi, package sun.reflect.annotation, package jdk.internal.reflect, package sun.launcher, package java.security.cert, package java.lang.invoke, package java.io, package java.nio, package jdk.internal.module, package java.lang.annotation, package jdk.internal.misc, package sun.util, package java.lang.module, package jdk.internal.loader, package jdk.internal.ref, package java.util.concurrent, package java.net, package jdk.internal.access, package com.Package]
Some other methods of Package
getAnnotation(Class): This method returns this element's annotation for the specified type if such an annotation is present, else null.
getAnnotations(): This method returns annotations that are present on this element.
getAnnotationsByType(Class): This method returns annotations that are associated with this element.
getDeclaredAnnotation(Class): This method returns this element's annotation for the specified type if such an annotation is directly present, else null. This method ignores inherited annotations.
getDeclaredAnnotations(): This method returns annotations that are directly present on this element. This method ignores inherited annotations.
getDeclaredAnnotationsByType(Class): This method returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.
getImplementationTitle(): This method returns the title of this package.
getImplementationVendor(): This method returns the vendor that implemented this package, null is returned if it is not known.
getImplementationVersion(): This method returns the version of this implementation.
getName(): This method returns the name of this package.
getPackages(): This method returns all of the Packages defined by the caller's class loader and its ancestors.
getSpecificationTitle(): This method returns the title of the specification that this package implements.
getSpecificationVendor(): This method returns the name of the organization, vendor, or company that owns and maintains the specification of the classes that implement this package.
getSpecificationVersion(): This method returns the version number of the specification that this package implements.
hashCode(): This method returns the hash code computed from the package name.
isAnnotationPresent(Class): This method returns true if an annotation for the specified type is present on this element, else false.
isCompatibleWith(String): This method compares this package's specification version with the desired version.
isSealed(): This method returns true if this package is sealed.
isSealed(URL): This method returns true if this package is sealed with respect to the specified code source URL.
toString(): This method returns the string representation of this Package.
No comments:
Post a Comment