Skip to content

Commit 1f69837

Browse files
Merge pull request #2019 from codeflash-ai/cf-1090-java-documentation-gaps
docs: fix Java documentation gaps across 5 pages
2 parents 4392694 + 71fc73e commit 1f69837

5 files changed

Lines changed: 92 additions & 23 deletions

File tree

docs/codeflash-concepts/how-codeflash-works.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ title: "How Codeflash Works"
33
description: "Understand Codeflash's generate-and-verify approach to code optimization and correctness verification"
44
icon: "gear"
55
sidebarTitle: "How It Works"
6-
keywords: ["architecture", "verification", "correctness", "testing", "optimization", "LLM", "benchmarking", "javascript", "typescript", "python"]
6+
keywords: ["architecture", "verification", "correctness", "testing", "optimization", "LLM", "benchmarking", "javascript", "typescript", "python", "java"]
77
---
88
# How Codeflash Works
99

1010
Codeflash follows a "generate and verify" approach to optimize code. It uses LLMs to generate optimizations, then it rigorously verifies if those optimizations are indeed
1111
faster and if they have the same behavior. The basic unit of optimization is a function—Codeflash tries to speed up the function, and tries to ensure that it still behaves the same way. This way if you merge the optimized code, it simply runs faster without breaking any functionality.
1212

13-
Codeflash supports **Python**, **JavaScript**, and **TypeScript** projects.
13+
Codeflash supports **Python**, **JavaScript**, **TypeScript**, and **Java** projects.
1414

1515
## Analysis of your code
1616

1717
Codeflash scans your codebase to identify all available functions. It locates existing unit tests in your projects and maps which functions they test. When optimizing a function, Codeflash runs these discovered tests to verify nothing has broken.
1818

19-
For Python, code analysis uses `libcst` and `jedi`. For JavaScript/TypeScript, it uses `tree-sitter` for AST parsing.
19+
For Python, code analysis uses `libcst` and `jedi`. For JavaScript/TypeScript and Java, it uses `tree-sitter` for AST parsing.
2020

2121
#### What kind of functions can Codeflash optimize?
2222

@@ -25,7 +25,7 @@ Codeflash supports optimizing async functions in all supported languages.
2525

2626
#### Test Discovery
2727

28-
Codeflash discovers tests that directly call the target function in their test body. For Python, it finds pytest and unittest tests. For JavaScript/TypeScript, it finds Jest and Vitest test files.
28+
Codeflash discovers tests that directly call the target function in their test body. For Python, it finds pytest and unittest tests. For JavaScript/TypeScript, it finds Jest and Vitest test files. For Java, it finds JUnit 5, JUnit 4, and TestNG test classes.
2929

3030
To discover tests that indirectly call the function, you can use the Codeflash Tracer. The Tracer analyzes your test suite and identifies all tests that eventually call a function.
3131

@@ -54,12 +54,12 @@ We recommend manually reviewing the optimized code since there might be importan
5454

5555
Codeflash generates two types of tests:
5656

57-
- **LLM Generated tests** - Codeflash uses LLMs to create several regression test cases that cover typical function usage, edge cases, and large-scale inputs to verify both correctness and performance. This works for Python, JavaScript, and TypeScript.
57+
- **LLM Generated tests** - Codeflash uses LLMs to create several regression test cases that cover typical function usage, edge cases, and large-scale inputs to verify both correctness and performance. This works for Python, JavaScript, TypeScript, and Java.
5858
- **Concolic coverage tests** - Codeflash uses state-of-the-art concolic testing with an SMT Solver (a theorem prover) to explore execution paths and generate function arguments. This aims to maximize code coverage for the function being optimized. Currently, this feature only supports Python (pytest).
5959

6060
## Code Execution
6161

62-
Codeflash runs tests for the target function on your machine. For Python, it uses pytest or unittest. For JavaScript/TypeScript, it uses Jest or Vitest. Running on your machine ensures access to your environment and dependencies, and provides accurate performance measurements since runtime varies by system.
62+
Codeflash runs tests for the target function on your machine. For Python, it uses pytest or unittest. For JavaScript/TypeScript, it uses Jest or Vitest. For Java, it uses Maven Surefire or Gradle's test task. Running on your machine ensures access to your environment and dependencies, and provides accurate performance measurements since runtime varies by system.
6363

6464
#### Performance benchmarking
6565

docs/getting-started/java-installation.mdx

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,72 @@ uv tool install codeflash
4747
```
4848

4949
</Step>
50-
<Step title="Initialize your project">
50+
<Step title="Authenticate with Codeflash">
51+
52+
Codeflash uses cloud-hosted AI models. You need to authenticate before running any commands.
53+
54+
**Option A: Browser login (recommended)**
55+
56+
```bash
57+
codeflash auth login
58+
```
59+
60+
This opens your browser to sign in with your GitHub account. Your API key is saved automatically to your shell profile.
61+
62+
If you're on a remote server without a browser, a URL will be displayed that you can open on any device.
63+
64+
**Option B: API key**
65+
66+
1. Visit the [Codeflash Web App](https://app.codeflash.ai/) and sign up with your GitHub account (free tier available)
67+
2. Navigate to the [API Key](https://app.codeflash.ai/app/apikeys) page to generate your key
68+
3. Set it as an environment variable:
69+
70+
```bash
71+
export CODEFLASH_API_KEY="your-api-key-here"
72+
```
73+
74+
Add this to your shell profile (`~/.bashrc`, `~/.zshrc`) so it persists across sessions.
75+
76+
<Info>
77+
If you skip this step, `codeflash init` will prompt you to authenticate interactively.
78+
</Info>
79+
80+
</Step>
81+
<Step title="Initialize your project (recommended)">
5182

5283
Navigate to your Java project root (where `pom.xml` or `build.gradle` is) and run:
5384

5485
```bash
5586
codeflash init
5687
```
5788

58-
This will:
59-
- Detect your build tool (Maven/Gradle)
60-
- Find your source and test directories
61-
- Write Codeflash configuration to your `pom.xml` properties (Maven) or `gradle.properties` (Gradle)
89+
The init command will:
90+
1. **Auto-detect your project** — find your build tool, source root (e.g., `src/main/java`), test root (e.g., `src/test/java`), and test framework
91+
2. **Confirm settings** — show the detected values and ask if you want to change anything
92+
3. **Configure formatter** — let you set up a code formatter (e.g., Spotless, google-java-format)
93+
4. **Install GitHub App** — offer to set up the [Codeflash GitHub App](https://github.com/apps/codeflash-ai/installations/select_target) for automatic PR creation (see next step)
94+
5. **Install GitHub Actions** — offer to add a CI workflow for automated optimization on PRs
95+
96+
Only non-default settings are written to your `pom.xml` properties (Maven) or `gradle.properties` (Gradle). For standard layouts, no config changes are needed.
97+
98+
<Info>
99+
**Can I skip init?** Yes. For standard Maven/Gradle projects, Codeflash auto-detects your project structure from `pom.xml` or `build.gradle` at runtime. If you're already authenticated and your project uses a standard layout (`src/main/java`, `src/test/java`), you can skip straight to optimizing.
100+
101+
Init is recommended because it also sets up the GitHub App and Actions workflow, and lets you override paths for non-standard project layouts (e.g., multi-module projects where source is under `client/src/`).
102+
</Info>
103+
104+
</Step>
105+
<Step title="Install the Codeflash GitHub App (recommended)">
106+
107+
To have Codeflash create pull requests with optimizations automatically, install the GitHub App:
108+
109+
[Install Codeflash GitHub App](https://github.com/apps/codeflash-ai/installations/select_target)
110+
111+
Select the repositories you want Codeflash to optimize. This allows the codeflash-ai bot to open PRs with optimization suggestions in your repository.
112+
113+
<Info>
114+
If you prefer to try Codeflash locally first, you can skip this step and use the `--no-pr` flag to apply optimizations directly to your local files (see next step).
115+
</Info>
62116

63117
</Step>
64118
<Step title="Run your first optimization">
@@ -69,6 +123,12 @@ Optimize a specific function:
69123
codeflash --file src/main/java/com/example/Utils.java --function myMethod
70124
```
71125

126+
If you installed the GitHub App, Codeflash will create a pull request with the optimization. If you haven't installed the app yet, or prefer to review changes locally first, add `--no-pr`:
127+
128+
```bash
129+
codeflash --file src/main/java/com/example/Utils.java --function myMethod --no-pr
130+
```
131+
72132
Or optimize all functions in your project:
73133

74134
```bash
@@ -80,7 +140,7 @@ Codeflash will:
80140
2. Generate tests and optimization candidates using AI
81141
3. Verify correctness by running tests (JUnit 5, JUnit 4, or TestNG)
82142
4. Benchmark performance improvements
83-
5. Create a pull request with the optimization (if the GitHub App is installed)
143+
5. Create a pull request with the optimization (or apply locally with `--no-pr`)
84144

85145
For advanced workflow tracing (profiling a running Java program), see [Trace & Optimize](/optimizing-with-codeflash/trace-and-optimize).
86146

docs/optimizing-with-codeflash/codeflash-all.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Optimize Your Entire Codebase"
33
description: "Automatically optimize all codepaths in your project with Codeflash's comprehensive analysis"
44
icon: "database"
55
sidebarTitle: "Optimize Entire Codebase"
6-
keywords: ["codebase optimization", "all functions", "batch optimization", "github app", "checkpoint", "recovery", "javascript", "typescript", "python"]
6+
keywords: ["codebase optimization", "all functions", "batch optimization", "github app", "checkpoint", "recovery", "javascript", "typescript", "python", "java"]
77
---
88

99
# Optimize your entire codebase
@@ -45,6 +45,11 @@ codeflash --all path/to/dir
4545
codeflash optimize --trace-only --vitest ; codeflash --all
4646
```
4747
</Tab>
48+
<Tab title="Java">
49+
```bash
50+
codeflash optimize --timeout 60 java -cp target/classes com.example.Main ; codeflash --all
51+
```
52+
</Tab>
4853
</Tabs>
4954

5055
This runs your test suite, traces all the code covered by your tests, ensuring higher correctness guarantees

docs/optimizing-with-codeflash/one-function.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,7 @@ codeflash --file path/to/your/file.ts --function ClassName.methodName
9393
```bash
9494
codeflash --file src/main/java/com/example/Utils.java --function methodName
9595
```
96+
97+
In Java, use just the method name — no `ClassName.` prefix is needed. Codeflash discovers the method by name within the specified file.
9698
</Tab>
9799
</Tabs>

docs/optimizing-with-codeflash/trace-and-optimize.mdx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ codeflash optimize --language javascript script.js
6060
To trace and optimize a running Java program, replace your `java` command with `codeflash optimize java`:
6161

6262
```bash
63-
# JAR application
64-
codeflash optimize java -jar target/my-app.jar --app-args
65-
66-
# Class with classpath
63+
# Class with classpath (recommended — works with any compiled project)
6764
codeflash optimize java -cp target/classes com.example.Main
6865

66+
# Executable JAR (requires maven-jar-plugin or equivalent with Main-Class manifest)
67+
codeflash optimize java -jar target/my-app.jar --app-args
68+
6969
# Maven exec
7070
codeflash optimize mvn exec:java -Dexec.mainClass="com.example.Main"
7171
```
7272

7373
For long-running programs (servers, benchmarks), use `--timeout` to limit each tracing stage:
7474

7575
```bash
76-
codeflash optimize --timeout 30 java -jar target/my-app.jar
76+
codeflash optimize --timeout 30 java -cp target/classes com.example.Main
7777
```
7878
</Tab>
7979
</Tabs>
@@ -228,21 +228,23 @@ The Java tracer uses a **two-stage approach**: JFR (Java Flight Recorder) for ac
228228
Replace your `java` command with `codeflash optimize java`:
229229

230230
```bash
231-
# JAR application
232-
codeflash optimize java -jar target/my-app.jar --app-args
233-
234-
# Class with classpath
231+
# Class with classpath (recommended — works with any compiled project)
235232
codeflash optimize java -cp target/classes com.example.Main
233+
234+
# Executable JAR (requires maven-jar-plugin or equivalent with Main-Class manifest)
235+
codeflash optimize java -jar target/my-app.jar --app-args
236236
```
237237

238+
The `-cp` approach works with any project after `mvn compile` or `gradle build`. The `-jar` approach requires your project to produce an executable JAR with a `Main-Class` entry in the manifest — this is not the default Maven behavior.
239+
238240
Codeflash will run your program twice (once for profiling, once for argument capture), generate JUnit replay tests, then optimize the most impactful functions.
239241

240242
2. **Long-running programs**
241243

242244
For servers, benchmarks, or programs that don't terminate on their own, use `--timeout` to limit each tracing stage:
243245

244246
```bash
245-
codeflash optimize --timeout 30 java -jar target/my-benchmark.jar
247+
codeflash optimize --timeout 30 java -cp target/classes com.example.Main
246248
```
247249

248250
Each stage runs for at most 30 seconds, then the program is terminated and captured data is processed.

0 commit comments

Comments
 (0)