{"record":{"id":"80ceb53c9a35fc9a","repo":"apache/cordova-android","slug":"failed-to-find-java-home-environment-variable-t","errorCode":null,"errorMessage":"Failed to find 'JAVA_HOME' environment variable. Try setting it manually.","messagePattern":"Failed to find 'JAVA_HOME' environment variable\\. Try setting it manually\\.","errorType":"validation","errorClass":"CordovaError","httpStatus":null,"severity":"error","filePath":"lib/env/java.js","lineNumber":97,"sourceCode":"        const javaHome = environment.CORDOVA_JAVA_HOME || environment.JAVA_HOME;\n        if (javaHome) {\n            // Ensure that CORDOVA_JAVA_HOME overrides\n            environment.JAVA_HOME = javaHome;\n            // Ensure that the JAVA_HOME bin path is before anything else\n            // to cover cases where different Java versions is in the PATH\n            environment.PATH = path.join(environment.JAVA_HOME, 'bin') + path.delimiter + environment.PATH;\n        } else {\n            const javacPath = utils.forgivingWhichSync('javac');\n            if (javacPath) {\n                // OS X has a command for finding JAVA_HOME.\n                const find_java = '/usr/libexec/java_home';\n                const default_java_error_msg = 'Failed to find \\'JAVA_HOME\\' environment variable. Try setting it manually.';\n                if (fs.existsSync(find_java)) {\n                    try {\n                        environment.JAVA_HOME = (await execa(find_java)).stdout;\n                    } catch (ex) {\n                        events.emit('verbose', ex.shortMessage);\n                        throw new CordovaError(default_java_error_msg);\n                    }\n                } else {\n                    // See if we can derive it from javac's location.\n                    const maybeJavaHome = path.dirname(path.dirname(javacPath));\n                    if (fs.existsSync(path.join(maybeJavaHome, 'bin', 'java')) ||\n                            fs.existsSync(path.join(maybeJavaHome, 'bin', 'java.exe'))) {\n                        environment.JAVA_HOME = maybeJavaHome;\n                    } else {\n                        throw new CordovaError(default_java_error_msg);\n                    }\n                }\n            } else if (utils.isWindows()) {\n                const baseDirs = [environment.ProgramFiles, environment['ProgramFiles(x86)']];\n                const globOpts = { absolute: true, onlyDirectories: true };\n                const flatMap = (arr, f) => [].concat(...arr.map(f));\n                const jdkDir = flatMap(baseDirs, cwd => {\n                    return glob.sync('java/jdk*', { cwd, ...globOpts });\n                }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/apache/cordova-android/blob/7c1e190064e349ffa4bbc6ac37b77cd773e4dbd3/lib/env/java.js#L79-L115","documentation":"Branch of java env detection: JAVA_HOME unset, javac found on PATH, and on macOS the helper /usr/libexec/java_home exists. The code execs it to discover JAVA_HOME; if that command exits non-zero (no JVM registered with macOS) it throws the generic 'Failed to find JAVA_HOME' message.","triggerScenarios":"`cordova requirements android` / build on macOS where javac is reachable (shim, manual install) but /usr/libexec/java_home reports 'Unable to find any JVM in this Home' - i.e. no JDK registered via the standard macOS mechanism.","commonSituations":"JDK removed by deleting files instead of uninstalling; a JDK unpacked manually to /opt without being registered; Homebrew-installed javac without the Cask JDK; macOS updates clearing old JVM registrations.","solutions":["Install a proper JDK on macOS (e.g. `brew install --cask temurin@8` or the Oracle installer) so /usr/libexec/java_home can find it.","Or bypass detection: export JAVA_HOME manually to the JDK root.","Diagnose registrations with `/usr/libexec/java_home -V`."],"exampleFix":"# before\n$ /usr/libexec/java_home\nUnable to find any JVM in this Home.\n\n# after\n$ brew install --cask temurin@8\n$ /usr/libexec/java_home   # prints the JDK root","handlingStrategy":"validation","validationCode":"if (!process.env.JAVA_HOME && process.platform === 'darwin' && fs.existsSync('/usr/libexec/java_home')) {\n  const probe = await execa('/usr/libexec/java_home').catch(() => null);\n  if (!probe || !probe.stdout) {\n    throw new Error('No JVM registered with macOS - install a JDK (brew install --cask temurin@8) or export JAVA_HOME');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await check_reqs.check_java();\n} catch (e) {\n  if (/Failed to find 'JAVA_HOME'/.test(e.message) && process.platform === 'darwin') {\n    // run `/usr/libexec/java_home -V` and either register a JDK or export JAVA_HOME manually\n  }\n  throw e;\n}","preventionTips":["Install JDKs via proper installers/casks so macOS registers them with /usr/libexec/java_home.","Never delete a JDK directory by hand; uninstall it so registrations stay consistent.","Set JAVA_HOME explicitly in CI instead of relying on macOS inference."],"tags":["java","jdk","java-home","macos","environment-variable"],"backgroundTag":"java-home-not-set","analyzedSha":"7c1e190064e349ffa4bbc6ac37b77cd773e4dbd3","analyzedAt":"2026-08-22T04:57:58.868Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}