export JAVA_HOME with spaces in Cygwin
Asked Answered
L

7

11

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,

Launalaunce answered 28/1, 2013 at 17:1 Comment(1)
You need to quote the cd command: cd "JAVA_HOME"Principate
C
18

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.

Cabretta answered 25/8, 2013 at 13:43 Comment(2)
+1 - Just a note - you have to restart your cygwin session for the modified environment variable to be detected.Piatt
Note that "C:\Program Files (x86)" is "/cygdrive/c/PROGRA~2"Powwow
L
8

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

cd "$JAVA_HOME"
Lytle answered 28/1, 2013 at 17:10 Comment(5)
What about ls "$JAVA_HOME"? And what's the new error message when it's not working?Lytle
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.Lytle
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.Launalaunce
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).Lytle
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.Powwow
A
6

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
Androus answered 10/2, 2015 at 20:48 Comment(1)
Great solution! Thanks. I had to switch between Java 8 and Java 11, which is a nightmare on Windows. This post saved my day!.Actinouranium
C
3

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"
Cacomistle answered 1/7, 2013 at 20:11 Comment(0)
U
2

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:"
Unskillful answered 20/3, 2014 at 2:1 Comment(0)
E
1

I installed Java outside of "Program Files", specifically in c:\tools. Then you can use cygpath to convert the C:\tools\jdk1.8.0_144 to /cygdrive/c/tools/jdk1.8.0_144

Ekaterinburg answered 9/8, 2017 at 18:51 Comment(1)
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.Inestimable
A
0

on MSYS2 terminal

checke for previous claim: ls "/C/program files/" !works! -thats for long file names so, to the point:

export JAVA_HOME="/D/Devel/jdk-12.0.2"

And now maven works....

check:

user55@DESKTOP MSYS ~
# echo $JAVA_HOME
/D/Devel/jdk-12.0.2
Allsopp answered 13/9, 2019 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.