quarkusio/quarkus · error · IllegalStateException

Unable to instantiate weigher class: <className>

Error message

Unable to instantiate weigher class: <className>

What it means

instantiateWeigher() creates the configured weigher via weigherClass.getConstructor().newInstance(). Any failure while loading, constructing, or reflectively instantiating the class (other than the interface check) is wrapped in this IllegalStateException naming the class that could not be instantiated.

Source

Thrown at extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/cache/QuarkusPersistenceUnitCaffeineCacheManager.java:121

        }

        caffeineConfig.setExpireAfterAccess(OptionalLong.of(quarkusConfig.maxIdle().toNanos()));
        return delegate.createCache(cacheName, caffeineConfig);
    }

    @SuppressWarnings("unchecked")
    private <K, V> Weigher<K, V> instantiateWeigher(String className) {
        try {
            Class<?> weigherClass = Thread.currentThread().getContextClassLoader().loadClass(className);
            if (!Weigher.class.isAssignableFrom(weigherClass)) {
                throw new IllegalStateException(
                        "Weigher class '" + className + "' must implement com.github.benmanes.caffeine.cache.Weigher");
            }
            return (Weigher<K, V>) weigherClass.getConstructor().newInstance();
        } catch (IllegalStateException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalStateException("Unable to instantiate weigher class: " + className, e);
        }
    }

    @Override
    public Iterable<String> getCacheNames() {
        return delegate.getCacheNames();
    }

    @Override
    public void destroyCache(String cacheName) {
        throw new UnsupportedOperationException("This should not be used by Hibernate ORM");
    }

    @Override
    public void enableManagement(String cacheName, boolean enabled) {
        throw new UnsupportedOperationException("This should not be used by Hibernate ORM");
    }

View on GitHub (pinned to e1c734241f)

Solutions

  1. Check the class name spelling/package in the config and confirm the class is on the application classpath (and in the native image if running native).
  2. Add a public no-arg constructor to the weigher class.
  3. In native mode, register the weigher class for reflection (e.g. @RegisterForReflection or reflect-config.json).
  4. Debug the wrapped cause (getCause() in the log) to see whether it was load failure vs constructor failure, and fix accordingly.

Example fix

// before
class MyWeigher implements Weigher<Object,Object> {
  MyWeigher(int weightFactor) { ... }
}

// after: public no-arg constructor
class MyWeigher implements Weigher<Object,Object> {
  private final int weightFactor = 1;
  public MyWeigher() { }
  public int weigh(Object k, Object v) { return weightFactor; }
}
Defensive patterns

Strategy: validation

Validate before calling

// Verify the class loads and can be constructed before configuring it
Class<?> c = Class.forName(weigherClassName);
java.lang.reflect.Constructor<?> ctor = c.getDeclaredConstructor();
if (!java.lang.reflect.Modifier.isPublic(ctor.getModifiers())
        || !java.lang.reflect.Modifier.isPublic(c.getModifiers())) {
    throw new IllegalStateException(weigherClassName + " must have a public no-arg constructor");
}

Try / catch

try {
    startPu();
} catch (IllegalStateException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Unable to instantiate weigher class")) {
        log.error("Weigher class missing, not public, or constructor threw; cause: "
            + (e.getCause() == null ? "?" : e.getCause()), e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Configuring a cache weigher class whose class name cannot be loaded (ClassNotFoundException), which has no public no-arg constructor (NoSuchMethodException), whose constructor throws (InvocationTargetException), or whose constructor is not accessible (IllegalAccessException).

Common situations: Typo in the fully-qualified class name; weigher class missing from the packaged artifact or not registered for reflection in native mode; weigher with only parameterized constructors; weigher constructor that performs failing initialization (e.g. reads a missing resource).

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/bd6847b88de3ad9d. Report an issue: GitHub.