Java “Unnamed Module of Loader ‘app’”: Meaning and Fixes

In Java, “are in unnamed module of loader ‘app’” means the classes belong to the unnamed module of a class loader named app, normally Java’s application class loader. This is diagnostic context, not the underlying error: in a ClassCastException, the important part is which class cannot be cast to which other class. Match the full exception to the symptoms below, then inspect the failing operation before changing dependencies or JVM settings.

Exception or Symptom Likely Cause Appropriate Fix
A cannot be cast to B, with both classes in loader `’app’ The object is not an instance of the requested type Correct the object’s source, use its actual interface, or explicitly convert its data
X cannot be cast to X, with different defining loaders Separate runtime copies of the same named type Make both sides use the same loaded API or class definition
AppClassLoader cannot be cast to URLClassLoader Older code assumes a particular system-loader implementation Upgrade the responsible library or remove that assumption
InaccessibleObjectException mentioning an unnamed module Reflective access to a package is blocked Upgrade the reflective code or configure narrowly targeted access

What “Unnamed Module of Loader ‘app’” Means

Consider this illustrative exception:

java.lang.ClassCastException: class com.example.Customer cannot be cast to class com.example.Order
(com.example.Customer and com.example.Order are in unnamed module of loader 'app')
    at com.example.Checkout.submit(Checkout.java:42)

The message contains three separate facts:

  • The failed operation: Java tried to treat a Customer object as an Order.
  • The module information: Both classes belong to an unnamed module.
  • The loader information: Both were defined by the loader identified as app.

The first fact explains why the cast failed. The remaining facts help distinguish an ordinary type mismatch from a class-loading problem.

The Unnamed Module Is Normal for Classpath Code

The Java Platform Module System, introduced in Java 9, supports named modules. Code loaded from the ordinary classpath instead belongs to an unnamed module associated with its defining class loader.

“Unnamed module” does not mean “unnamed package,” and it does not mean your application is missing a required module declaration. Classes can have normal package names and still belong to an unnamed module.

You generally do not need to add module-info.java to fix this message. Even a JAR containing a module descriptor normally behaves as classpath code when launched on the classpath rather than as a named module.

The Loader Name Identifies the Loading Context

A class loader loads class definitions into the JVM. The built-in application loader is normally named app; framework and plugin loaders may have other names or appear as a class name followed by an identity suffix.

The loader named in an exception belongs to the class being described. For example, an error involving the AppClassLoader class itself can mention bootstrap, because the JDK class implementing that loader is defined by the bootstrap loader.

Do not infer a loader conflict merely because loader information appears in the message. Java includes that information even for straightforward invalid casts.

How to Fix the Java ClassCastException

Trace the failed cast by inspecting the actual and expected runtime types, defining loaders, and code locations.

Step 1: Find the Failed Cast in the Full Exception

  1. Capture the complete exception, including its Caused by: sections.
  2. Find the relevant ClassCastException, rather than stopping at an outer framework exception.
  3. Read its first at frame, then follow the call chain to the code that supplied or cast the value.

The top frame normally identifies the execution point where the exception was created. Start there even when it belongs to a library; it can reveal the precise boundary your application crossed, as defined by Java’s StackTraceElement documentation.

A failing cast is not always visible as (SomeType) in your source. The compiler also inserts casts when values come out of generics. For example, an incorrectly populated collection may fail when you retrieve an element:

Order order = orders.get(0);

If unchecked code put a Customer into that collection, the retrieval can throw even though the line contains no explicit cast.

Also distinguish stack-frame formatting from package names:

com.foo.loader//com.foo.bar.App.run(App.java:12)

Here, com.foo.loader is the loader name, the empty component between the slashes represents an unnamed module, and com.foo.bar.App is the class name.

A frame can omit both loader and module information:

MyClass.mash(MyClass.java:9)

That is a normal form for application-classpath code. The absence of a printed loader name does not mean the class was loaded without a loader.

Step 2: Inspect the Actual Object and Expected Type

  1. Pause immediately before the failing operation.
  2. Inspect the runtime class of the value, not just its declared variable type.
  3. Compare the actual and expected types’ defining loaders and code locations.

This helper prints those details. It uses module APIs available in Java 9 and later:

public final class TypeProbe {
    public static void inspect(Object value, Class<?> expected) {
        if (value == null) {
            System.out.println("value=null; a reference cast would succeed");
            return;
        }
        Class<?> actual = value.getClass();
        describe("actual", actual);
        describe("expected", expected);
        System.out.println(
                "assignable=" + expected.isInstance(value));
        System.out.println(
                "same defining loader="
                + (actual.getClassLoader() == expected.getClassLoader()));
    }
    private static void describe(String label, Class<?> type) {
        ClassLoader loader = type.getClassLoader();
        java.security.CodeSource source =
                type.getProtectionDomain().getCodeSource();
        System.out.println(label + "=" + type.getName());
        System.out.println("  module=" + type.getModule());
        System.out.println(
                "  loader=" + (loader == null ? "bootstrap" : loader));
        System.out.println(
                "  source="
                + (source == null ? "unavailable" : source.getLocation()));
    }
}

Call it with the value being cast and the destination type, replacing these example names with yours:

TypeProbe.inspect(value, Order.class);

Read the output as follows:

  • assignable=false means that non-null object cannot be cast to the expected type.
  • Matching loaders do not make unrelated classes compatible.
  • Different loaders do not automatically indicate a bug. A plugin implementation can legitimately implement an interface defined by a parent loader.
  • The code location helps identify an unexpected dependency, stale deployment, or duplicate API JAR. Some generated or runtime-provided classes have no available code location.

For a suspected plugin conflict, inspect the implemented interface as well as the concrete class. The crucial question is whether the implementation uses the same runtime interface definition that the caller expects.

Step 3: Correct the Object or API Contract

If the actual and expected types are genuinely different, fix the data or API boundary before touching class loaders.

  1. Trace where the value was created: a constructor, repository, deserializer, cache, factory, or test fixture.
  2. Make that producer return the intended type, or change the receiving code to use the type the producer actually promises.
  3. Remove the invalid cast, rebuild the affected code, and rerun the failing operation.

A cast does not transform an object. This code cannot turn a customer into an order:

Object value = new Customer();
Order order = (Order) value;

If the operation requires an Order, obtain or construct an Order. If it requires behavior shared by several classes, use an interface those classes actually implement. If you need to transfer data between unrelated models, write a conversion or use an appropriate mapper.

Deserialization is a common source of this mistake. A JSON object deserialized without the intended target type may become a map rather than your POJO. Configure the deserializer to produce the required model instead of casting the map afterward.

For generic collections, correct unchecked assignments and raw-type usage at the producer. Adding another cast at the point of failure only moves the problem.

Use an instanceof check when multiple runtime types are valid inputs. If the wrong type represents a broken contract, fail explicitly rather than silently skipping the operation.

Step 4: Resolve Duplicate Types and Loader Boundaries

When identical type names represent separate runtime definitions, fix dependency visibility or loader delegation, not the package spelling.

A runtime class is identified by its binary name together with its defining loader. The loader association is exposed through Class.getClassLoader(), as documented in Java’s ClassLoader API.

  1. Inspect the dependencies used by the failing process. Include test dependencies if the failure occurs only in tests. For Maven:
    mvn dependency:tree

    For a Gradle project using the Java plugin and a Unix-style wrapper:

    ./gradlew dependencies --configuration runtimeClasspath

    For that Gradle project’s test runtime:

    ./gradlew dependencies --configuration testRuntimeClasspath
  2. Compare the dependency report with the diagnostic code locations. A clean build graph does not rule out extra JARs in a deployed application, plugin directory, container image, or application server.
  3. Correct the ownership of shared types. For plugins, put the shared interface in a loader visible to both host and plugin, and prevent the plugin from defining a private copy of that interface. For dependency conflicts, align compatible versions and remove unintended duplicate artifacts.
  4. Check for custom system-loader configuration. The startup property java.system.class.loader can select a custom system loader instead of the default implementation. Inspect it in the failing JVM:
    System.out.println(
            System.getProperty("java.system.class.loader"));
    System.out.println(ClassLoader.getSystemClassLoader());

    A null property value means no custom system loader was selected through that property. Frameworks can still create additional loaders.

  5. Rebuild the affected artifacts, stop the old process, and launch the corrected deployment. Then repeat the exact operation that failed.

Configure the ordinary application classpath in the launch or build configuration. Changing java.class.path after startup does not make the existing system loader load additional JARs. Applications that intentionally support dynamic plugins need an explicit loading mechanism, not a runtime property edit.

Special Cases in Libraries and Frameworks

Library, reload, test, and plugin failures can be repaired by removing loader assumptions, recreating affected objects, and using the intended implementation.

AppClassLoader Cannot Be Cast to URLClassLoader

After an upgrade, an older application may fail when casting the system loader to URLClassLoader:

jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to
java.net.URLClassLoader

Older code may contain an assumption like this:

java.net.URLClassLoader loader =
        (java.net.URLClassLoader) ClassLoader.getSystemClassLoader();

The built-in application loader is not a URLClassLoader on modern JDKs. This is an implementation-assumption failure, not evidence that your application needs a named module.

If the cast occurs in a dependency, upgrade that dependency to a release compatible with your JDK, rebuild the application, and rerun the operation.

If you own the code, replace the cast according to its purpose:

  • To locate a resource, use the ClassLoader resource APIs.
  • To inspect the configured application classpath, read the classpath property, but do not treat it as an inventory of every module or custom-loader resource.
  • To load plugin JARs, create and manage a dedicated loader rather than assuming the system loader exposes URL-based behavior.

After replacing the assumption, test the resource lookup or plugin-loading behavior on the JDK used in deployment. Merely making the cast conditional can leave the original function unimplemented.

Hot Reloading and Cached Objects

Reloading can leave an old object alive after a new loader defines the replacement version of its class. This can produce the confusing result that a class cannot be cast to another class with exactly the same name.

The retained object may live in a static field, session, cache, or long-lived service outside the reloaded application’s lifecycle.

To repair this:

  1. Compare the cached object’s defining loader with the currently expected class.
  2. Remove cross-reload references, or keep the shared contract outside the reloadable portion of the application.
  3. Recreate affected cache entries under the current loader when their data can safely be regenerated.
  4. Restart the application and test both initial startup and another reload.

Do not indiscriminately delete persistent cache data. If the cache stores serialized objects, verify serialization and schema compatibility separately; that is not the same issue as retaining an in-memory object from an old loader.

Test Fixtures and Plugin Implementations

A test can instantiate a base class even though production creates a subclass implementing the required interface. In that situation, the cast fails because the test supplied the wrong object, regardless of the module suffix.

Check the actual instance before changing mocking or loader settings. Configure the test to create the intended implementation, rebuild the test code, and rerun the failing test.

If the runtime object appears correct but its interface is loaded separately, inspect the test runner’s loader isolation and dependency packaging. Keep the shared API visible through a common loader, then rerun the test through the same runner that originally failed.

When It Really Is a Module Access Problem

An unnamed-module reference can also appear in an access error. The distinction is the exception and its wording:

  • ClassCastException concerns runtime type compatibility.
  • InaccessibleObjectException commonly concerns blocked reflective access.
  • IllegalAccessError can concern inaccessible classes or members, including module-access restrictions.

Opening a package does not make unrelated types cast-compatible.

For reflective-access failures, first update the library attempting the reflection. If an update is unavailable, a narrowly scoped JVM option may be appropriate for the exact package named in the exception.

For example, if the error specifically says that java.base does not open java.lang to an unnamed module, this launch command grants that reflective access to classpath code:

java --add-opens java.base/java.lang=ALL-UNNAMED -jar app.jar

Replace app.jar with your executable JAR. This option broadens reflective access for all unnamed modules, so use it only when that access is understood and required, not as a generic cure for loader messages.

Launch the actual failing process with the option and repeat the reflective operation. A flag added to an unrelated shell, IDE process, or build daemon will not repair a separate application JVM.

Confirm the Fix and Prevent Regressions

A successful startup alone does not confirm a cast fix. Exercise the path that previously produced the incompatible value.

Use this checklist:

  • The original request, test, plugin action, or cache lookup completes successfully.
  • The returned object satisfies the intended type contract.
  • Any shared plugin interface resolves to the intended runtime definition.
  • The packaged application contains the dependencies you inspected during diagnosis.
  • The failure stays fixed after a full restart and, when relevant, another reload.
  • Temporary diagnostics and unnecessary JVM workarounds have been removed.

Add a regression test at the boundary that supplied the wrong value. For deserialization, assert the produced model type; for plugin loading, verify the implementation is assignable to the shared interface; for a test fixture, verify the configured implementation is instantiated.

Run that test against the packaged application or equivalent runtime configuration before deploying the fix.

Frequently Asked Questions

Why Is io.jdevops.restful.pojo.job Named in the Exception?

The name io.jdevops.restful.pojo.job identifies a type involved in the failed operation; it does not by itself indicate a problem with the type’s package or module declaration. Read both sides of cannot be cast to to determine whether job is the actual object type or the expected destination type. Then check the repository, deserializer, or factory that produced the value and make its return type match the receiving code’s contract.

Why Does This Happen With jakarta.transaction.Transaction?

A transaction object from another API is not necessarily a jakarta.transaction.Transaction, even if both types have “Transaction” in their names. Check the imports and the transaction manager’s declared return type; javax.transaction.Transaction and jakarta.transaction.Transaction are also distinct types, not interchangeable names. Align the application’s transaction API and compatible dependencies, remove the unsupported cast, and rerun the transactional operation.

What Should I Check for a Playwright ClassCastException?

Check the actual Playwright object and the interface your code expects before changing module settings. Browser, BrowserContext, and Page represent different API roles, so a cast is not a substitute for calling the method that returns the required object. Correct the API call, check for inconsistent Playwright dependencies if the runtime types still look wrong, and rerun the failing browser action.