IDEA+wsl2 环境搭建求助

2025 年 12 月 17 日
 insert000

IDEA 安装在物理机器上,JDK 和 maven 都安装在 WSL2 子系统的 Ubuntu 上。项目也在 Ubuntu 子系统的目录中,配置如截图,运行 IDEA 自带的 MAVEN 插件 clean ,test 等均报错

\\wsl.localhost\Ubuntu-24.04\home\abner\app\apache-maven-3.9.11\bin\mvn.cmd -Didea.version=2025.3 -Dmaven.ext.class.path=E:\soft\JetBrains\IntelliJ IDEA 2025.3\plugins\maven\lib\maven-event-listener.jar -Djansi.passthrough=true -Dstyle.color=always -s \\wsl.localhost\Ubuntu-24.04\home\abner\.m2\settings.xml -Dmaven.repo.local=\\wsl.localhost\Ubuntu-24.04\home\abner\.m2\repository clean -f pom.xml
'\\wsl.localhost\Ubuntu-24.04\home\abner\project\java\hd\urthink-base-admin'
用作为当前目录的以上路径启动了 CMD.EXE 。
UNC 路径不受支持。默认值设为 Windows 目录。
The JAVA_HOME environment variable is not defined correctly, 
this environment variable is needed to run this program. 

图片配置: https://imgur.com/a/daAL0kZ

2185 次点击
所在节点    JetBrains
28 条回复
yukunZhan9
2025 年 12 月 17 日
你这 idea 是用的 windows 本地的吗?推荐使用 jebrains gateway 连到 wsl 里的 idea 做开发就不会有这个问题了
insert000
2025 年 12 月 17 日
@yukunZhan9 对本地的 idea
yukunZhan9
2025 年 12 月 17 日
insert000
2025 年 12 月 17 日
@yukunZhan9 好吧,这样体验太差了
Mogugugugu
2025 年 12 月 17 日
你这样性能很差的,各种奇葩问题,换 gateway,但是 gateway + wsl2 也问题挺多的,内存小的机器不建议搞。

wsl2 的核心玩法都是把整个环境+代码全部放到 wsl2 中,然后在 Windows 中远程连接,像是 VS Code 和 JBd 都是在 wsl2 中单独启动一个 Server,Windows 中的相当于客户端,所有的编译等操作都放在 WSL2 里面的 Server 端进行。

不是劝退你哈,IDEA+WSL2 无论使用哪种解决方案都有不爽的点,之前折腾过,全部放弃了。用 VS Code + WSL2 写 Java 都比 IDEA+WSL2 的方式要舒服,这俩就天生不配。
insert000
2025 年 12 月 17 日
@Mogugugugu 我试了一下,gateway + wsl2 ,总感觉不跟手,有点延迟的。那我还是本地老实的安装吧
shunia
2025 年 12 月 17 日
@Mogugugugu #5 感谢分享。有点没想到 2025 年了 JB 还没有解决这个问题。
holyliao
2025 年 12 月 17 日
我是使用 wslg, idea 都安装在子系统中,凑合能用
Plutooo
2025 年 12 月 17 日
好奇这样的好处是啥
lxqxqxq
2025 年 12 月 17 日
@insert000 #6 VS Code + WSL2 正解
lisxour
2025 年 12 月 17 日
@Plutooo 玩得多 wsl 的都知道,这是纯搞自己而已,很多人都把 wsl 当成是真正的完整独立 linux 了
WashFreshFresh
2025 年 12 月 17 日
idea/jdk/maven/项目都装在了 wslg 里,强行工作了一年,只能说很痛苦,一方面是时不时卡死,一方面是输入法用起来很痛苦,不跟光标,至于为啥这么痛苦还坚持了一年,因为 Windows 编译启服务耗时 200s ,linux 60s 不到,后来用了 JRebel 就只用 Windows 了。
Mjhhh
2025 年 12 月 17 日
同样是 IDEA 2025.1 + wsl2 开发,目前没有很顺畅,性能也很好
orrinex
2025 年 12 月 17 日
我用 windows 下的 idea 打开 wsl2 下的项目,根据官方的说法是没问题的,但实际用下来全是 bug 。

使用 gateway 去远程到 wsl2 勉强可用,就是插件有的有 bug 。
zed1018
2025 年 12 月 17 日
我建议是在 ws 里装 idea 直接走 wslg 。唯独就是输入法我没搞好,后来还是用 gateway 了
insert000
2025 年 12 月 17 日
@Mjhhh 你这样可以正常使用 maven 吗?
Mjhhh
2025 年 12 月 17 日
@insert000 #16 可以的,你试试在 idea 中双击 ctrl ,输入 mvn --version ,看看用的是 wsl 中的 maven ,还是 idea 的 maven ,我建议是用 wsl 的
insert000
2025 年 12 月 17 日
@Mjhhh \\wsl.localhost\Ubuntu-24.04\home\abner\app\apache-maven-3.9.11\bin\mvn.cmd -Didea.version=2025.3 -Dmaven.ext.class.path=E:\soft\JetBrains\IntelliJ IDEA 2025.3\plugins\maven\lib\maven-event-listener.jar -Djansi.passthrough=true -Dstyle.color=always -s \\wsl.localhost\Ubuntu-24.04\home\abner\.m2\settings.xml -Dmaven.repo.local=\\wsl.localhost\Ubuntu-24.04\home\abner\.m2\repository clean -f pom.xml
'\\wsl.localhost\Ubuntu-24.04\home\abner\project\java\hd\urthink-base-admin'
用作为当前目录的以上路径启动了 CMD.EXE 。
UNC 路径不受支持。默认值设为 Windows 目录。
The JAVA_HOME environment variable is not defined correctly,
this environment variable is needed to run this program. 看报错是走的 WSL 的 maven
Mjhhh
2025 年 12 月 17 日
@insert000 #18 这是我执行 mvn --version 打印的信息,给你参考下/bin/sh /usr/share/maven/bin/mvn -Didea.version=2025.2.2 -Dmaven.ext.class.path=/tmp/CKHrIJmaven-event-listener.jar -Djansi.passthrough=true -Dstyle.color=always -s /home/user/.m2/settings.xml -Dmaven.repo.local=/home/user/.m2/repository --version -f pom.xml
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 17.0.9, vendor: GraalVM Community, runtime: /home/user/.sdkman/candidates/java/17.0.9-graalce
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "6.6.87.2-microsoft-standard-wsl2", arch: "amd64", family: "unix"
XR843
2025 年 12 月 17 日
虽然是把代码、JDK 和 Maven 都放在了 Ubuntu (WSL2) 里,但 IDEA (运行在 Windows 上)错误地试图用 Windows 的方式(调用 cmd.exe 和 mvn.cmd )去执行位于网络共享路径( UNC 路径 \\wsl.localhost\...)下的程序。这是关键问题所在的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1179360

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX