在linux上启动arthas报“Can not find java process”
阅读原文时间:2023年08月11日阅读:6

发生背景

完整报错信息:

[***@localhost ~]$ java -jar arthas-boot.jar
[INFO] JAVA_HOME: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.222.b03-1.el7.x86_64/jre
[INFO] arthas-boot version: 3.6.9
[INFO] Can not find java process. Try to run `jps` command lists the instrumented Java HotSpot VMs on the target system.
Please select an available pid.

linux环境:

[***@localhost ~]$ cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)

解决方案

出现这个报错是因为找不到jps命令,我们默认安装的openjdk是不支持jps的,所以需要卸载Open JDK,安装Oracle JDK,步骤如下:

  1. 查看已有的openjdk版本

    [***@localhost ~]$ rpm -qa|grep jdk
    java-1.8.0-openjdk-1.8.0.222.b03-1.el7.x86_64
    java-1.7.0-openjdk-1.7.0.221-2.6.18.1.el7.x86_64
    java-1.8.0-openjdk-headless-1.8.0.222.b03-1.el7.x86_64
    java-1.7.0-openjdk-headless-1.7.0.221-2.6.18.1.el7.x86_64
    copy-jdk-configs-3.3-10.el7_5.noarch

  2. 卸载openjdk

    [***@localhost ~]$ su
    Password:
    [root@localhost yuwei]# yum -y remove copy-jdk-configs-3.3-10.el7_5.noarch

    [root@localhost yuwei]# java -version
    bash: java: command not found…

  3. 安装jdk

    3.1 将jdk上传到home目录

    3.2 创建jvm目录 mkdir /usr/lib/jvm

    3.3 解压安装包 tar -zxvf ./jdk-8u181-linux-x64.tar.gz -C /usr/lib/jvm

    3.4 编辑环境变量 vi ~/.bashrc

    3.5 文件最下面添加

    export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_181
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
    export PATH=${JAVA_HOME}/bin:$PATH

    3.6 刷新配置文件,查看版本

    [root@localhost ~]# source ~/.bashrc
    [root@localhost ~]# java -version
    java version "1.8.0_181"
    Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
  4. 最后重启jar看效果

    [root@localhost ***]# java -jar arthas-boot.jar
    [INFO] JAVA_HOME: /usr/lib/jvm/jdk1.8.0_181/jre
    [INFO] arthas-boot version: 3.6.9
    [INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.

    • [1]: 3137 math-game.jar
  5. 继续报错

    [INFO] File size: 17.83 MB, downloaded size: 17.81 MB, downloading …
    [INFO] Download arthas success.
    [INFO] arthas home: /root/.arthas/lib/3.6.9/arthas
    [INFO] Try to attach process 3137
    Picked up JAVA_TOOL_OPTIONS:
    [ERROR] Start arthas failed, exception stack trace:
    com.sun.tools.attach.AttachNotSupportedException: Unable to open socket file: target process not responding or HotSpot VM not loaded
    at sun.tools.attach.LinuxVirtualMachine.(LinuxVirtualMachine.java:106)
    at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
    at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
    at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:102)
    at com.taobao.arthas.core.Arthas.(Arthas.java:27)
    at com.taobao.arthas.core.Arthas.main(Arthas.java:161)
    [ERROR] attach fail, targetPid: 3137

切换用户,arthas程序和应用程序需要在同一个用户账号下,app应用程序是普通用户,当前在root用户下执行的arthas,所以报错。

启动成功,效果图