在Windows 10的WSL2上使用JDK的正确方法是什么?

huangapple go评论108阅读模式
英文:

What is the proper way of using JDK on WSL2 on Windows 10?

问题

我已在WSL上安装了Ubuntu 20.04 LTS。我的Windows 10已经安装了JDK。我是否需要在WSL上的Ubuntu上安装JDK,还是可以在Ubuntu中使用Windows 10的JDK?您如何在WSL上进行Java编程?哪种方式是正确的?

我只是在想,如果我需要在Linux上再次安装所有开发工具和二进制文件,这不会占用很多空间并且占用大量的CPU/内存资源吗?

英文:

I have installed Ubuntu 20.4 LTS on WSL. My windows 10 already have the JDK installed. Do I need to install JDK on ubuntu on WSL or can I use the Windows 10 JDK in the Ubuntu? How you do Java programming on WSL? Which is the proper way?

I was just wondering if I need to install all the development tools and binaries again on Linux won't it take a lot of space & hog a lot of CPU/Ram resources?

答案1

得分: 36

以下是翻译好的内容:

以具有sudo权限或root权限的用户身份运行以下命令,以更新软件包索引并安装OpenJDK 11 JDK软件包:

$ sudo apt update
$ sudo apt install openjdk-11-jdk

安装完成后,可以通过检查Java版本来进行验证:

$ java -version

输出应类似于以下内容:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

设置JAVA_HOME环境变量:
OpenJDK 11位于/usr/lib/jvm/java-11-openjdk-amd64/bin/java

找到首选Java安装路径后,打开/etc/environment文件:

$ sudo nano /etc/environment

假设您希望将JAVA_HOME设置为指向OpenJDK 11,请在文件末尾添加以下行:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

要使更改在当前shell中生效,可以注销并重新登录,或者运行以下源命令:

$ source /etc/environment

验证是否正确设置了JAVA_HOME环境变量:

$ echo $JAVA_HOME

您应该会看到Java安装路径:

/usr/lib/jvm/java-11-openjdk-amd64

供参考,您可以按照下面的链接进行操作:
在Ubuntu 20.04上安装Java的方法

英文:

Run the following commands as a user with sudo privileges or root to update the packages index and install the OpenJDK 11 JDK package:

$ sudo apt update
$ sudo apt install openjdk-11-jdk

Once the installation is complete, you can verify it by checking the Java version:

$ java -version

The output should look something like this:

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

Set JAVA_HOME Environment Variable:
OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java

Once you found the path of your preferred Java installation, open the /etc/environment file:

$ sudo nano /etc/environment

Assuming you want to set JAVA_HOME to point to OpenJDK 11, add the following line, at the end of the file:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

For changes to take effect on your current shell you can either log out and log in or run the following source command:

$ source /etc/environment

Verify that the JAVA_HOME environment variable was correctly set:

$ echo $JAVA_HOME

You should see the path to the Java installation:

/usr/lib/jvm/java-11-openjdk-amd64

for reference you can follow this link below
How to Install Java on Ubuntu 20.04

答案2

得分: 15

我们可以在wsl2中使用Windows JDK。我们应该将这个路径添加到/etc/environment文件中:

JAVA_HOME=/mnt/c/Program Files/Java/jdk-11.0.8/bin/

通过添加这个bin文件夹,我们可以运行常规命令,但需要在命令后加上.exe格式,例如:javac.exe hello.javajava.exe hello.java

如果你不喜欢这种方式,可以像下面这样添加别名:

alias java='java.exe'
alias javac='javac.exe'

我认为我们可以像这样使用Windows中的任何程序 在Windows 10的WSL2上使用JDK的正确方法是什么?

英文:

We can use that Windows JDK inside the wsl2. we should add this to /etc/environment

JAVA_HOME=/mnt/c/Program Files/Java/jdk-11.0.8/bin/

by adding this bin folder we may run regular commands but append with .exe format eg: javac.exe hello.java java.exe hello.java

if you don't like that way then add alias like below:

alias java='java.exe'
alias javac='javac.exe'

I think we can use any of the windows programs like this 在Windows 10的WSL2上使用JDK的正确方法是什么?

答案3

得分: 8

在WSL上安装或使用Java没有一个“适当”的(即由JDK提供者支持推荐的)方法。我找不到任何官方建议。

然而,有可能要么从WSL安装并使用Windows安装的Oracle JDK,要么从Ubuntu软件包管理器中将OpenJDK Java安装到您的WSL环境中。

> 我只是在想,如果我需要在Linux上重新安装所有开发工具和二进制文件,那么它不会占用很多空间并占用大量CPU/内存资源吗?

请参阅上文。但要注意,只有在同时运行两种类型的JVM时才会“占用CPU/内存”。

参考资料:

<sup>(如果上述内容未解决您的问题,关于这个主题还有许多其他文章。)</sup>

英文:

There is not a "proper" (as in supported or recommended by JDK providers) way to install or use Java on WSL. I could not find any official recommendations.

However, it is possible to either install and use Oracle JDK for Windows installation from WSL, or install OpenJDK Java into your WSL world from the Ubuntu package manager.

> I was just wondering if I need to install all the development tools and binaries again on Linux won't it take a lot of space & hog a lot of CPU/Ram resources ?

See above. But note that you are only going to "hog CPU/RAM" if you are running both kinds of JVM at the same time.

References:

<sup>(There are many more articles on this topic if the above don't address your concerns.)</sup>

答案4

得分: 2

在 Windows 11 上,我通过 IntelliJ IDEA 安装了 Java,并希望在 WSL 上重用这个安装。

~/.bashrc 中创建相应的函数,以便可以直接启动这些可执行文件,并导出它们,以便可以从子shell中使用:

java() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-<version>/bin/java.exe "$@"
}
export -f java

javac() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-<version>/bin/javac.exe "$@"
}
export -f javac
英文:

I installed Java via IntelliJ IDEA on Windows 11 and wanted to reuse the installation on WSL.

Create matching functions in ~/.bashrc to start the executables directly and export them so they can be used from subshells:

java() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-&lt;version&gt;/bin/java.exe &quot;$@&quot;
}
export -f java

javac() {
    /mnt/c/Program\ Files/Eclipse\ Adoptium/jdk-&lt;version&gt;/bin/javac.exe &quot;$@&quot;
}
export -f javac

答案5

得分: 2

以下是翻译好的部分:

  1. 因此,我采用了另一种方式,在WSL/Ubuntu上使用命令行安装了JDK。

    sudo apt install default-jdk
    

    或者安装任何JDK版本。

  2. 在安装之前确保更新您的软件包/模块。

    sudo apt-get update
    
  3. 获取安装的JDK路径。

    which java
    

    这里的问题是它给出的位置实际上是一个符号链接。您需要跟随它几次:

  4. 设置JAVA_HOME变量。

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java
    

    检查JAVA_HOME变量的值:

    echo $JAVA_HOME
    
  5. 这还没有结束。您刚刚声明的JAVA_HOME变量是临时的。如果您关闭终端或启动新会话,它将再次为空。

    要 '永久性地' 设置JAVA_HOME变量,您应将其添加到您的主目录中的bashrc文件中。

    备份您的bashrc文件(以防您弄乱它,您可以恢复它):

    cp ~/.bashrc ~/.bashrc.bak
    

    接下来,使用echo命令将您在本节开头使用的export命令附加到文件中。将下面的命令更改为使用您系统中显示的正确路径。

    echo "export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64" >> ~/.bashrc
    

    验证它是否已正确添加到文件末尾:

    tail -3 ~/.bashrc
    

上述tail命令将显示指定文件的最后3行。

英文:

Well I had faced same issue of installing jdk on wsl but I did'nt find any solution for directing putting the path of windows jdk path into wsl.

  1. So I followed an alterantive way, I had installed jdk on wsl/Ubuntu with cmd.

    sudo apt install default-jdk

or install any of jdk version.

  1. Before installing make sure to update ur packages/modules.

    sudo apt-get update

3.Get the path of the isntalled jdk

which java

The problem here is that the location it gives is actually a symbolic link. You’ll have to follow it a couple of times:

  1. Setting JAVA_HOME variable

    export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/bin/java

check the value of JAVA_HOME variable:

echo $JAVA_HOME
  1. This is not over yet. The JAVA_HOME variable you just
    declared is temporary. If you close the terminal or start a
    new session, it will be empty again.

    To set JAVA_HOME variable ‘permanently’, you should add it
    to the bashrc file in your home directory.

    Back up your bashrc file (in case you mess it, you can get
    it back):

cp ~/.bashrc ~/.bashrc.bak
 
 Next, use the echo command to append the export command you used at the beginning of this section. Change the command below to use the correct path as displayed by your system in.

echo &quot;export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64&quot; 
&gt;&gt; ~/.bashrc

Verify that it has been correctly added to the end of the file:

tail -3 ~/.bashrc

The above tail command will show the last 3 lines of the specified file.

huangapple
  • 本文由 发表于 2020年9月13日 11:14:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63866813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定