10

I've been using Maven on Git Bash (64-bit) for a few months now, and suddenly it stopped working, and is now generating this error on any maven command:

myuser@mypc MINGW64 ~ (master *)
$ mvn -v
Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

I have reviewed many questions on SO, including this one: Maven error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher but have not solved my problem.

I have upgraded to the latest Git for Windows (2.14.2.windows.2) from 2.12 but the problem remains.

My Maven installation is at C:\apache-maven-3.5.0\bin, which is in my PATH variable:

myuser@mypc MINGW64 ~ (master *)
$ echo $PATH
...:/c/jdk1.7.0_79/bin:/c/apache-maven-3.5.0/bin:...

I also have JAVA_HOME set correctly:

myuser@mypc MINGW64 ~ (master *)
$ echo $JAVA_HOME
C:\jdk1.7.0_79

I've tried adding/removing MAVEN_HOME but that doesn't seem to be detected in the Apache Maven Startup Script (C:\apache-maven-3.5.0\bin\mvn) :

myuser@mypc MINGW64 ~ (master *)
$ echo $MAVEN_HOME
c:\apache-maven-3.5.0

If I go into the Apache Maven Startup Script and replace instances of ${MAVEN_HOME} with C:\apache-maven-3.5.0, then it seems to find the Launcher class and executes correctly.

Edits like this:

CLASSWORLDS_JAR=`echo "${MAVEN_HOME}"/boot/plexus-classworlds-*.jar`
  to
CLASSWORLDS_JAR=`echo /c/apache-maven-3.5.0/boot/plexus-classworlds-*.jar`

then produce:

myuser@mypc MINGW64 ~ (master *)
$ mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: C:\apache-maven-3.5.0
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: C:\jdk1.7.0_79\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

This is an inelegant workaround, and would rather find the proper solution. What environment or configuration changes can I try that would allow Maven to run without hardcoding the path?


Steps/results for VonC's answer:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\windows\system32>set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\windows\system32>set GH=C:\Program Files\Git
C:\windows\system32>set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%
C:\windows\system32>set PATH=%JAVA_HOME%\bin;%MAVEN_HOME%\bin;%PATH%

C:\windows\system32>echo %PATH%
C:\jdk1.7.0_79\bin;c:\apache-maven-3.5.0\bin;C:\Program Files\Git\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\

C:\windows\system32>echo %M2_HOME%
C:\apache-maven-3.5.0

C:\windows\system32>mvn -v
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T15:39:06-04:00)
Maven home: c:\apache-maven-3.5.0\bin\..
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: C:\jdk1.7.0_79\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

C:\Windows\System32>bash

myuser@mypc MINGW64 /c/Windows/System32
$ mvn -v
Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

myuser@mypc MINGW64 /c/Windows/System32
$ echo $M2_HOME
C:\apache-maven-3.5.0

S279887@P2025774 MINGW64 /c/Windows/System32
$ echo $MAVEN_HOME
c:\apache-maven-3.5.0

After further debugging, I realized my problem appears to be identical to Maven error in MINGW Git bash: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher, where the MAVEN_HOME variable gets cleared in this section of code:

# For MinGW, ensure paths are in Unix format before anything is touched
if $mingw ; then
  [ -n "$MAVEN_HOME" ] &&
    MAVEN_HOME=`(cd "$MAVEN_HOME"; pwd)`
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME=`(cd "$JAVA_HOME"; pwd)`
  # TODO classpath?
fi

If I run the offending line [[ -n "$MAVEN_HOME" ]] && MAVEN_HOME=`(cd "$MAVEN_HOME"; pwd)` in a bash session, I can reproduce the behavior in the latest Git for Windows:

myuser@mypc MINGW64 ~ (master *)
$ git --version
git version 2.14.2.windows.3

myuser@mypc MINGW64 ~ (master *)
$ echo $MAVEN_HOME
c:\apache-maven-3.5.0

myuser@mypc MINGW64 ~ (master *)
$ [[ -n "$MAVEN_HOME" ]] && MAVEN_HOME=`(cd "$MAVEN_HOME"; pwd)`

myuser@mypc MINGW64 ~ (master *)
$ echo $MAVEN_HOME


myuser@mypc MINGW64 ~ (master *) 
$

This is what I get when I execute the command directly -- it returns the current directory and not MAVEN_HOME as I would expect. Assuming that happens because the command executes in a subshell?

myuser@mypc MINGW64 ~ (master *)
$ echo `(cd "$MAVEN_HOME"; pwd)`
/c/home

myuser@mypc MINGW64 ~ (master *)
$ `(cd "$MAVEN_HOME"; pwd)`
bash: /c/home: Is a directory
mcknz
  • 467
  • 9
  • 27
  • Could you share the JAVA_HOME and MAVEN_HOME coniguration and the git bash version in use in detail as well. – Naman Oct 16 '17 at 17:24
  • @nullpointer thanks -- have incorporated into question, let me know if you need more info. – mcknz Oct 16 '17 at 17:38
  • 1
    Just to tell that I changed `MAVEN_HOME=\`(cd "$MAVEN_HOME" ; pwd)\`` to `MAVEN_HOME=$(cd "$MAVEN_HOME" ;pwd)` and it's now working as I expected. I'm not such a great bash scripter so I don't really now the exact difference between the 2 (`$()` vs `\`()\`` notation) but it does make a difference and this is how I would have done it – MaxouMask May 04 '18 at 15:14

1 Answers1

1

The first thing to test is the %PATH%: in a CMD session, set up a simplified PATH.

set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\
set GH=C:\path\to\git
set PATH=%GH%\bin;%GH%\usr\bin;%GH%\mingw64\bin;%PATH%

Note how the C:\WINDOWS\... paths are kept at the end of the PATH: that is important.

Then prepend the path of your %JAVA_HOME%\bin, and of <maven>\bin.

And make sure to set M2_HOME to C:\apache-maven-3.5.0.

Finally, test your mvn command, both in the current CMD, or (in that same Windows) in a git bash session (typing just "bash")

Finally, depending on your program and your brand of OS, don't forget that with Windows 10 you now have WSL and a full-fledge Linux bash. That can be a possible alternative.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you -- unfortunately I'm stuck on Win7 with this one. I've added my results to the question. Does this mean I need to re-order my PATH? – mcknz Oct 16 '17 at 22:08
  • Yes, set your path exactly as I describe, cutting everything not needed. – VonC Oct 16 '17 at 22:09
  • still doesn't work in bash with limited path -- when I type "bash" in the CMD window my PATH shows as /mingw64/bin:/usr/bin:/c/dotfiles/bin:/c/jdk1.7.0_79/bin:/c/apache-maven-3.5.0/bin:/c/jdk1.7.0_79:/c/apache-maven-3.5.0:/bin:/usr/bin:/mingw64/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell /v1.0 – mcknz Oct 16 '17 at 23:30
  • @mcknz when you type `echo $M2_HOME` what do you see? In your bash session, try: `export M2_HOME=/c/‌​apache-maven-3.5.0` (no trailing `bin/`, as in https://stackoverflow.com/a/18155226/6309) – VonC Oct 17 '17 at 04:27
  • thanks -- added steps/results to the above. Did some additional digging and it looks like I have the same problem as https://stackoverflow.com/questions/45391097/maven-error-in-mingw-git-bash-could-not-find-or-load-main-class-org-codehaus-pl – mcknz Oct 17 '17 at 14:55
  • @mcknz and I suppose the issues persists with the latest https://github.com/git-for-windows/git/releases? (2.14.2(3)) – VonC Oct 17 '17 at 15:27
  • 1
    yes, I just upgraded to 2.14.2.windows.3 and the issue remains. I have added more detail to my question about exactly what I'm experiencing. I might try downgrading Git for Windows to an earlier version to see if that's the issue. Or I might just comment out that section of mvn. :) – mcknz Oct 17 '17 at 16:38