ca.adamgordon
ca.adamgordon

Reputation: 17

Strange CMD Evironment Variable Behavior

So I was trying to set my JAVA_HOME variable for a program and though I was sure I set the right directory the program was printing:

The system cannot find the path specified.

I did some testing and now I'm just baffled. Terminal Output

Other info:

C:\Program Files\Java\jre7\bin>java.exe -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Windows version

OS Name:                   Microsoft Windows 8.1 Pro
OS Version:                6.3.9600 N/A Build 9600

Upvotes: 0

Views: 48

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

The cd command handles paths with spaces but dir requires that you quote the path if it contains spaces.

dir "%JAVA_HOME%"

will do what you expect.

The issue is that cd expects only one path, so even if the path contains a space it's still treated as a single string. dir can handle multiple paths, delimited by spaces, so when %JAVA_HOME% gets expanded you get

dir C:\Program Files\Java\jre7\bin

which contains TWO paths (as seen by the dir command), neither of which exists:

C:\Program
Files\Java\jre7\bin

Upvotes: 2

Related Questions