javax.script.ScriptException: Cannot find engine named: ‘nashorn‘, ensure you set language field in

本文记录了在使用JMeter进行接口测试时遇到的脚本执行错误,问题源于高版本Java移除了nashorn JavaScript引擎,而测试脚本中使用了该引擎。解决方案是将Java版本回退到1.8.0_151,重启JMeter后,登录接口测试成功通过。
摘要由CSDN通过智能技术生成
jmeter运行脚本报错,跟java版本有关,做个记录。

1. 问题记录:

执行登录接口测试,登录失败。点击jmeter右上角【黄色!】,查看错误日志。显示如下:

2022-09-23 10:29:53,642 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 预处理程序
javax.script.ScriptException: Cannot find engine named: 'nashorn', ensure you set language field in JSR223 Test Element: JSR223 预处理程序
    at org.apache.jmeter.util.JSR223TestElement.getScriptEngine(JSR223TestElement.java:100) ~[ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
    at java.lang.Thread.run(Thread.java:833) [?:?]
2022-09-23 10:29:53,692 ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 预处理程序
javax.script.ScriptException: Cannot find engine named: 'nashorn', ensure you set language field in JSR223 Test Element: JSR223 预处理程序
    at org.apache.jmeter.util.JSR223TestElement.getScriptEngine(JSR223TestElement.java:100) ~[ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.modifiers.JSR223PreProcessor.process(JSR223PreProcessor.java:44) [ApacheJMeter_components.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
    at java.lang.Thread.run(Thread.java:833) [?:?]

2. 问题分析:

执行脚本中有一个javascript的脚本(security.js),而高版本java已经移除了nashorn javascript引擎。可推断security.js脚本含有nashorn javascript引擎,因为不支持,所以会报错。

//jmeter JSR223 预处理程序
load("security.js");
//pwd1 = vars.get("passwd");
function RSA(){
    var exponent = "10001";    
    var modulus = vars.get("publicKeyModulus");    
    var passwd = "1";     
    RSAUtils.setMaxDigits(200);
    var publicKey = new RSAUtils.getKeyPair(exponent,"",modulus);    
    return RSAUtils.encryptedString(publicKey,passwd);
}    
var data = RSA();
vars.put("Password",data);

查看脚本运行环境的Java版本

C:\Users\Zyl>java -version
java version "18.0.1.1" 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)

3. 解决办法:

将本地Java版本换为较低版本,如:jdk1.8.0_151

C:\Users\Zyl>java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

重启jmeter后,再次执行登录接口测试。测试通过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值