Class KiwiJars

java.lang.Object
org.kiwiproject.jar.KiwiJars

public final class KiwiJars extends Object
Utilities for working with Java JAR files.
  • Method Details

    • getPathComponents

      public static List<String> getPathComponents(Class<?> classInJar)
      Get the path components of the JAR file path that the given class lives in, or an empty list if the path components could not be obtained.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      a mutable list of path components if found, or an immutable empty list if not found or an exception is thrown attempting to get the path
    • getPath

      public static Optional<String> getPath(Class<?> classInJar)
      Get the path of the JAR file that the given class lives in, or an empty Optional if the path could not be obtained for any reason.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      optional containing the path if found, or an empty optional
    • getDirectoryPath

      public static Optional<String> getDirectoryPath(Class<?> classInJar)
      Get the directory path of the JAR file that the given class lives in, or an empty Optional if the path could not be obtained for any reason.
      Parameters:
      classInJar - the class which resides in the JAR you want to find
      Returns:
      optional containing the directory path if found, or an empty optional
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(String mainAttributeName)
      Get the value of a main attribute from the manifest file (if found) in the current class loader.
      Parameters:
      mainAttributeName - The name of the main attribute to resolve
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(ClassLoader classLoader, String mainAttributeName)
      Get the value of a main attribute from the manifest file (if found) from the given class loader.
      Parameters:
      classLoader - The class loader to find the manifest file to search
      mainAttributeName - The name of the main attribute to resolve
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
    • readSingleValueFromJarManifest

      public static Optional<String> readSingleValueFromJarManifest(ClassLoader classLoader, String mainAttributeName, @Nullable Predicate<URL> manifestFilter)
      Get the value of a main attribute from the manifest file (if found) from the given class loader and filtering manifests using the given Predicate.
      Parameters:
      classLoader - The class loader to find the manifest file to search
      mainAttributeName - The name of the main attribute to resolve
      manifestFilter - An optional filter that can be used to filter down manifest files if there are more than one.
      Returns:
      an Optional containing the resolved value or Optional.empty() if not
      Implementation Note:
      If this code is called from a "fat-jar" with a single manifest file, then the filter predicate is unnecessary. The predicate filter is really only necessary if there are multiple jars loaded in the classpath all containing manifest files.
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(String... mainAttributeNames)
      Get the values of the given main attribute names from the manifest (if found) from the current class loader.
      Parameters:
      mainAttributeNames - an array of main attribute names to resolve from the manifest
      Returns:
      a Map<String, String> of resolved main attributes
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(ClassLoader classLoader, String... mainAttributeNames)
      Get the values of the given main attribute names from the manifest (if found) from the given class loader.
      Parameters:
      classLoader - the classloader to search for manifest files in
      mainAttributeNames - an array of names to resolve from the manifest
      Returns:
      a Map<String, String> of resolved main attributes
    • readValuesFromJarManifest

      public static Map<String,String> readValuesFromJarManifest(ClassLoader classLoader, @Nullable Predicate<URL> manifestFilter, String... mainAttributeNames)
      Get the values of the given main attribute names from the manifest (if found) from the given class loader and filtering manifests using the given Predicate.
      Parameters:
      classLoader - the classloader to search for manifest files in
      manifestFilter - a predicate filter used to limit which jar files to search for a manifest file
      mainAttributeNames - an array of names to resolve from the manifest
      Returns:
      a Map<String, String> of resolved main attributes
      Implementation Note:
      If this code is called from a "fat-jar" with a single manifest file, then the filter predicate is unnecessary. The predicate filter is really only necessary if there are multiple jars loaded in the classpath all containing manifest files.
    • readAllMainAttributesFromJarManifest

      public static Map<String,String> readAllMainAttributesFromJarManifest()
      Get the values of all main attributes from the manifest (if found) from the current class loader.
      Returns:
      a Map<String, String> of all main attributes
    • readAllMainAttributesFromJarManifest

      public static Map<String,String> readAllMainAttributesFromJarManifest(ClassLoader classLoader)
      Get the values of all main attributes from the manifest (if found) from the given class loader.
      Parameters:
      classLoader - the classloader to search for manifest files in
      Returns:
      a Map<String, String> of all main attributes
    • readAllMainAttributesFromJarManifest

      public static Map<String,String> readAllMainAttributesFromJarManifest(ClassLoader classLoader, @Nullable Predicate<URL> manifestFilter)
      Get the values of all main attributes from the manifest (if found) from the given class loader and filtering manifests using the given Predicate.
      Parameters:
      classLoader - the classloader to search for manifest files in
      manifestFilter - a predicate filter used to limit which jar files to search for a manifest file
      Returns:
      a Map<String, String> of all main attributes