# Install on Windows

The Windows service runs on CrushMFT's own service launcher, the one CrushMFT 27 and CrushTunnel use: `-i` writes `service\CrushBalanceService.exe` (the launcher) and `service\CrushBalanceService.conf`, and the launcher runs `CrushBalance.jar -d` as the service **"CrushBalance Server"**. The jar picks the engine from `prefs.XML`.

1. Create an install folder that **only administrators can write to**, for example `C:\Program Files\CrushBalance` (a folder, not the root of a drive), and copy `CrushBalance.jar` into it. Install Java where only administrators can write too (the Java installers' default under `C:\Program Files` is such a folder).
2. Open a Command Prompt in the install folder and run
   ```
   java -jar CrushBalance.jar -i [heapMB]
   ```
   `heapMB` is the Java heap in MB (128–65536, default 1024). `-i` must be run from the folder that contains the jar. The launcher asks for administrator rights itself, with one UAC prompt. `-i` writes the `service\` files itself, so in a folder that needs elevation to write (such as one under `C:\Program Files`) run it from an elevated Command Prompt ("Run as administrator"). The service always uses the install folder as its prefs directory; `-Dcrushbalance.prefs` does not reach it.
- **What `-i` does:**
  - checks the folder before it writes anything (the refusals below);
  - checks that ordinary users cannot write to the folder, `service\` or the jar (the service runs as SYSTEM and runs that jar), and warns when they can read the folder;
  - writes `service\CrushBalanceService.exe` and `service\CrushBalanceService.conf` (UTF-8 `key=value` lines). An existing conf is kept: `-i` sets only `service_name`, `display_name`, `working_dir`, `jar` and, when you pass a heap or the key is missing, `memory`; your other edits stay. `memory` also takes a percentage of the machine's RAM (`memory=60%`). Restart the service to apply a change made by hand;
  - installs and starts the service through the launcher (`install start`); a running service is updated and restarted (`install restart`);
  - waits until the service has been running for 3 seconds (two checks of its state at least 3 s apart, within 30 s of the start), and then reports `Service "CrushBalance Server" installed and running (heap …).` (or `… updated and restarted.`);
  - on a failure, prints the reason and then the last 20 lines of `service\wrapper.log`, with exit code 4. A service that stops again right after its start (for example because a CrushBalance started by hand in a console holds the folder) is a failure too: `the service "CrushBalance Server" was not running for 3 s in a row within 30 s after the start (state 4).`, or `the service "CrushBalance Server" is not running 30 s after the start (state …).`
- **Administrator approval.** When the UAC prompt is declined, or cannot be shown (no desktop, for example over SSH), `-i` stops with exit code 4: `Administrator approval was declined, or it could not be asked (no desktop, for example in an SSH session). Run this command from an elevated Command Prompt ("Run as administrator").`
- The **setup token** is in `service\wrapper.log` (with the admin URL and the certificate fingerprint) and in `setup-token.txt` in the install folder. Continue with [First-run setup](First-Run-Setup.md).
- **Refusals before anything is written** (exit code 4):
  - not started from the jar's folder: `Run -i from the folder that contains CrushBalance.jar`;
  - the root of a drive or share: `Install CrushBalance into a folder, not the root of a drive (E:\).` (the `icacls` fix below would rewrite the whole drive);
  - a `service\` folder this prompt cannot write: `… cannot be written from this Command Prompt. Run this command from an elevated Command Prompt ("Run as administrator").`;
  - the folder check below.
- **If the folder check refuses:** `-i` stops with exit code 4: `CrushBalance: <dir> can be written by Everyone, Authenticated Users or Users, so any local user could make the service run their code as SYSTEM.`, the offending entries, `To remove that access, run from an elevated Command Prompt:` and the fix, two commands to run on the folder, then `A folder under C:\Program Files is protected already.`:
  ```
  icacls "<dir>" /inheritance:d
  icacls "<dir>" /remove:g *S-1-1-0 *S-1-5-11 *S-1-5-32-545 /T
  ```
  (the first stops inheriting rights from the parent folder, the second removes the rights of Everyone, Authenticated Users and Users on the folder and everything in it).
  - To install anyway, run `java -Dcrushbalance.service.allow_writable_dir=true -jar CrushBalance.jar -i`; the refusal becomes a warning (`CrushBalance: WARN: …; installing anyway because -Dcrushbalance.service.allow_writable_dir=true.`). Do this only when nobody else can log on to the host.
- **A warning when ordinary users can read the folder.** The install goes on, with `CrushBalance: WARN: <dir> can be read by Everyone, Authenticated Users or Users, and prefs.XML there, with the registration code and the admin password verifier, inherits that access.`, the entries, `To remove that access, run from an elevated Command Prompt:` and the same two `icacls` commands. Run them to keep `prefs.XML` private ([Hardening: What is sensitive](Hardening.md#what-is-sensitive)).
- **Which Java the service runs** is the launcher's choice, not the Java that ran `-i`; keep that Java where only administrators can write ([Hardening: The Windows service](Hardening.md#the-windows-service)).
- **One service per host.** Run `-i` and `-r` in the folder the service was installed from.
- **Run the other commands from an elevated prompt too** (`-set-admin-password`, `-check-config`, `-r`), in the install folder. In a protected folder a non-elevated prompt can neither write `prefs.XML` nor read `crushbalance.key`, whose access list admits only its owner, SYSTEM and Administrators (`-check-config` then reports KEY_UNUSABLE).
- **Start and stop:** `net start "CrushBalance Server"`, `net stop "CrushBalance Server"`, or the Services console. The launcher stops CrushBalance by closing its standard input; it then drains open connections for at most 3 seconds and ends after about 4.
- **Change the heap:** run `-i heapMB` again in the install folder, or edit `memory=` in `service\CrushBalanceService.conf` and restart the service.
- **Remove the service** with `java -jar CrushBalance.jar -r` in the install folder. The launcher uninstalls the service (one UAC prompt); `-r` then deletes the launcher and the conf. `service\wrapper.log` is kept.
- The web UI never installs or removes the service; System › Windows service only shows the state and the commands.
