Class ResourceResolver

java.lang.Object
org.djutils.io.ResourceResolver

public final class ResourceResolver extends Object
ResourceResolver resolves a resource on the classpath, on a file system, as a URL or in a jar-file. For resources, it looks both in the root directory ad in the directory /resources as some Maven packaging stores the resources inside this folder. The class can handle HTTP resources, also with username and password. Entries within jar-files can be retrieved from any type of resource. Example of usage of the class:
 // Relative file (resolved against baseDir, but also against classpath and classpath/resources)
 var h1 = ResourceResolver.resolve("data/input.csv");
 
 // Absolute file
 var h2 = ResourceResolver.resolve("/var/tmp/report.txt");
 
 // HTTP(S)
 var h3 = ResourceResolver.resolve("https://example.com/file.json");
 
 // FTP with credentials
 var h4 = ResourceResolver.resolve("ftp://user:pass@ftp.example.com/pub/notes.txt");
 
 // Classpath with specific classloader and relative to a specific path
 var h5 = ResourceResolver.resolve("config/app.yaml", MyApp.class.getClassLoader(), Path.of("."));
 
 // Raw bang syntax -> normalized to jar:
 var h6 = ResourceResolver.resolve("file:/opt/app/lib/app.jar!/META-INF/MANIFEST.MF");
 
 // Use the result as a URL, URI, or Path:
 try (var in = h6.openStream())
 {
     // use h6 to read something from the stream
 }
 h6.asPath().ifPresent(path -> System.out.println(Files.size(path)));
 System.out.println(h6.asUrl());
 System.out.println(h6.asUri());
 

Copyright (c) 2025-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.

Author:
Alexander Verbraeck
  • Method Details

    • resolve

      public static ResourceResolver.ResourceHandle resolve(String resource, ClassLoader classLoader, Path baseDirPath)
      Resolve a resource string against a base directory and classloader.
      Parameters:
      resource - the specification of the resource to load
      classLoader - the class loader to specifically use (can be null)
      baseDirPath - the potential base directory to which the resource is relative (can be null)
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
    • resolve

      public static ResourceResolver.ResourceHandle resolve(String resource, ClassLoader classLoader, String baseDir)
      Resolve a resource string against a base directory and classloader.
      Parameters:
      resource - the specification of the resource to load
      classLoader - the class loader to specifically use (can be null)
      baseDir - the potential base directory to which the resource is relative
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
      InvalidPathException - if the baseDir path string cannot be converted to a Path
    • resolve

      public static ResourceResolver.ResourceHandle resolve(String resource, Path baseDirPath)
      resolve() method without ClassLoader and base path.
      Parameters:
      resource - the specification of the resource to resolve
      baseDirPath - the potential base directory to which the resource is relative (can be null)
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
    • resolve

      public static ResourceResolver.ResourceHandle resolve(String resource, String baseDir)
      resolve() method without ClassLoader and base path.
      Parameters:
      resource - the specification of the resource to resolve
      baseDir - the potential base directory to which the resource is relative
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
      InvalidPathException - if the baseDir path string cannot be converted to a Path
    • resolve

      public static ResourceResolver.ResourceHandle resolve(String resource)
      resolve() method without ClassLoader.
      Parameters:
      resource - the specification of the resource to resolve
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
    • resolveAsResource

      public static ResourceResolver.ResourceHandle resolveAsResource(String resource)
      Resolve the resource relative to the classloader path.
      Parameters:
      resource - the specification of the resource to resolve
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found
    • resolveAsFile

      public static ResourceResolver.ResourceHandle resolveAsFile(String resource)
      Resolve the resource relative to the base path, but NOT relative to the classloader path.
      Parameters:
      resource - the specification of the resource to resolve
      Returns:
      a resource handle to the resource
      Throws:
      NoSuchElementException - when the resource could not be found