如何通过 Snapcraft 在 Ubuntu 中安装和设置 OpenJDK?

如何通过 Snapcraft 在 Ubuntu 中安装和设置 OpenJDK?

如何从 snap 安装 OpenJDK 以便可以从 CLI 使用 javac 和 java?

nicholas@mordor:~$ 
nicholas@mordor:~$ snap list
Name                       Version           Rev    Tracking         Publisher      Notes
bare                       1.0               5      latest/stable    canonical✓     base
chromium                   113.0.5672.126    2477   latest/stable    canonical✓     -
core18                     20230503          2751   latest/stable    canonical✓     base
core20                     20230503          1891   latest/stable    canonical✓     base
core22                     20230531          750    latest/stable    canonical✓     base
cups                       2.4.2-5           872    latest/stable    openprinting✓  -
firefox                    111.0.1-2         2517   latest/stable/…  mozilla✓       -
gnome-3-38-2004            0+git.6f39565     140    latest/stable    canonical✓     -
gnome-42-2204              0+git.587e965     102    latest/stable/…  canonical✓     -
gtk-common-themes          0.1-81-g442e511   1535   latest/stable/…  canonical✓     -
openjdk                    20.0.1+9          1349   latest/stable    jgneff         -
snap-store                 41.3-71-g709398e  959    latest/stable/…  canonical✓     -
snapd                      2.59.4            19361  latest/stable    canonical✓     snapd
snapd-desktop-integration  0.9               83     latest/stable/…  canonical✓     -
nicholas@mordor:~$ 
nicholas@mordor:~$ openjdk.java -version
openjdk version "20.0.1" 2023-04-18
OpenJDK Runtime Environment (build 20.0.1+9-snap)
OpenJDK 64-Bit Server VM (build 20.0.1+9-snap, mixed mode, sharing)
nicholas@mordor:~$ 
nicholas@mordor:~$ java --version
Command 'java' not found, but can be installed with:
sudo apt install openjdk-17-jre-headless  # version 17.0.7+7~us1-0ubuntu1~23.04, or
sudo apt install default-jre              # version 2:1.17-74
sudo apt install openjdk-11-jre-headless  # version 11.0.19+7~us1-0ubuntu1~23.04
sudo apt install openjdk-20-jre-headless  # version 20.0.1+9~us1-0ubuntu1~23.04
sudo apt install openjdk-8-jre-headless   # version 8u372-ga~us1-0ubuntu1~23.04
sudo apt install openjdk-18-jre-headless  # version 18.0.2+9-2ubuntu1
sudo apt install openjdk-19-jre-headless  # version 19.0.2+7-0ubuntu4
sudo apt install openjdk-21-jre-headless  # version 21~14ea~us1-0ubuntu1
nicholas@mordor:~$ 
nicholas@mordor:~$ javac --version
Command 'javac' not found, but can be installed with:
sudo apt install openjdk-17-jdk-headless  # version 17.0.7+7~us1-0ubuntu1~23.04, or
sudo apt install default-jdk              # version 2:1.17-74
sudo apt install openjdk-11-jdk-headless  # version 11.0.19+7~us1-0ubuntu1~23.04
sudo apt install openjdk-20-jdk-headless  # version 20.0.1+9~us1-0ubuntu1~23.04
sudo apt install openjdk-8-jdk-headless   # version 8u372-ga~us1-0ubuntu1~23.04
sudo apt install ecj                      # version 3.32.0+eclipse4.26-2
sudo apt install openjdk-18-jdk-headless  # version 18.0.2+9-2ubuntu1
sudo apt install openjdk-19-jdk-headless  # version 19.0.2+7-0ubuntu4
sudo apt install openjdk-21-jdk-headless  # version 21~14ea~us1-0ubuntu1
nicholas@mordor:~$ 
nicholas@mordor:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 23.04
Release:    23.04
Codename:   lunar
nicholas@mordor:~$ 

也可以看看:

https://stackoverflow.com/q/67469925/4531180

https://snapcraft.io/install/openjdk/ubuntu

答案1

您的系统中没有 snap 二进制文件PATH,请尝试使用完整的 snap 命令运行java和命令,如和,您还可以创建符号链接,如:javacopenjdk.javaopenjdk.javac

sudo ln -s /snap/bin/openjdk.java /usr/bin/java
sudo ln -s /snap/bin/openjdk.javac /usr/bin/javac

最好的办法还是使用 Java 的环境管理器,SDKMAN例如jEnv

相关内容