centos7自带openjdk | javac命令报错bash:javac:command not found | yum provide javac

前言:centos7,没有另外手动安装过jdk。检查java -version发现自带了jdk。
但运行javac命令报错:bash:javac:command not found

试了n种方法没能解决!
最后发现自带的jdk是没有安装开发环境的,需要安装devel才有javac命令。

问题分析:自带的jdk默认只安装了运行环境(找到jdk的安装目录下,发现只有jre文件夹,没有bin、lib等文件夹)。

自查方法

(1)先找到jdk的安装目录

[root@localhost home]# which java
/usr/bin/java
[root@localhost home]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 Apr 18 13:09 /usr/bin/java -> /etc/alternatives/java
[root@localhost home]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Apr 18 13:09 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64/jre/bin/java

这就是jdk安装的文件夹:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64

(2)打开这个文件夹,检查它的子文件夹。如果只有jre一个文件夹,那就是需要安装devel开发环境。

[root@localhost home]# cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
[root@localhost java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64]# ll
total 0
drwxr-xr-x. 4 root root   28 Apr 18 13:09 jre

解决方法:使用yum安装开发环境 devel。(注意下载对应的版本。所以最好用yum找到准确的devel版本名字,精准下载)


0. 检查自己的openjdk版本,发现是1.8.0
(如果没有安装openjdk,则用yum安装openjdk之后,再来安装一次devel即可)
【参考教程linux 上使用yum 安装openjdk1.8

[root@localhost ~]# java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

1. 用yum查找可以安装的jdk:yum search java|grep jdk

[root@localhost java-1.8.0-openjdk-1.8.0.102-4.b14.el7.x86_64]# yum search java|grep jdk
openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility-debug.i686 : OpenJDK 8 accessibility connector
java-1.8.0-openjdk-accessibility-debug.x86_64 : OpenJDK 8 accessibility
java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment 8 with full debug on
java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment 8 with full debug
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment

(有很多很多文件,这里删减了一部分)
找到了我需要的版本:java-1.8.0-openjdk-devel.x86_64。

2. 安装java-1.8.0-openjdk-devel.x86_64:
yum install java-1.8.0-openjdk-devel.x86_64

这时候会有提示:Is this ok [y/d/N]:
输入y (y的意思是-下载并安装)。

然后静待下载…complete!

3.检查jdk目录下是否有了bin、lib等文件夹:

[root@localhost home]# cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
[root@localhost java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64]# ll
total 4
drwxr-xr-x. 2 root root 4096 Apr 18 13:09 bin
drwxr-xr-x. 3 root root  132 Apr 18 13:09 include
drwxr-xr-x. 4 root root   28 Apr 18 13:09 jre
drwxr-xr-x. 3 root root  144 Apr 18 13:09 lib
drwxr-xr-x. 2 root root  204 Apr 18 13:09 tapset

有了!
再次输入javac,没有报错:

[root@localhost jvm]# javac
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files and annotation processors
  -cp <path>                 Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -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
  -processorpath <path>      Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method parameters
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -h <directory>             Specify where to place generated native header files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -profile <profile>         Check that API used is available in the specified profile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @<filename>                Read options and filenames from file

4. 测试能否正常编译:

写一个简单的Hello.java

public  class Hello{
        public static void main (String[] args){
                System.out.println("hello,hello,hello!four!");
        }
}

用javac命令编译:

[root@localhost home]# javac Hello.java
[root@localhost home]# java Hello
hello,hello,hello!four!

行了,圆满成功。


另:配置java环境变量,其实就是export对应的路径:

  1. vim /etc/profile
  2. 在后面加上这些:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

(我的jdk安装路径是这样的,你的要改成你自己对应的路径。就是等号“=”后面的内容改成你自己的)

  1. 使环境变量生效:source /etc/profile

over

  • 9
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值