Auto-run LeetCode Java solutions locally without modifying Solution.java
npm install leetcode-local-runnerSolution.java file.
Solution.java file.
Main.java file.
Solution class into a Main class to avoid classpath issues.
ListNode and TreeNode.
bash
npm install -g leetcode-local-runner
`
Alternatively, you can install it as a development dependency in your project:
`bash
npm install --save-dev leetcode-local-runner
`
---
Usage
1. Open your terminal or command prompt.
2. Navigate to the directory containing your LeetCode problem folders (each with a Solution.java).
3. Run the following command:
`bash
leetcode-watch
`
4. The tool will start watching for file modifications. Every time you save a Solution.java file, it will automatically:
- Generate a new Main.java in the same directory.
- Compile and run Main.java to show you the output.
Now you can focus on writing your solution and see the results instantly!
---
$3
The official VS Code LeetCode extension, with over 1.2 million downloads, is excellent for:
- Browsing problems
- Writing solutions
- Submitting to LeetCode
But it does not provide a stable local Java runner without:
- Editing Solution.java
- Writing custom main() methods
- Fighting classpath / package issues
LeetCode Local Runner solves exactly that, while staying 100% compatible with the extension.
---
Required VS Code Configuration
To ensure zero conflicts between the extension and this runner, add the following to your settings.json:
`json
{
"leetcode.hint.configWebviewMarkdown": false,
"editor.inlineSuggest.edits.allowCodeShifting": "never",
"leetcode.hint.commentDescription": false,
"leetcode.hint.commandShortcut": false,
"leetcode.filePath": {
"default": {
"folder": "${id}.${name}",
"filename": "Solution.${ext}"
}
}
}
`
$3
| Setting | Reason |
| :--- | :--- |
| Solution.java filename | Required for automatic detection |
| Stable folder naming | Ensures watcher works correctly |
| Disable inline hints | Prevents file rewrite / cursor shifts |
| No markdown hints | Avoids unintended code injection |
With this configuration, both tools coexist perfectly.
---
How It Works
The script works by parsing your Solution.java file to understand its structure. It then generates a complete, runnable Main.java file that wraps your code.
For example, if your Solution.java is:
`java
// Solution.java
class Solution {
public int[] twoSum(int[] nums, int target) {
// Your code here...
}
}
`
The tool will generate a Main.java file like this:
`java
// Main.java (Auto-generated)
import java.util.*;
public class Main {
// Helper classes like ListNode or TreeNode are automatically injected here if needed.
static class Solution {
// Your entire Solution.java code is placed here.
public int[] twoSum(int[] nums, int target) {
// Your code here...
}
}
public static void main(String[] args) {
Solution sol = new Solution();
// Dummy data is used to call your method.
// (Note: The dummy data is hardcoded in the script for now)
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] result = sol.twoSum(nums, target);
System.out.println(Arrays.toString(result));
}
}
`
This design means you don't have to worry about managing classpaths or writing a main` method just for testing.