# java-parser

> Java Parser in JavaScript

Latest version **3.0.1** (published 2025-07-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install java-parser
pnpm add java-parser
yarn add java-parser
bun add java-parser
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 3.0.1 |
| Published | 2025-07-01 |
| First published | 2016-04-23 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 3 |
| Unpacked size | 251 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1214 |
| Maintainers | jdubois, deepu105, pascalgrimaud, cdessoude, jtkiesel |

## Links

- npm: https://www.npmjs.com/package/java-parser
- Repository: https://github.com/jhipster/prettier-java/tree/main/packages/java-parser
- npm.io page: https://npm.io/package/java-parser

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) 4.17.21
- [chevrotain](https://npm.io/package/chevrotain.md) 11.0.3
- [chevrotain-allstar](https://npm.io/package/chevrotain-allstar.md) 0.3.1

## Recent versions

- 3.0.1 (latest) — 2025-07-01
- 3.0.0 — 2025-07-01
- 2.3.4 — 2025-06-04
- 2.3.3 — 2025-01-08
- 2.3.2 — 2024-07-12
- 2.3.1 — 2024-07-12
- 2.3.0 — 2024-02-22
- 2.2.0 — 2023-11-26
- 2.1.0 — 2023-11-12
- 2.0.5 — 2023-08-21
- 2.0.4 — 2023-03-11
- 2.0.3 — 2022-11-26
- 2.0.2 — 2022-05-25
- 2.0.1 — 2022-01-06
- 2.0.0 — 2021-10-16
- … 23 more at https://npm.io/package/java-parser/versions

## README

[![npm](https://img.shields.io/npm/v/java-parser.svg)](https://www.npmjs.com/package/java-parser)

# java-parser

A Java Parser implemented in JavaScript using the [Chevrotain Parsing ToolKit](https://github.com/Chevrotain/chevrotain).
It outputs a **C**oncrete **S**yntax **T**ree, rather than an **A**bstract **S**yntax **T**ree.

- [On the Difference between a CST and an AST](https://stackoverflow.com/questions/1888854/what-is-the-difference-between-an-abstract-syntax-tree-and-a-concrete-syntax-tre)

Currently the main focus of this project is to be used in implementing a prettier Java plugin.
But it could also be used as the basis for other Java related tools in the JavaScript ecosystem.

## Installation

```sh
npm install java-parser --save-dev
```

or

```sh
yarn add java-parser --dev
```

## Usage

### Parsing

```javascript
const { parse } = require("java-parser");
const javaText = `
public class HelloWorldExample{
  public static void main(String args[]){
    System.out.println("Hello World !");
  }
}
`;

const cst = parse(javaText);
// explore the CST
```

### Traversing the CST

See relevant [Chevrotain documentation on CST Traversal](http://chevrotain.io/docs/guide/concrete_syntax_tree.html#traversing).

```javascript
const {
  BaseJavaCstVisitor,
  BaseJavaCstVisitorWithDefaults
} = require("java-parser");

// Use "BaseJavaCstVisitor" if you need to implement all the visitor methods yourself.
class LambdaArrowsPositionCollector extends BaseJavaCstVisitorWithDefaults {
  constructor() {
    super();
    this.customResult = [];
    this.validateVisitor();
  }

  lambdaExpression(ctx) {
    // Collects all the starting offsets of lambda arrows in lambdas with short (no parenthesis)
    // single argument lists: e.g:
    // - n -> n*n (will be collected)
    // - (n) -> n*n (not collected)
    if (ctx.lambdaParameters[0].children.Identifier) {
      this.customResult.push(ctx.Arrow[0].startOffset);
    }
  }
}

const lambdaArrowsCollector = new LambdaArrowsPositionCollector();
// The CST result from the previous code snippet
lambdaArrowsCollector.visit(cst);
lambdaArrowsCollector.customResult.forEach(arrowOffset => {
  console.log(arrowOffset);
});
```

---
_Source: https://npm.io/package/java-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
