Set Environment Variables for GUI Apps with launchctl setenv

Give Dock-launched applications an environment variable they can't get from your shell profile.

macOSlaunchctlautomationenvironmentMedium risk
Run launchctl setenv JAVA_HOME /path/to/jdk so every app launched from the Dock or Spotlight sees JAVA_HOME.

01Command

Command
$ launchctl setenv JAVA_HOME /Library/Java/JavaVirtualMachines/jdk-21.jdk/Contents/Home

02What it does

GUI apps are started by launchd, not by your shell, so exports in .zshrc never reach them. launchctl setenv injects the variable into the user's launchd instance, and every app spawned afterwards inherits it — no logout needed. Use launchctl getenv to read it back and unsetenv to remove it; note it resets on reboot, so also add it to a LaunchAgent for a permanent fix.

03Risk

RiskMedium risk
!Warning: Apps already running keep their old environment — quit and relaunch them to pick up the variable.

04Undo

Undo
$ launchctl unsetenv JAVA_HOME

05Sources

  • launchctl - Interact with launchd
    SS64 · technical · 2026-09-17
    Evidence: Documents the launchctl setenv and getenv subcommands that set and read environment variables for all processes managed by the user's launchd.
  • launchctl(1) man page
    manpagez.com · technical · 2026-09-17
    Evidence: Describes 'launchctl setenv name value' which sets an environment variable in launchd so later-spawned jobs inherit it, and unsetenv to remove it.

Last verified: 2026-09-19 · Category: System & Settings

06Related solutions