JAVA编译报错:A JNI error has occurred, please check your installation and try again

笔记:JDK版本不一致导致的错误::A JNI error has occurred, please check your installation and try again

今天通过Idea工具实现一个HelloWorld的Java程序

首先通过建立一个新的项目,命名为test-java

然后在项目下新建一个package,命名为com.wwh

在package下方新建一个类,命名为HelloWorld

编写HellowWorld代码如下:

package com.wwh;

public class HelloWorld {

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

}

鼠标在空白处右键“Run HelloWorld.main()”,程序输出如下所示:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第1张图片
可以看到,exit code为0,已经打印出Hello World ,没有错误
在File-Project Structure-Artifacts对项目进行打包,当对类进行打包时可以选择Main Class,这里不选
打包后对项目进行 Build Artifacts-------test-java-------build,在本地文件夹下打开项目下的out文件夹,可以找到打包后的jar文件,如图
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第2张图片
通过cmd进入到当前文件夹下,运行jar程序,输入java -cp test-java.jar com.wwh.HelloWorld
出现错误:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第3张图片

错误描述:

编译时错误:A JNI error has occurred, please check your installation and try again

错误原因:

重复安装了JDK,电脑中存在不同版本JDK,即安装oracle数据库在配置jdk路径之前,导致运行命令调用了oracle自带的jdk,从而导致这个问题

解决方案:

①命令行中通过“java -version”和“javac -version”查询SDK版本
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第4张图片

②首先进入高级系统设置界面,如下图:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第5张图片
③单击环境变量选项卡,如下图:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第6张图片④单击系统变量中的Path变量
⑤可以看到存在两个版本的java,而我目前使用的是Java17的路径,所以将此选项上移到另一个java上方即可,如下图:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第7张图片
⑥点击一系列确定,完成设置,再次实验一开始的编译、运行命令,如下图(记得重启dos窗口,重新加载环境变量)
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第8张图片
成功解决问题!!

验证:

检查Java版本,发现一致:
JAVA编译报错:A JNI error has occurred, please check your installation and try again_第9张图片

你可能感兴趣的:(JavaEE,java)