Skip to content

jvms switch clobbers JAVA_HOME and fails with cryptic "symlink ... : The system cannot find the path specified" when java_home is unset #71

Description

@sebyku

Summary

When java_home is empty in the config (e.g. jvms init was never run), jvms switch <ver> produces a misleading error and destructively overwrites the machine JAVA_HOME to an empty string before failing.

Steps to reproduce

  1. Fresh setup, place jvms.exe, run jvms install 25 (do not run jvms init).
  2. In an elevated shell: jvms switch 25.

Actual result

Switch jdk failed, symlink C:\Program Files\jvms\store\25 : The system cannot find the path specified.

The version is correctly installed (jvms list shows it; store\25 contains a valid JDK). The empty gap between store\25 and : is the missing link target. Before this error, setx JAVA_HOME "" /M has already run, wiping any previously-set machine JAVA_HOME.

Root cause (internal/cmdCli/switch.go)

config.JavaHome is "". switchFunc then:

  • runs setx JAVA_HOME "" /M (treated as success), then
  • calls os.Symlink(filepath.Join(config.Store, v), config.JavaHome) with an empty newname, which Windows rejects.

There is no guard that config.JavaHome is non-empty. Confirmed via %APPDATA%\jvms\jvms.json: "java_home": "".

Suggested fix

  • Early in switchFunc (and init), if config.JavaHome == "", return a clear error: "JAVA_HOME location not configured — run jvms init [--java_home <path>] first."
  • Do not run setx JAVA_HOME ... /M until the target is validated, so a failed switch can't clear an existing JAVA_HOME.

Environment

  • jvms v2.1.14, default new branch index
  • Windows 11 Pro (10.0.26200), x64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions