Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

妖精的绣舞 提交于 2020-05-10 08:46:09

问题


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.

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 INPUT/OUTPUT:

C:\Users\jaall>javac --version
javac 11.0.1

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!


回答1:


As Biswapriyo suggested, you should use WSLENV.

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

  2. In your case, run setx JAVA_HOME "D:\Program Files\Java\jdk-11.0.1"

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

  1. Then run setx WSLENV "JAVA_HOME/p"

You should see the success message again.

  1. Type 'env' into your WSL bash prompt.

You should see JAVA_HOME correctly set at this point.

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




回答2:


Since I've never been able to share variables between the 2 systems easily, I creayed a simple bash function which can easily retrieve (and define, if asked to) any Windows Environment variable. It also takes care of paths so they get converted from Win32 to Un*x-line.

I added this to /etc/bash.bashrc:

winenv()
{
  if [ "$#" == "0" ] || [ "$1" == "--help" ]
  then
    echo $'\n'Usage:
    echo $'\t'winenv [-d] WINDOWS_ENVIRONEMENT_VARIABLE_NAME
    echo $'\t'-d: Defines environment variable in current shell
    echo $'\t    Note that paths will be translated into un*x-like paths\n'
    return
  fi
  local IFS='$\n'
  local PATH_TO_TRANSLATE=$1
  [ "$1" == "-d" ] && PATH_TO_TRANSLATE=$2
  local VAR=$(cmd.exe /c echo %${PATH_TO_TRANSLATE}% | tr -d '\r')
  local NEW=$(wslpath -u "${VAR}" 2>/dev/null || echo ${VAR})
  echo "${PATH_TO_TRANSLATE} = ${VAR} -> ${NEW}"
  [ "$1" == "-d" ] && export "${PATH_TO_TRANSLATE}=${NEW}"
}

And all I have to do to display one is to call winenv PROGRAMFILES (for example)
Or if I expect to export it, I just have to add a -d argument before the variable name as in winenv -d WINDIR.



来源:https://stackoverflow.com/questions/53365643/windows-subsystem-for-linux-not-recognizing-java-home-environmental-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!