private void watch() {
			try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
				this.watchService = watchService;
				WatchKey watchKey = scriptsDirectory.register(watchService,
						StandardWatchEventKinds.ENTRY_CREATE,
						StandardWatchEventKinds.ENTRY_MODIFY,
						StandardWatchEventKinds.ENTRY_DELETE);
				WatchKey key;
				while ((key = watchService.take()) != null) {
					for (WatchEvent<?> event : key.pollEvents()) {
						Path eventPath = scriptsDirectory.resolve((Path) event.context());
						WatchEvent.Kind<?> kind = event.kind();
						if (Files.isRegularFile(eventPath)) {
							try {
								// We are only interested in 'ENTRY_MODIFY' events since that is when file content is written.
								// A script file created via 'ENTRY_CREATE' will always be empty, so reading from it at
								// that point would be useless.
								if (kind == StandardWatchEventKinds.ENTRY_MODIFY) {
									Set<ScriptFile> scriptsCopy = new HashSet<>(scriptFiles);
									Optional<ScriptFile> matchingScript = scriptsCopy.stream()
											.filter(script -> script.path().equals(eventPath))
											.findFirst();
									if (matchingScript.isPresent()) {
										scriptsCopy.remove(matchingScript.get());
										onScriptUpdated(eventPath);
									} else {
										onScriptCreate(eventPath);
									}
								}
							} catch (Throwable t) {
								logger.error("Unhandled exception updating available scripts", t);
							}
						} else if (kind == StandardWatchEventKinds.ENTRY_DELETE) {
							onScriptRemoved(eventPath);
						}
					}
					if (!key.reset())
						logger.warn("Key was unregistered: {}", key);
				}
				watchKey.cancel();
				logger.info("Stopped watching script directory for updates");
			} catch (IOException ex) {
				logger.error("IO exception when handling file watch on scripts directory", ex);
			} catch (InterruptedException ex) {
				logger.error("File watch on scripts directory was interrupted", ex);
			} catch (ClosedWatchServiceException ignored) {
				// expected when watch service is closed
			}
		}
Wrapper to manage threaded watch service on the script directory.
recaf-core/src/main/java/software/coley/recaf/services/script/ScriptManager.java