V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
insert000
V2EX  ›  JetBrains

IDEA+wsl2 环境搭建求助

  •  
  •   insert000 · 11 小时 8 分钟前 · 889 次点击

    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

    20 条回复    2025-12-17 17:31:12 +08:00
    yukunZhan9
        1
    yukunZhan9  
       10 小时 57 分钟前
    你这 idea 是用的 windows 本地的吗?推荐使用 jebrains gateway 连到 wsl 里的 idea 做开发就不会有这个问题了
    insert000
        2
    insert000  
    OP
       10 小时 56 分钟前
    @yukunZhan9 对本地的 idea
    yukunZhan9
        3
    yukunZhan9  
       10 小时 50 分钟前
    insert000
        4
    insert000  
    OP
       10 小时 28 分钟前
    @yukunZhan9 好吧,这样体验太差了
    Mogugugugu
        5
    Mogugugugu  
       10 小时 12 分钟前
    你这样性能很差的,各种奇葩问题,换 gateway,但是 gateway + wsl2 也问题挺多的,内存小的机器不建议搞。

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

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

    使用 gateway 去远程到 wsl2 勉强可用,就是插件有的有 bug 。
    zed1018
        15
    zed1018  
       4 小时 3 分钟前
    我建议是在 ws 里装 idea 直接走 wslg 。唯独就是输入法我没搞好,后来还是用 gateway 了
    insert000
        16
    insert000  
    OP
       3 小时 38 分钟前
    @Mjhhh 你这样可以正常使用 maven 吗?
    Mjhhh
        17
    Mjhhh  
       3 小时 18 分钟前
    @insert000 #16 可以的,你试试在 idea 中双击 ctrl ,输入 mvn --version ,看看用的是 wsl 中的 maven ,还是 idea 的 maven ,我建议是用 wsl 的
    insert000
        18
    insert000  
    OP
       3 小时 15 分钟前
    @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
        19
    Mjhhh  
       3 小时 5 分钟前
    @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
        20
    XR843  
       2 小时 41 分钟前
    虽然是把代码、JDK 和 Maven 都放在了 Ubuntu (WSL2) 里,但 IDEA (运行在 Windows 上)错误地试图用 Windows 的方式(调用 cmd.exe 和 mvn.cmd )去执行位于网络共享路径( UNC 路径 \\wsl.localhost\...)下的程序。这是关键问题所在的。
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   3067 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 12:12 · PVG 20:12 · LAX 04:12 · JFK 07:12
    ♥ Do have faith in what you're doing.