apache/dubbo · error · IllegalStateException

The java code not endsWith "}", code: {}

Error message

The java code not endsWith "}", code: 
{}

What it means

Thrown by AbstractCompiler.compile when the class was not found via Class.forName and the source code does not end with '}'. This indicates the source string is truncated or malformed; a complete Java compilation unit must end with the closing brace of the class body.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/compiler/support/AbstractCompiler.java:67

        matcher = CLASS_PATTERN.matcher(code);
        String cls;
        if (matcher.find()) {
            cls = matcher.group(1);
        } else {
            throw new IllegalArgumentException("No such class name in " + code);
        }
        String className = pkg != null && pkg.length() > 0 ? pkg + "." + cls : cls;
        Lock lock = CLASS_IN_CREATION_MAP.get(className);
        if (lock == null) {
            CLASS_IN_CREATION_MAP.putIfAbsent(className, new ReentrantLock());
            lock = CLASS_IN_CREATION_MAP.get(className);
        }
        try {
            lock.lock();
            return Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException e) {
            if (!code.endsWith("}")) {
                throw new IllegalStateException("The java code not endsWith \"}\", code: \n" + code + "\n");
            }
            try {
                return doCompile(neighbor, classLoader, className, code);
            } catch (RuntimeException t) {
                throw t;
            } catch (Throwable t) {
                throw new IllegalStateException("Failed to compile class, cause: " + t.getMessage() + ", class: "
                        + className + ", code: \n" + code + "\n, stack: " + ClassUtils.toString(t));
            }
        } finally {
            lock.unlock();
        }
    }

    protected Class<?> doCompile(ClassLoader classLoader, String name, String source) throws Throwable {
        return null;
    }

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Ensure the source code string ends with '}' after trimming.
  2. Inspect the code generator that assembles the source and confirm it appends the class-closing brace.
  3. Log the full code string to see exactly where it was truncated.
  4. Increase any buffer/length cap that may be cutting the source short.

Example fix

// before
StringBuilder sb = new StringBuilder();
sb.append("public class Foo {"); /* forgot closing brace */
// after
sb.append("public class Foo {");
sb.append("}");
Defensive patterns

Strategy: validation

Validate before calling

String code = sourceCode.trim();
if (!code.endsWith("}")) {
    throw new IllegalStateException("source is truncated; does not end with '}'");
}
compiler.compile(neighbor, code, classLoader);

Type guard

static boolean isCompleteSource(String code) {
    return code != null && code.trim().endsWith("}");
}

Try / catch

try {
    compiler.compile(neighbor, code, classLoader);
} catch (IllegalStateException e) {
    // source truncated; regenerate with closing brace
}

Prevention

When it happens

Trigger: After Class.forName fails, the code checks code.endsWith("}"); if false, it throws immediately rather than attempting to compile an incomplete source. Happens when the generated or supplied source string was cut off.

Common situations: A StringBuilder-based code generator that builds the source incrementently forgot the final append of '}'. A size/buffer limit truncated the string. Copy-paste of a code template missed the closing brace.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/d365d3a6cb260912. Report an issue: GitHub.