A command-line tool to compile and run Java exercises instantly, without an IDE, a build system, or any project structure. Point it at a .java file and it handles the rest.
Live site: llerandi.github.io/java-runner
The same pattern - wrap the compile-and-run loop into a single command, add watch mode and clean error output - applies to any compiled language. This project implements it for Java.
Tip
If you want to validate your solutions with automated tests, check out java-kata-judge - a local kata judge that compiles your solution and JUnit tests, runs them, and reports pass/fail.
java-runner wraps javac and java into a single command. It compiles the file into a temporary directory, runs the resulting class, prints the output, shows the execution time, and cleans up after itself. If compilation fails, it prints the errors with the absolute path noise removed so they are easier to read.
| Tool | Purpose | Notes |
|---|---|---|
| Java JDK | Compile and run | Version 11 or higher. Must include javac. |
| Bash | Run runner.sh |
Version 4+. Linux and macOS only. |
inotify-tools |
Watch mode on Linux | Install with sudo apt install inotify-tools |
fswatch |
Watch mode on macOS | Install with brew install fswatch |
Windows users should use
runner.bat. No additional tools are needed for watch mode on Windows, which polls every 2 seconds.
Make sure you have the JDK installed, not just the JRE. The JRE does not include
javac. You can verify withjavac -version.
git clone https://github.com/llerandi/java-runner.git
cd java-runner
chmod +x runner.sh./runner.sh HelloWorld.java # run a specific file
./runner.sh # auto-detect
./runner.sh HelloWorld.java --watch # watch mode
./runner.sh *.java --all # compile all .java in the directory together
./runner.sh Main.java --input in.txt # pipe file into stdin
./runner.sh Main.java --output out.txt
./runner.sh Main.java --classpath lib/junit.jar
./runner.sh --helprunner.bat HelloWorld.java
runner.bat HelloWorld.java --watch
runner.bat Main.java --all
runner.bat Main.java --input in.txt --output out.txt
runner.bat Main.java --classpath lib\junit.jarrunner.ps1 uses FileSystemWatcher for event-based watch mode instead of polling.
.\runner.ps1 HelloWorld.java
.\runner.ps1 HelloWorld.java -Watch
.\runner.ps1 Main.java -All
.\runner.ps1 Main.java -InputFile in.txt -OutputFile out.txt
.\runner.ps1 Main.java -Classpath lib\junit.jar| Flag | Short form (PS1) | Description |
|---|---|---|
--watch |
-Watch |
Recompile and rerun every time the file is saved |
--all |
-All |
Compile all .java files in the same directory together |
--input <file> |
-InputFile <file> |
Pipe a file into stdin when running |
--classpath <path> |
-Classpath <path> |
Append to the compile and run classpath |
--output <file> |
-OutputFile <file> |
Save program output to a file (also prints to terminal) |
When no file is passed as an argument, runner searches in this order:
- Any
.javafile in the current directory. If exactly one is found, it runs it. If more than one is found, it lists them and asks you to specify. - If none is found in the current directory, it falls back to the
examples/folder and applies the same logic.
Successful run:
Compiling FizzBuzz.java...
Compiled successfully
───────────────────────── output ─────────────────────────
1
2
Fizz
4
Buzz
Fizz
7
...
───────────────────────────────────────────────────────────
Finished in 143ms
Compilation error:
Compiling FizzBuzz.java...
Compilation failed:
FizzBuzz.java:6: error: ';' expected
int x = 10
^
1 error
Watch mode recompiles and reruns the file automatically every time you save it. It is useful when working through an exercise and iterating quickly without leaving the terminal.
./runner.sh HelloWorld.java --watchOn Linux it uses inotifywait. On macOS it uses fswatch. On Windows it polls every 2 seconds using a built-in loop in the batch script, so no extra tool is needed.
Press Ctrl+C to stop watch mode.
To run the test suite locally:
# Linux and macOS
chmod +x test.sh
./test.sh
# Windows
test.batThe tests cover successful runs, compilation errors, runtime errors, and missing files. The same suite runs automatically on every push via the CI pipeline.
java-runner/
├── .github/
│ └── workflows/
│ ├── ci.yaml # Test pipeline (Linux, macOS, Windows)
│ └── pages.yaml # GitHub Pages deploy pipeline
├── docs/
│ ├── diagrams/ # draw.io source files
│ ├── img/ # Exported images
│ ├── article.md # Published Medium article
│ ├── index.html # GitHub Pages site
│ ├── robots.txt
│ └── sitemap.xml
├── examples/
│ ├── HelloWorld.java # Basic output
│ ├── FizzBuzz.java # Classic exercise
│ └── ReadInput.java # Reading input from stdin
├── tests/
│ ├── CompileError.java # Fixture: syntax error
│ └── RuntimeError.java # Fixture: runtime exception
├── .gitattributes
├── .gitignore
├── README.md
├── runner.bat # Windows (Command Prompt)
├── runner.ps1 # Windows (PowerShell, event-based watch mode)
├── runner.sh # Linux and macOS
├── test.bat # Test suite for Windows
└── test.sh # Test suite for Linux and macOS
Every push to main or dev, and every pull request targeting main, triggers the CI pipeline defined in .github/workflows/ci.yaml. It runs on Ubuntu, macOS, and Windows in parallel. Each job installs JDK 21 and runs the full test suite.
The examples/ folder contains three files you can use to verify the setup or as a starting point for exercises.
./runner.sh examples/HelloWorld.java
./runner.sh examples/FizzBuzz.java
./runner.sh examples/ReadInput.javaReadInput.java reads from stdin, so the terminal will wait for you to type input before producing output.
- Compile and run a
.javafile with a single command - Clean compilation error output (absolute paths stripped)
- Elapsed time display after each run
- Automatic file detection in current directory and
examples/fallback - Watch mode on Linux (
inotifywait) and macOS (fswatch) - Windows support (
runner.bat) with polling-based watch mode - Cross-platform CI pipeline (Ubuntu, macOS, Windows)
- Runtime exit code capture and display on failure
- Watch mode skips rerun if file has not changed (Windows)
- Local test suite (
test.sh/test.bat) with fixtures for compile errors and runtime errors - CI simplified to run the full test suite on every push
- ANSI color output on Windows
- Auto-detect test added to
test.shandtest.bat - GitHub Pages site with project documentation
- Clear screen between runs in watch mode
-
runner.ps1using PowerShellFileSystemWatcherfor event-based watch mode on Windows
-
--allflag: compile all.javafiles in the same directory together -
--input <file>flag to pipe a file into stdin -
--classpath <path>flag to append jars to the compile and run commands -
--output <file>flag to save program output to a file (also prints to terminal)
-
CONTRIBUTING.md - Issue templates
MIT. Free to use, modify, and share.