适用于 Linux 的 Windows 子系统无法识别 JAVA_HOME 环境变量

我试图让 WSL 识别我的 Windows 安装的 JAVA_HOME 环境变量.我附上了我的 bashrc 中的内容和我的 windows 环境变量中的内容以及 cmd 和 bash 的输出.

I'm trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash.

我的 bashrc 末尾是什么:

What's at the end of my bashrc:

export JAVA_HOME="/mnt/d/Program Files/Java/jdk-11.0.1"
export PATH="/mnt/d/Program Files/Java/jdk-11.0.1/bin:$PATH"

CMD 输入/输出:

C:Usersjaall>javac --version
javac 11.0.1

BASH 输入/输出:

BASH INPUT/OUTPUT:

myubuntu_name@DESKTOP-LUK3BII:~$ javac --version

Command 'javac' not found, but can be installed with:

sudo apt install default-jdk
sudo apt install openjdk-11-jdk-headless
sudo apt install ecj
sudo apt install openjdk-8-jdk-headless

我已经被这个问题困扰了一段时间,无法弄清楚或在线找到有效的解决方案.谢谢!

I've been stuck on this for awhile and can't figure it out or find a working solution online. Thanks!

推荐答案

正如 Biswapriyo 所建议的,您应该使用 WSLENV.

As Biswapriyo suggested, you should use WSLENV.

  1. 打开 PowerShell.然后将 JAVA_HOME 设置为您的 java 安装路径.

  1. Open PowerShell. Then set JAVA_HOME to the path to your java installation.

在你的情况下,运行 setx JAVA_HOME "D:Program FilesJavajdk-11.0.1"

您应该会看到一条消息成功:指定的值已保存.

You should see a message that says "SUCCESS: Specified value was saved.

  1. 然后运行 ​​setx WSLENV "JAVA_HOME/p"

您应该会再次看到成功消息.

You should see the success message again.

  1. 在 WSL bash 提示符中输入env".

此时您应该会看到 JAVA_HOME 设置正确.

You should see JAVA_HOME correctly set at this point.

注意:如果第 2 步不起作用,您可能需要更改 JAVA_HOME 的路径以包含in"文件夹.

Note: If step 2 doesn't work, you might want to changing the path to JAVA_HOME to include the 'in' folder.

相关文章