maven-mvnd icon indicating copy to clipboard operation
maven-mvnd copied to clipboard

Problem when running in Cygwin with an existed env variable “JAVA_HOME”.

Open imbennie opened this issue 2 years ago • 3 comments

This problem seems like #156 but has a little difference.

I'm using SDKMAN installed JDK and mvnd. So Here's a env variable JAVA_HOME managed by Cygwin.

~ » echo $JAVA_HOME        

/home/Bennie/.sdkman/candidates/java/current

The problem is that an error output when checkout mvnd version:

~ » mvnd -v                                                                                                                                                                                                                 
mvnd native client 0.7.1-windows-amd64 (97c587c11383a67b5bd0ff8388bd94c694b91c1e)
Terminal: org.jline.terminal.impl.jansi.win.JansiWinSysTerminal
Exception in thread "main" java.lang.RuntimeException: Could not get a real path from path \home\Bennie\.sdkman\candidates\java\current
        at org.mvndaemon.mvnd.client.DaemonParameters.javaHome(DaemonParameters.java:144)
        at org.mvndaemon.mvnd.client.DaemonConnector.connect(DaemonConnector.java:99)
        at org.mvndaemon.mvnd.client.DefaultClient.execute(DefaultClient.java:272)
        at org.mvndaemon.mvnd.client.DefaultClient.main(DefaultClient.java:118)

I'm trying to solve the problem as the below as description.

Configure java.home manually

I'm trying mvnd -v agin after edit java.home in the conf/mvnd.properties with following:

java.home=/home/Bennie/.sdkman/candidates/java/8.0.322-tem

Still got the same error output before.

Unset JAVA_HOME

After read the #156 I find out maybe the @gnodet doesn't have a JAVA_HOME in Cygwin itself which just copied from Windows one.

Thus I unset the JAVA_HOME and ran mvnd -v agin.

~ » unset JAVA_HOME
~ » mvnd -v                                                                                                                     
mvnd native client 0.7.1-windows-amd64 (97c587c11383a67b5bd0ff8388bd94c694b91c1e)
Terminal: org.jline.terminal.impl.jansi.win.JansiWinSysTerminal
Exception in thread "main" java.lang.RuntimeException: Could not get a real path from path \home\Bennie\.sdkman\candidates\java\8.0.322-tem
        at org.mvndaemon.mvnd.client.DaemonParameters.javaHome(DaemonParameters.java:144)
        at org.mvndaemon.mvnd.client.DaemonConnector.connect(DaemonConnector.java:99)
        at org.mvndaemon.mvnd.client.DefaultClient.execute(DefaultClient.java:272)
        at org.mvndaemon.mvnd.client.DefaultClient.main(DefaultClient.java:118)

This time the java.home path point to the configured one. Verified that the unset JAVA_HOME operation is valid. But the problem still does what it does.

Adjust path

I checked out the #156 bug-fix details. Found out the java.home path start with \cygdrive\, So edit java.home in the conf/mvnd.properties again.

Then It's just worked.

java.home=/cygdrive/d/Tools/cygwin/home/Bennie/.sdkman/candidates/java/8.0.322-tem

~ » mvnd -v

mvnd native client 0.7.1-windows-amd64 (97c587c11383a67b5bd0ff8388bd94c694b91c1e)
Terminal: org.jline.terminal.impl.jansi.win.JansiWinSysTerminal
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: D:\Tools\cygwin\home\Bennie\.sdkman\candidates\mvnd\0.7.1\mvn
Java version: 1.8.0_322, vendor: Temurin, runtime: D:\Tools\cygwin\home\Bennie\.sdkman\candidates\java\8.0.322-tem\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Finally

Is there any good ways to solve this. This is may be a bug? due to the java.home property override by $JAVA_HOME.

imbennie avatar Apr 24 '22 09:04 imbennie

My suggestion would be to check whether cygpath is on the PATH or detect cygwin using uname -s. If a path doesn't work, try to run it through cygpath --windows and try again.

Something like using this everywhere:

File path = resolveLocalPath(...)

and then having code like this in resolveLocalPath():

if file.exists() return file;
if (cygwin) { // cygwin
    String mightWork = runOSCommand("cygpath", "--windows", file.toString());
    File f = new File(mightWork);
    if (f.exists()) { return file)
}

uname -s returns CYGWIN_NT-10.0-19042. I suggest to check for String.startsWith("CYGWIN")

digulla avatar May 31 '22 15:05 digulla

Workaround: Run this command to set JAVA_HOME in such a way that mvnd can use it:

export JAVA_HOME="$(cygpath --windows ~/.sdkman/candidates/java/current)"

digulla avatar May 31 '22 16:05 digulla

Just for the records and maybe to help someone else. I had a very similar problem but without the cygwin environment. mvnd always reported some not existing path (old java version) with the same exception as above. After some searching in environment variables and config files, I found the reason: I had an old ~/.m2/mvnd.properties with java.home set to the obsolete path. Removing this file solved the problem.

Maybe it would be nice if mvnd would report where it got the java path from, at least in case of an error.

matinh avatar May 31 '23 06:05 matinh