Registry / data / javalang

javalang

JSON →
library0.13.0pypypi✓ verified 83d ago

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.

pip install javalang
INSTALL
IMPORT
SIG · JAVALANG
J
javalang
datapythonv0.13.0
Install
1.6s avg
Import
20ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.13.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.021s · 18.1MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.020s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from javalang import parse
import javalang

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.

import javalang java_code = """ package com.example; public class MyClass { // Simple field int myField = 10; public static void main(String[] args) { System.out.println("Hello, Javalang!"); } } """ try: tree = javalang.parse.parse(java_code) print(f"Parsed package: {tree.package.name}") print(f"Parsed class: {tree.types[0].name}") # Iterate through nodes to find methods and fields for path, node in tree: if isinstance(node, javalang.tree.MethodDeclaration): print(f" Method found: {node.name}") elif isinstance(node, javalang.tree.FieldDeclaration): print(f" Field found: {node.declarators[0].name}") except javalang.tokenizer.LexerError as e: print(f"Lexer Error: {e}") except javalang.parser.JavaSyntaxError as e: print(f"Syntax Error: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
Debug
Known issues
breakingJavalang 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`.
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.
affects: All versions (0.1.0 - 0.13.0)
gotchaThe `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.
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.
affects: All versions (0.1.0 - 0.13.0)
deprecatedThe `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.
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.
affects: 0.13.0 and older
Upgrade
Version history
0.13.0latest on PyPI · released Mar 28, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
javalang — pip install javalang · libregistry