{"record":{"id":"4421e7cbe1b3ad52","repo":"gradle/gradle","slug":"unable-to-locate-antlr-class-s","errorCode":null,"errorMessage":"Unable to locate Antlr class [%s]","messagePattern":"Unable to locate Antlr class \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"platforms/jvm/antlr/src/main/java/org/gradle/api/plugins/antlr/internal/antlr2/GrammarDelegate.java","lineNumber":145,"sourceCode":"        return vocabName;\n    }\n\n    private static final Class<?> ANTLR_GRAMMAR_CLASS;\n    private static final Class<?> ANTLR_OPTION_CLASS;\n\n    static {\n        ANTLR_GRAMMAR_CLASS = loadAntlrClass(\"antlr.preprocessor.Grammar\");\n        ANTLR_OPTION_CLASS = loadAntlrClass(\"antlr.preprocessor.Option\");\n    }\n\n    public static final Class<?>[] NO_ARG_SIGNATURE = new Class<?>[0];\n    public static final Object[] NO_ARGS = new Object[0];\n\n    private static Class<?> loadAntlrClass(String className) {\n        try {\n            return Class.forName(className, true, GrammarDelegate.class.getClassLoader());\n        } catch (ClassNotFoundException e) {\n            throw new IllegalStateException(\"Unable to locate Antlr class [\" + className + \"]\", e);\n        }\n    }\n}\n","sourceCodeStart":127,"sourceCodeEnd":149,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/jvm/antlr/src/main/java/org/gradle/api/plugins/antlr/internal/antlr2/GrammarDelegate.java#L127-L149","documentation":"GrammarDelegate's static initializer eagerly loads 'antlr.preprocessor.Grammar' and 'antlr.preprocessor.Option' via Class.forName with GrammarDelegate's classloader. If either class is missing, the initializer throws IllegalStateException(\"Unable to locate Antlr class [...]\") - so this surfaces as an ExceptionInInitializerError wrapping this exception at first use. It simply means ANTLR 2 classes are not on the executing classpath.","triggerScenarios":"An antlr2 grammar (.g with preprocessor options) is processed but the antlr configuration contains no antlr2 artifact (or contains antlr3/4 which do not ship antlr.preprocessor.*).","commonSituations":"Only org.antlr:antlr4 declared while the source set still contains legacy antlr2 grammars; a migration from antlr2 to antlr4 that left stale .g files behind.","solutions":["Add the antlr2 tool: dependencies { antlr 'antlr:antlr:2.7.7' }","Or remove/exclude the stale antlr2 grammar files from the AntlrTask source if the project migrated to ANTLR 3/4","Verify with './gradlew dependencies --configuration antlr' that the antlr2 jar actually resolves"],"exampleFix":"// before: only antlr4, but a legacy .g (antlr2) grammar is still in source\ndependencies { antlr 'org.antlr:antlr4:4.13.2' }\n\n// after: add the antlr2 tool for the legacy grammar (or delete the grammar)\ndependencies {\n    antlr 'antlr:antlr:2.7.7'\n}","handlingStrategy":"validation","validationCode":"// guard legacy antlr2 processing\ntasks.matching { it.name ==~ /generate.*GrammarSource/ }.configureEach {\n    doFirst {\n        def legacyG2 = source.files.findAll { it.name.endsWith('.g') }\n        if (!legacyG2.isEmpty()) {\n            assert configurations.antlr.dependencies.any { it.group == 'antlr' } :\n                \"legacy antlr2 grammars (${legacyG2*.name}) need antlr:antlr:2.7.7\"\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["During antlr2->antlr4 migrations, delete or exclude stale .g files from the source set","Declare the antlr2 dependency in the same commit that adds antlr2 grammars","Remember this throws from a static initializer, so it can surface as ExceptionInInitializerError"],"tags":["antlr2","gradle","classloading","missing-dependency","static-init"],"backgroundTag":"missing-tool-dependency","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}