Linux安装JDK

最后编辑时间: 2023-03-21

 

在学习Java之前,我们要安装Java的运行环境JDK

1.打开命令行窗口

输入javac,查看java是否安装了,出下下面的表示没安装

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

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

2.安装jdk

输入 sudo apt-get install jdk的版本

如 :

sudo apt install openjdk-11-jdk-headless

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ca-certificates-java java-common libasound2 libasound2-data libgraphite2-3
  libharfbuzz0b liblcms2-2 libpcsclite1 openjdk-11-jre-headless
Suggested packages:
  default-jre libasound2-plugins alsa-utils liblcms2-utils pcscd openjdk-11-demo
  openjdk-11-source libnss-mdns fonts-dejavu-extra fonts-ipafont-gothic
  fonts-ipafont-mincho fonts-wqy-microhei | fonts-wqy-zenhei fonts-indic
The following NEW packages will be installed:
  ca-certificates-java java-common libasound2 libasound2-data libgraphite2-3
  libharfbuzz0b liblcms2-2 libpcsclite1 openjdk-11-jdk-headless openjdk-11-jre-headless
0 upgraded, 10 newly installed, 0 to remove and 31 not upgraded.
Need to get 259 MB of archives.
After this operation, 404 MB of additional disk space will be used.
Do you want to continue? [Y/n] 输入 y

等待安装完成

3.设置环境变量

使用vim打开profile

vim /etc/profile

将下面的添加到profile中

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 #jdk的安装路径
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

修改

export JAVA_HOME=你的jdk的安装路径

你不知道路径时,你看在安装时输入y 之后有 openjdk-amd64,你就将 /....openjdk-and64 填到JAVA_HOME

Setting up openjdk-11-jre-headless:amd64 (11.0.11+9-0ubuntu2~18.04) ...
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin
/java (java) in auto mode
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/jjs to provide /usr/bin/
jjs (jjs) in auto mode
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/keytool to provide /usr/
bin/keytool (keytool) in auto mode
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/rmid to provide /usr/bin
/rmid (rmid) in auto mode

4.输入

sudo source /etc/profile

5.检验环境变量是否设置成功

输入

java

出现

Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
  --add-modules <module>(,<module>)*
        Root modules to resolve in addition to the initial modules, or all modules
        on the module path if <module> is ALL-MODULE-PATH.
  --boot-class-path <path>, -bootclasspath <path>
        Override location of bootstrap class files
  --class-path <path>, -classpath <path>, -cp <path>
        Specify where to find user class files and annotation processors
  -d <directory>               Specify where to place generated class files
  -deprecation
        Output source locations where deprecated APIs are used
  --enable-preview
        Enable preview language features. To be used in conjunction with either -source or --release.
  -encoding <encoding>         Specify character encoding used by source files
  -endorseddirs <dirs>         Override location of endorsed standards path
  -extdirs <dirs>              Override location of installed extensions
  -g                           Generate all debugging info
  -g:{lines,vars,source}       Generate only some debugging info
  -g:none                      Generate no debugging info
  -h <directory>
        Specify where to place generated native header files
  --help, -help, -?            Print this help message
  --help-extra, -X             Print help on extra options
  -implicit:{none,class}
        Specify whether or not to generate class files for implicitly referenced files
  -J<flag>                     Pass <flag> directly to the runtime system
  --limit-modules <module>(,<module>)*
        Limit the universe of observable modules
  --module <module-name>, -m <module-name>
        Compile only the specified module, check timestamps
  --module-path <path>, -p <path>
        Specify where to find application modules
  --module-source-path <module-source-path>
        Specify where to find input source files for multiple modules
  --module-version <version>
        Specify version of modules that are being compiled
  -nowarn                      Generate no warnings
  -parameters
        Generate metadata for reflection on method parameters
  -proc:{none,only}
        Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]
        Names of the annotation processors to run; bypasses default discovery process
  --processor-module-path <path>
        Specify a module path where to find annotation processors
  --processor-path <path>, -processorpath <path>
        Specify where to find annotation processors
  -profile <profile>
        Check that API used is available in the specified profile
  --release <release>
        Compile for a specific VM version. Supported targets: 6, 7, 8, 9, 10, 11
  -s <directory>               Specify where to place generated source files
  -source <release>
        Provide source compatibility with specified release
  --source-path <path>, -sourcepath <path>
        Specify where to find input source files
  --system <jdk>|none          Override location of system modules
  -target <release>            Generate class files for specific VM version
  --upgrade-module-path <path>
        Override location of upgradeable modules
  -verbose                     Output messages about what the compiler is doing
  --version, -version          Version information
  -Werror                      Terminate compilation if warnings occur

表明成功

验证一下是否成功

java -version

相关:https://www.cnblogs.com/ssrs-wanghao/articles/8994856.html

请在下方留下您的评论.加入TG吹水群