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,

user1508893
  • 9,223
  • 14
  • 45
  • 57

7 Answers7

17

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.

vikas kapdoskar
  • 271
  • 2
  • 4
8

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

cd "$JAVA_HOME"
Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • What about `ls "$JAVA_HOME"`? And what's the new error message when it's not working? – Anton Kovalenko Jan 28 '13 at 17:19
  • 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. – Anton Kovalenko Jan 28 '13 at 17:22
  • 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. – user1508893 Jan 28 '13 at 18:06
  • 1
    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). – Anton Kovalenko Jan 28 '13 at 18:10
  • 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 Sep 20 '16 at 09:24
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
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • 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!. – Mingtao Sun Apr 17 '20 at 01:32
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"
demongolem
  • 9,474
  • 36
  • 90
  • 105
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:"
Tim Child
  • 2,994
  • 1
  • 26
  • 25
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

TikiTavi
  • 242
  • 1
  • 3
  • 9
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
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
Mondie
  • 1