export JAVA_HOME with spaces in Cygwin

21,287

Solution 1

I faced this problem too and I saw many posts but nothing really worked. There is a small trick that I did and things started working.

My JAVA_HOME was set to C:/Program Files/Java/jdk1.7.0_23. The problem was with Program Files directory and I was getting the same error.

In Windows there is a short name created for every directory with a space which is without a space. You can see it by running dir /X command on the command prompt. The Short name for Program Files was PROGRA~1.

In the Windows env variable through My Computer I changed the JAVA_HOME to C:/PROGR~1/Java/jdk1.7.0_23 and in hadoop-env.sh I changed JAVA_HOME to /cygdrv/c/PROGRA~1/Java/jdk1.7.0_23.

It worked fine.

Solution 2

You set JAVA_HOME correctly. Now let's cd correctly too.

cd "$JAVA_HOME"

Solution 3

To avoid using the tedious Windows environment variables, and also use the actual path string copied from Windows explorer, I suggest adding the following to your startup script:

             TMP=`cygpath -sw "C:\Program Files\Java\jdk1.8.0_31"`
export JAVA_HOME=`cygpath -u $TMP`

The first cygpath invocation obtains a short, windows path; the second converts it to unix format, which works fine in cygwin.

This will also now work fine:

$ cd $JAVA_HOME

Solution 4

Try to use short name to avoid a space in a path.

"C:\Program Files" should have short name C:\Progra~1 (you can verify it using DOS dir command or entering it into address bar in file explorer).

Set your JAVA_HOME this way:

export JAVA_HOME="/cygdrive/c/Progra~1/Java/jdk1.7.0_10"

Solution 5

Try using the DOS subst command to take the spaces of the JAVA_HOME path name. Assuming drive J; is not mounted or otherwise used.

In a DOS shell

subst j: "C:/Program Files/Java/jdk1.7.0_45"

J: is now an abbreviation for C:/Program Files/Java/jdk1.7.0_45

You can now cd to J:

now run Cygwin and

export JAVA_HOME="J:"
Share:
21,287
user1508893
Author by

user1508893

Updated on July 09, 2022

Comments

  • user1508893
    user1508893 almost 2 years

    I'm trying to set my JAVA_HOME in Cygwin with this command:

    export JAVA_HOME="/cygdrive/c/Program Files/Java/jdk1.7.0_10"
    

    But when I do cd $JAVA_HOME, I'd get this error:

    $ cd $JAVA_HOME
    -bash: cd: /cygdrive/c/Program: No such file or directory
    

    I tried quoting, and escaping the space (ie., \), but none worked. Any idea what else would?

    Thanks,

  • Anton Kovalenko
    Anton Kovalenko about 11 years
    What about ls "$JAVA_HOME"? And what's the new error message when it's not working?
  • Anton Kovalenko
    Anton Kovalenko about 11 years
    And btw, is it 64-bit system? There is some kind of redirection of Program Files to Program Files (x86) involved, maybe cygwin 32-bit stuff just doesn't see your real Program Files at all.
  • user1508893
    user1508893 about 11 years
    Yes, there is a Program Files (x86), but I do have stuff in Program Files, too (for historic reasons). And the ls command returns the same error the cd command does.
  • Anton Kovalenko
    Anton Kovalenko about 11 years
    You posted your command and error message when you cd without double quotes. Now post the same for cd with double quotes. Also try to check it element by element: does ls "/cygrive/c/Program Files/" show Java subdirectory? What about ls "/cygrive/c/Program Files/Java/"? (The main thing to understand is that we already solved the problem with variable by using double quotes. What remains is a problem with filesystem).
  • sheikhjabootie
    sheikhjabootie almost 10 years
    +1 - Just a note - you have to restart your cygwin session for the modified environment variable to be detected.
  • Dave Richardson
    Dave Richardson over 7 years
    This solution is not much use when the cd command is embedded in a script - for example one that starts tomcat. I don't want to edit tomcat scripts to get it working, a better solution is to use the windows short name although I feel that having to do this highlights a shortcoming in Cygwin more than anything else.
  • Dave Richardson
    Dave Richardson over 7 years
    Note that "C:\Program Files (x86)" is "/cygdrive/c/PROGRA~2"
  • Mingtao Sun
    Mingtao Sun about 4 years
    Great solution! Thanks. I had to switch between Java 8 and Java 11, which is a nightmare on Windows. This post saved my day!.
  • dan.m was user2321368
    dan.m was user2321368 over 3 years
    And if you can't choose your install directory, use cygwin to make a soft-link, where the link's path has no spaces, and set JAVA_HOME to that.