Class ClassPathUtils

  • All Implemented Interfaces:
    Utils

    public abstract class ClassPathUtils
    extends java.lang.Object
    implements Utils
    ClassPathUtils is an abstract utility class that provides methods for retrieving various class path-related information.

    Key Features:

    • Retrieving bootstrap class paths
    • Retrieving application class paths
    • Locating the runtime URL of a class by name or type

    Example Usage:

    
     // Get all application class paths
     Set<String> classPaths = ClassPathUtils.getClassPaths();
     for (String path : classPaths) {
         System.out.println("ClassPath: " + path);
     }
    
     // Get the location of a specific class
     URL location = ClassPathUtils.getRuntimeClassLocation(io.microsphere.util.ClassPathUtils.class);
     if (location != null) {
         System.out.println("Class Location: " + location);
     }
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    ClassPathUtils
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.management.RuntimeMXBean runtimeMXBean  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Set<java.lang.String> getBootstrapClassPaths()
      Returns the set of bootstrap class paths.
      static java.util.Set<java.lang.String> getClassPaths()
      Returns the set of application class paths.
      static java.net.URL getRuntimeClassLocation​(java.lang.Class<?> type)
      Get Class Location URL from specified Class at runtime.
      static java.net.URL getRuntimeClassLocation​(java.lang.String className)
      Get Class Location URL from specified class name at runtime.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • runtimeMXBean

        protected static final java.lang.management.RuntimeMXBean runtimeMXBean
    • Method Detail

      • getBootstrapClassPaths

        @Nonnull
        public static java.util.Set<java.lang.String> getBootstrapClassPaths()
        Returns the set of bootstrap class paths.

        If RuntimeMXBean.isBootClassPathSupported() returns false, this method will return an empty set.

        Example Usage

        
         Set<String> bootClassPaths = ClassPathUtils.getBootstrapClassPaths();
         for (String path : bootClassPaths) {
             System.out.println("Bootstrap ClassPath: " + path);
         }
         
        Returns:
        a non-null set of bootstrap class paths; if boot class path is not supported, returns an empty set.
      • getClassPaths

        @Nonnull
        public static java.util.Set<java.lang.String> getClassPaths()
        Returns the set of application class paths.

        This method provides access to the class paths used by the Java runtime for loading application classes.

        Example Usage

        
         Set<String> classPaths = ClassPathUtils.getClassPaths();
         for (String path : classPaths) {
             System.out.println("ClassPath: " + path);
         }
         
        Returns:
        a non-null set of class paths; if no class paths are available, returns an empty set.
        See Also:
        RuntimeMXBean.getClassPath()
      • getRuntimeClassLocation

        @Nullable
        public static java.net.URL getRuntimeClassLocation​(java.lang.String className)
        Get Class Location URL from specified class name at runtime.

        If the class associated with the provided className is loaded by the default class loader (see ClassLoaderUtils.getDefaultClassLoader()), this method returns the location URL of that class. Otherwise, it returns null.

        Example Usage

        
         String className = "io.microsphere.util.ClassPathUtils";
         URL classLocation = ClassPathUtils.getRuntimeClassLocation(className);
         if (classLocation != null) {
             System.out.println("Class Location: " + classLocation);
         } else {
             System.out.println("Class not found or not loadable.");
         }
         
        Parameters:
        className - the fully qualified name of the class to find the location for
        Returns:
        the URL representing the location of the class if it is loaded by the default class loader; otherwise, null
        See Also:
        getRuntimeClassLocation(Class)
      • getRuntimeClassLocation

        @Nullable
        public static java.net.URL getRuntimeClassLocation​(java.lang.Class<?> type)
        Get Class Location URL from specified Class at runtime.

        This method determines the location (URL) from which the provided class was loaded. If the class is a primitive type, array type, or synthetic type, or if a security manager prevents access to the protection domain, this method returns null.

        Example Usage

        
         Class<?> type = io.microsphere.util.ClassPathUtils.class;
         URL classLocation = ClassPathUtils.getRuntimeClassLocation(type);
         if (classLocation != null) {
             System.out.println("Class Location: " + classLocation);
         } else {
             System.out.println("Class location could not be determined.");
         }
         
        Parameters:
        type - The class for which to find the location.
        Returns:
        The URL representing the location of the class if it can be determined; otherwise, null.