{"id":8243,"library":"javalang","title":"Javalang: Pure Python Java Parser","description":"Javalang is a pure Python library designed for working with Java source code. It provides a lexer and parser specifically targeting Java 8, with its implementation based on the Java Language Specification. The library's current version is 0.13.0, released on March 28, 2020.","status":"maintenance","version":"0.13.0","language":"en","source_language":"en","source_url":"https://github.com/c2nes/javalang","tags":["Java","parsing","AST","lexer","parser","static analysis"],"install":[{"cmd":"pip install javalang","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"javalang.parse.parse","correct":"import javalang\ntree = javalang.parse.parse(java_code)"},{"symbol":"javalang.tree.CompilationUnit","correct":"from javalang import tree\n# tree is an instance of CompilationUnit"},{"note":"AST nodes like CompilationUnit are in javalang.tree, not javalang.ast directly, though CompilationUnit is a subclass of javalang.ast.Node.","wrong":"import javalang.ast.Node","symbol":"javalang.tree.Node","correct":"from javalang import tree\n# For traversing AST nodes"}],"quickstart":{"code":"import javalang\n\njava_code = \"\"\"\npackage com.example;\n\npublic class MyClass {\n    // Simple field\n    int myField = 10;\n\n    public static void main(String[] args) {\n        System.out.println(\"Hello, Javalang!\");\n    }\n}\n\"\"\"\n\ntry:\n    tree = javalang.parse.parse(java_code)\n    print(f\"Parsed package: {tree.package.name}\")\n    print(f\"Parsed class: {tree.types[0].name}\")\n\n    # Iterate through nodes to find methods and fields\n    for path, node in tree:\n        if isinstance(node, javalang.tree.MethodDeclaration):\n            print(f\"  Method found: {node.name}\")\n        elif isinstance(node, javalang.tree.FieldDeclaration):\n            print(f\"  Field found: {node.declarators[0].name}\")\n\nexcept javalang.tokenizer.LexerError as e:\n    print(f\"Lexer Error: {e}\")\nexcept javalang.parser.JavaSyntaxError as e:\n    print(f\"Syntax Error: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\n","lang":"python","description":"This quickstart demonstrates parsing a complete Java source file using `javalang.parse.parse()` and then traversing the resulting Abstract Syntax Tree (AST) to extract package, class, method, and field names. Error handling for common parsing issues is included."},"warnings":[{"fix":"Ensure the Java source code to be parsed adheres to Java 8 syntax. For newer Java versions, consider alternatives or extensions like `javalang-ext` if they support the desired Java version, though `javalang-ext` also builds on `javalang`'s Java 8 parsing.","message":"Javalang explicitly targets Java 8. Attempting to parse Java code leveraging newer language features (e.g., `var` keyword, text blocks, sealed classes, `switch` expressions from Java 9+) will likely result in `LexerError` or `JavaSyntaxError`.","severity":"breaking","affected_versions":"All versions (0.1.0 - 0.13.0)"},{"fix":"Always wrap code snippets in a minimal but valid Java class and package structure before passing them to `javalang.parse.parse()`. For more flexible snippet parsing, investigate `javalang-ext` or `javalang-plus`, which offer 'syntactic element-deducing' parse functions.","message":"The `javalang.parse.parse()` function strictly requires a 'complete compilation unit' (a full, valid Java source file). It cannot parse isolated code snippets like a single method, statement, or declaration without the enclosing class and package structure.","severity":"gotcha","affected_versions":"All versions (0.1.0 - 0.13.0)"},{"fix":"Users requiring ongoing support, new features, or compatibility with Java versions beyond 8 should evaluate forks, extensions (like `javalang-ext` or `javalang-plus`), or other Java parsing libraries in Python.","message":"The `javalang` library has not seen a new release since March 2020. While still functional for Java 8, active development appears to have ceased, meaning new features, bug fixes, or compatibility with recent Python or Java versions are unlikely.","severity":"deprecated","affected_versions":"0.13.0 and older"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Rewrite the Java code to be compatible with Java 8 syntax, or use a parsing library that supports newer Java versions. If you must use `javalang`, ensure the input is strictly Java 8 compliant.","cause":"Attempting to parse Java code that uses the `var` keyword (introduced in Java 10) or other post-Java 8 language features.","error":"javalang.tokenizer.LexerError: Unknown token: ('var', <position>)"},{"fix":"Restrict Java input to Java 8 syntax. `javalang` does not support newer Java features like sealed classes or records.","cause":"Parsing a Java 9+ feature like 'sealed classes' or 'records' that uses new keywords or syntax not recognized by the Java 8-targeted parser.","error":"javalang.parser.JavaSyntaxError: Unexpected token 'extends'"},{"fix":"Add explicit checks for `None` before accessing attributes of AST nodes, especially when dealing with optional elements in Java syntax (e.g., `tree.package` can be `None` if no package is declared). Ensure the input Java code is syntactically valid and complete for `javalang`. Example: `if tree.package: print(tree.package.name)`.","cause":"This typically occurs when traversing the AST, and an expected node or attribute is `None`. This can happen if the parsing failed partially, or if the AST structure for a specific Java construct was not as anticipated, leading to accessing a non-existent child.","error":"AttributeError: 'NoneType' object has no attribute 'name'"}]}