eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Spring Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 30% off until 31st March, 2026

>> EXPLORE ACCESS NOW

Partner – Diagrid – NPI EA (cat= Testing)
announcement - icon

In distributed systems, managing multi-step processes (e.g., validating a driver, calculating fares, notifying users) can be difficult. We need to manage state, scattered retry logic, and maintain context when services fail.

Dapr Workflows solves this via Durable Execution which includes automatic state persistence, replaying workflows after failures and built-in resilience through retries, timeouts and error handling.

In this tutorial, we'll see how to orchestrate a multi-step flow for a ride-hailing application by integrating Dapr Workflows and Spring Boot:

>> Dapr Workflows With PubSub

Course – Spring Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 30% off until 31st March, 2026

>> EXPLORE ACCESS NOW

1. Overview

In this article, we’ll explore how to obtain the classpath from a ClassLoader in recent OpenJDK distributions.

2. ClassLoaders and Classpath

The Java Virtual Machine uses ClassLoaders to resolve classes or other resources at runtime. Even though a ClassLoader implementation is free to choose a resolution mechanism, the classpath is the standard choice.

A classpath is an abstraction that Java programs use to locate classes and other resources using relative naming syntax. For example, if we start a Java program with:

java -classpath /tmp/program baeldung.Main

The Java Runtime will search for a file on the location /tmp/program/baeldung/Main.class, and, assuming it exists and is valid, the JVM will load the Class and execute the main method.

Once the program starts, any additional classloading will proceed by searching classes by their respective Internal Names, meaning, the fully qualified Class name with ‘.’ (dots) replaced by ‘/’ (slashes), as relative paths from the directory /tmp/program.

Furthermore, the classpath also supports:

  • Multiple locations, specified by the ‘:’ separator
  • Jar Files

Finally, we can specify any valid URL as classpath locations, not just file system paths. Therefore, objectively speaking a classpath is equivalent to a set of URLs.

3. Common Types of ClassLoaders

To be useful, a ClassLoader implementation must be capable of resolving instances of java.lang.Class objects by overriding the following method:

protected Class<?> findClass(String name) throws ClassNotFoundException {
    throw new ClassNotFoundException(name);
}

Implementing a bespoke ClassLoader can be tricky. ClassLoaders were designed like a LinkedList structure and each may have a parent ClassLoader which can be queried by the getParent() method to support the delegation model.

Lastly, there is no mandatory coupling between a classpath and a ClassLoader and we’ll restrict ourselves to well-known types that effectively employ the classpath mechanism for resource resolution.

3.1. java.net.URLClassLoader

The URLClassLoader exposes its classpath via the method getURLs() and it’s the usual choice when a program needs to handle dynamic classpaths. For instance, Tomcat extends URLClassLoader to segregate the classpath of web applications: the container gives each deployed app its isolated classpath, separate from the others.

In addition, a URLClassLoader is equipped with logic to handle not only local directories but also jar files hosted on disk or remotely accessible via HTTP or other protocols, such as FTP, as long as they support the URL::openConnection() method.

3.2. The Application ClassLoader

The ClassLoader responsible for launching a Java program, that is the ClassLoader that loads a class with a main method and executes it, is called the Application ClassLoader.

In older JDKs (<=9) the Application ClassLoader was a subclass of URLClassLoader. However, the hierarchy has changed since OpenJDK 10 and the Application ClassLoader inherits from the module-private jdk.internal.loader.BuiltinClassLoader, which is not a subclass of URLClassLoader.

By definition, the Application ClassLoader will have its classpath bound to the -classpath startup option. We can retrieve this information at runtime by querying the system property java.class.path, however, this can be inadvertently overwritten at any time and if we want to determine the actual runtime classpath we must query the ClassLoader itself.

4. Obtaining the Classpath From a ClassLoader

To obtain the classpath from a given ClassLoader, let’s start by defining a ClasspathResolver interface, which exposes the capability of querying the classpath of a single ClassLoader and also its whole hierarchy:

package com.baeldung.classloader.spi;

public interface ClasspathResolver {
    void collectClasspath(ClassLoader loader, Set<URL> result);

    default Set<URL> getClasspath(ClassLoader loader) {
        var result = new HashSet<URL>();
        collectClasspath(loader, result);
        return result;
    }

    default Set<URL> getFullClasspath(ClassLoader loader) {
        var result = new HashSet<URL>();
        collectClasspath(loader, result);
        loader = loader.getParent();

        while (loader != null) {
            collectClasspath(loader, result);
            loader = loader.getParent();
        }

        return result;
    }
}

As we mentioned before, Application ClassLoaders do not inherit from URLClassLoader anymore, thus we can’t simply obtain the associated classpath by calling the URLClassLoader::getURLs() method. However, there is still a common denominator to URLClassLoader and BuiltinClassLoader classes: both wrap an instance of jdk.internal.loader.URLClassPath, which is the actual class responsible for locating URL-based resources.

Therefore, an effective implementation of the ClasspathResolver interface will have to tinker with non-visible classes from the JDK, consequently requiring access to non-exported packages.

Since exposing JDK internals to a whole program is a bad practice, it would be better if we isolate this capability by placing the ClasspathResolver interface and its implementations in a modular jar, by also defining a module-info file:

module baeldung.classloader {
    exports com.baeldung.classloader.spi;
}

4.1. Basic Implementation

A basic implementation of ClasspathResolver will only handle URLClassLoaders and will always be available whether the program has access to JDK internals or not:

public class BasicClasspathResolver implements ClasspathResolver {
    @Override
    public void collectClasspath(ClassLoader loader, Set<URL> result) {
        if(loader instanceof URLClassLoader ucl) {
            var urls = Arrays.asList(ucl.getURLs());

            result.addAll(urls);
        }
    }
}

4.2. Extended Implementation

This implementation will demand non-exported OpenJDK classes and a few things can go wrong, namely:

  • Consumers of our library might not set proper runtime flags
  • The program is running in a different JDK, that may not have BuiltinClassLoaders
  • Future versions of OpenJDK may change or remove BuiltinClassLoader, rendering this implementation useless

When using internal JDK APIs we must program defensively, which means taking a few extra steps to compile and run our programs correctly.

First, we must export the jdk.internal.loader package to our module, by adding proper command line options to the compiler. Then, we must also open the package to support reflective access at runtime to test our implementation.

When using Maven as the build and test tool, the plugins have to be set up like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>22</source>
                <target>22</target>
                <compilerArgs>
                    <arg>--add-exports</arg>
                    <arg>java.base/jdk.internal.loader=baeldung.classloader</arg>
                </compilerArgs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>--add-opens java.base/jdk.internal.loader=baeldung.classloader</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

Note: If you prefer not to use modular projects, you can omit the module-info file. Instead, replace the baeldung.classloader module with ALL-UNNAMED in the exports and open clauses.

Some IDEs might require additional configuration as well. For example, in Eclipse, it is necessary to expose the package in Module Dependencies as shown in the picture below:

Export jdk.internal.loader

Now, we are ready to code our extended implementation. Essentially, the call chain we need is:

BuiltinClassLoader -> URLClassPath -> getURLs()

The URLClassPath instance is hoisted in the BuiltinClassLoader#ucp private field, therefore we must obtain it via reflection. This might fail at runtime and, in case it does what should we do?

We will opt to fall back to BasicClasspathResolver, and to do so we should code a support class that tells whether or not we can access BuiltinClassLoader#ucp:

public class InternalJdkSupport {

    static final Class<?> BUILT_IN_CLASSLOADER;
    static final VarHandle UCP;

    static {
        var log = LoggerFactory.getLogger(InternalJdkSupport.class);
        var version = System.getProperty("java.version");

        Class<?> clazz = null;
        VarHandle ucp = null;

        try {
            var ucpClazz = Class.forName("jdk.internal.loader.URLClassPath");
            clazz = Class.forName("jdk.internal.loader.BuiltinClassLoader");
            var lookup = MethodHandles.privateLookupIn(clazz, MethodHandles.lookup());
            ucp = lookup.findVarHandle(clazz, "ucp", ucpClazz);
        } catch (ClassNotFoundException e) {
            log.warn("JDK {} not supported => {} not available.", version, e.getMessage());
        } catch (NoSuchFieldException e) {
            log.warn("JDK {} not supported => BuiltinClassLoader.ucp not present", version);
        } catch (IllegalAccessException e) {
            log.warn("""
                BuiltinClassLoader.ucp requires \
                --add-opens java.base/jdk.internal.loader=baeldung.classloader
                """);
        }

        BUILT_IN_CLASSLOADER = clazz;
        UCP = ucp;
    }

    public static boolean available() {
        return UCP != null;
    }

    public static Object getURLClassPath(ClassLoader loader) {
        if (!isBuiltIn(loader)) {
            throw new UnsupportedOperationException("Loader not an instance of BuiltinClassLoader");
        }

        if (UCP == null) {
            throw new UnsupportedOperationException("""
                Program must be initialized with \
                --add-opens java.base/jdk.internal.loader=baeldung.classloader
                """);
        }

        try {
            return UCP.get(loader);
        } catch (Exception e) {
            throw new InternalError(e);
        }
    }

    static boolean isBuiltIn(ClassLoader loader) {
        return BUILT_IN_CLASSLOADER != null && BUILT_IN_CLASSLOADER.isInstance(loader);
    }
}

With InternalJdkSupport in place, our extended ClasspathResolver implementation, capable of extracting URLs from Application ClassLoaders becomes simply:

import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.loader.URLClassPath;
public final class InternalClasspathResolver implements ClasspathResolver { @Override public void collectClasspath(ClassLoader loader, Set<URL> result) { var urls = switch (loader) { case URLClassLoader ucl -> Arrays.asList(ucl.getURLs()); case BuiltinClassLoader bcl -> { URLClassPath ucp = (URLClassPath) InternalJdkSupport.getURLClassPath(loader); yield ucp == null ? Collections.<URL> emptyList() : Arrays.asList(ucp.getURLs()); } default -> { yield Collections.<URL> emptyList(); } }; result.addAll(urls); } }

4.3. Exposing the ClasspathResolver

Since our module exports only the com.baeldung.classloader.spi package, clients won’t be able to instantiate the implementations directly. Therefore, we should offer access to the implementations through a factory method:

public interface ClasspathResolver {

    static ClasspathResolver get() {
        if (InternalJdkSupport.available()) {
            return new InternalClasspathResolver();
        }

        return new BasicClasspathResolver();
    }

}

5. Conclusion

In this article, we’ve explored how to create a safe modular solution to obtain the classpath of common OpenJDK ClassLoaders.

The extended implementation was designed to work along OpenJDK, since it requires access to implementation-specific classes, however, it will work with other vendors such as Zulu, Graal, and Temurin.

Modularization limits the exposed JDK internals surface to a single, trusted module, preventing unwanted rogue access by third-party dependencies.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Spring Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 30% off until 31st March, 2026

>> EXPLORE ACCESS NOW

Course – Spring Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our Spring Sale. All Courses are 30% off until 31st March, 2026

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)