参考

0. 准备阶段

  1. #!/bin/bash
  2. javaPath=/usr/local/
  3. tempPath=/tmp/testFilePath/
  4. read -p "请输入下载的jdk名字(全名):" jdkName
  5. echo "你输入的数是:$jdkName"
  6. mkdir $tempPath
  7. cp $jdkName $tempPath
  8. cd $tempPath
  9. tar -zxvf $jdkName
  10. dirName="java"
  11. rm -rf $jdkName
  12. tempName=`ls`
  13. mv $tempName "java"
  14. fileName=`ls`
  15. mv $fileName $javaPath
  16. cd ~
  17. rm -rf $tempPath
  18. echo "export JAVA_HOME=$javaPath$dirName">>/etc/profile
  19. echo "export CLASSPATH=.:\$JAVA_HOME/jre/lib/rt.jar:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar">>/etc/profile
  20. echo "export PATH=\$JAVA_HOME/bin:\$PATH">>/etc/profile
  21. #重新加载profile文件
  22. source /etc/profile

2. 运行 sh

一句命令: **chmod -R 777 java.sh && source java.sh**

2.1 给与java.sh文件和tmp文件夹读写权限

  1. chmod -R 777 java.sh
  2. chmod -R 777 /tmp
  3. -R 是指级联应用到目录里的所有子目录和文件
  4. 777 是所有用户都拥有最高权限

2.2 运行 java.sh

  • 把脚本和下载的jdk放在同一个目录中
    1. ./java.sh
    2. or
    3. source java.sh

2.3 如果配置没有生效手动使配置生效

  1. source /etc/profile

3. 环境说明

  • linux ```shell LSB Version: :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description: CentOS Linux release 8.2.2004 (Core) Release: 8.2.2004 Codename: Core
  1. [jdk-8u281-linux-x64.tar.zip](https://www.yuque.com/attachments/yuque/0/2021/zip/1642320/1628238530718-a80d7f7f-07ac-4321-b3de-e5828455862e.zip?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2021%2Fzip%2F1642320%2F1628238530718-a80d7f7f-07ac-4321-b3de-e5828455862e.zip%22%2C%22name%22%3A%22jdk-8u281-linux-x64.tar.zip%22%2C%22size%22%3A143047365%2C%22type%22%3A%22%22%2C%22ext%22%3A%22zip%22%2C%22source%22%3A%22transfer%22%2C%22status%22%3A%22done%22%2C%22mode%22%3A%22title%22%2C%22download%22%3Atrue%2C%22id%22%3A%22AU7Y7%22%2C%22card%22%3A%22file%22%7D)
  2. <a name="KOtgw"></a>
  3. # [shell 问题总汇](https://www.yuque.com/tanning/epv4c9/vqsvn8?view=doc_embed)
  4. <a name="TyZhH"></a>
  5. # 备注
  6. <a name="o3pzs"></a>
  7. ## 单步走
  8. ```shell
  9. # 解压
  10. tar -zxvf jdk-8u351-linux-x64.tar.gz
  11. # 重命名解压文件
  12. mv jdk1.8.0_351 java
  13. # 移动到 usr/local下
  14. sudo mv java /usr/local/java
  15. # 编辑环境
  16. sudo vim /etc/profile
  17. export JAVA_HOME=/usr/local/java
  18. export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  19. export PATH=$PATH:$JAVA_HOME/bin
  20. # 让环境生效
  21. source /etc/profile
  22. # 测试
  23. java -version
  24. jps
  25. # 把root 用户加入 vmware 组 (我使用vmware用户处理的所以root用户想使用的加入
  26. usermod -G vmware root