11

I'm trying to run Maven on cygwin. I've added maven to the path and my Java home looks like this:

$ echo $JAVA_HOME 
/cygdrive/c/Program Files/Java/jdk1.6.0

Yet when I try to run mvn --version, I get this:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /cygdrive/c/Program Files/Java/jdk1.7.0_01/bin/java

Any idea why it's not working? Java -version works fine, so it's not a problem with java. I've also tried jdk1.6.0 instead.

Running which java gives,

$ which java
/cygdrive/c/windows/system32/java

and java -version gives,

$ java -version
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) Client VM (build 21.1-b02, mixed mode, sharing)
gsgx
  • 12,020
  • 25
  • 98
  • 149
  • 1
    do you need to escape the space in Program Files with a \? I never install anything dev related into Program Files just because of that stupid space. – digitaljoel Dec 15 '11 at 17:57
  • I escaped it but it gives the same error. I also don't like the space in Program Files, but I'd prefer not to change the way I use windows just because of cygwin. – gsgx Dec 15 '11 at 18:04
  • what does `which java` print out? and `java -version`? – jtahlborn Dec 15 '11 at 18:13
  • @jtahlborn I added the outputs to the question. – gsgx Dec 15 '11 at 18:16
  • @gsingh2011 Have you added JAVA_HOME to your Windows environment variables? You may need that. – Cody S Dec 15 '11 at 19:01
  • You can try this in Cygwin environment: `export JAVA_HOME='/cygdrive/c/Program Files/Java/jdk1.6.0'` or `set JAVA_HOME='/cygdrive/c/Program Files/Java/jdk1.6.0'` The quotes are needed since there are spaces in the path. – ee. Dec 16 '11 at 01:33
  • If the problem still persists, can you add the output of `echo $PATH` to your question? Even if `$JAVA_HOME` points to where you want it to, it's useless if it's not in your path. I use Maven with Cygwin daily with JDK 7 and I have no problem. See my [`.bashrc`](https://github.com/kohanyirobert/cygwin/blob/master/.bashrc) and the variables defined in it, maybe it'll help to solve your problem. Although it's strange that Maven won't run with JDK 7 (which is obviously in your path). Your `$PATH` holds the truth I guess. – Kohányi Róbert Dec 22 '11 at 07:18
  • Which Maven version are you trying to run? – khmarbaise Jan 20 '12 at 11:52
  • Did you try Stefan's solution? Run `mvn.bat` instead of `mvn`. It worked for me, though I was trying to run `ant`, not `mvn` – Ruan Mendes Feb 29 '12 at 22:45

5 Answers5

17

If I run mvn.bat instead of mvn, I didn't have this problem.

So, since Cygwin will run the mvn file, I replaced this mvn file with a softlink (ln -s mvn.bat mvn) to the batch file. This way, running mvn will silently run the mvn.bat which handles these problems gracefully.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Stefan De Laet
  • 1,409
  • 11
  • 20
  • I had the same problem, I was trying to run ant from cygwin. The JAVA_HOME path was definitely correct, running ant.bat instead of ant did fix my problem – Ruan Mendes Feb 29 '12 at 22:43
  • I have the same problem with Gradle: I wanted to start a maven build from within Gradle, but when 'mvn' is executed from within the gradle script, JAVA_HOME is suddenly "corrected" to the /cygdrive/-path (instead of the C:\..-path it is upon invocation of gradle), and thus the maven invoker which evidently operates in dos-space can't find java. Anyone knows who changes the JAVA_HOME?! It seems like this is the only env-var which gets this treatment. (It might seem like it has something to do with the 'cmd /c' style invocation of the subprocess). – stolsvik Jul 22 '13 at 11:07
10

Anyone Using Cygwin and Maven, here's exactly what you need:

In your Cygwin bash prompt:

$ vim ~/.bashrc

or:

$ nano ~/.bashrc

(Which ever you fancy...)

Append the following:

alias mvn=mvn.bat

Save and Exit. Then run:

$ source ~/.bashrc

Now you should be able to use the mvn command as you do in cmd prompt.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Timothy Perez
  • 20,154
  • 8
  • 51
  • 39
4

Cygwin uses a dos console to execute Maven builds (mvn.bat).

Set your Java home to the C:\Program Files\Java\jdk1.7.0_01 location.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Steven
  • 3,844
  • 3
  • 32
  • 53
  • actually this is the most correct way to solve that. All cygwin tools undestand windows-style `JAVA_HOME`, but not vice versa. – Sergey Grinev Dec 17 '13 at 17:10
  • It worked for my hard drive but not for flash drive. using mvn.bat worked for both cases, I'm clueless why this should be so! – binit Aug 25 '14 at 08:05
0

I was facing the same problem while running gradle from cygwin tool.

I used to set the Java home including the bin folder:

C:\Program Files\Java\jdk1.7.0_67\bin

But later I realized that some application do not recognize if you include bin folder, so I changed the class path to C:\Program Files\Java\jdk1.7.0_67 and it started working.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Sam
  • 1
0

Setting JAVA_HOME to /cygdrive/c/Progra~1/Java/jdk1.6.0 might help.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95