Java调用JavaScript函数报错:Expected : but found . 求排查方案
问题排查:Java调用JavaScript函数时的语法错误
错误信息
Expected : but found . fs.writeFileSync( ^ in <eval> at line number 24 at column number 9
对应的JavaScript代码
var YamlToJson = function (name) { var fs = require('fs'), Converter = require('openapi-to-postmanv2'), openapiData = fs.readFileSync('./output.json', { encoding: 'UTF8' }) Converter.convert( { type: 'json', data: openapiData }, {}, (err, conversionResult) => { if (!conversionResult.result) { console.log('Could not convert', conversionResult.reason) } else { fs.writeFileSync( 'outputfile1.json', JSON.stringify(conversionResult.output[0].data), ) console.log('The collection object is: ', conversionResult.output[0].data) } }, ) }
调用的Java代码
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); try { engine.eval(new FileReader("D:/CodeFactory/TestCases/GenerateTetsCases/src/main/java/com/GenerateTetsCases/GenerateTetsCases/YamlToJson.js")); Invocable invocable = (Invocable) engine; Object result; invocable.invokeFunction("myFunction", "value"); } catch (FileNotFoundException | NoSuchMethodException | ScriptException e) { e.printStackTrace(); }
错误原因分析
- 变量声明语法错误:JS代码中
var fs = require('fs'), Converter = require('openapi-to-postmanv2'), openapiData = fs.readFileSync(...)行末尾未加分号,直接衔接Converter.convert,导致解析器无法识别语句边界,触发语法错误。 - ES6特性兼容性问题:Nashorn引擎对ES6支持有限,代码中的箭头函数
(err, conversionResult) => {...}和尾随逗号(如fs.writeFileSync参数列表最后一个逗号、Converter.convert回调后的逗号)都会引发解析异常。 - 函数名不匹配:Java代码调用的是
myFunction,但JS代码定义的函数名是YamlToJson,会触发NoSuchMethodException。 - Node.js模块依赖问题:Nashorn并非Node.js环境,直接使用
require('fs')和第三方包openapi-to-postmanv2无法正常加载,后续执行也会报错。
解决办法
1. 修正JS语法问题
- 给变量声明行添加分号:
var fs = require('fs'), Converter = require('openapi-to-postmanv2'), openapiData = fs.readFileSync('./output.json', { encoding: 'UTF8' }); // 添加分号 - 移除所有尾随逗号:
// 修正fs.writeFileSync参数 fs.writeFileSync( 'outputfile1.json', JSON.stringify(conversionResult.output[0].data) // 去掉末尾逗号 ) // 修正Converter.convert参数列表 Converter.convert( { type: 'json', data: openapiData }, {}, function(err, conversionResult) { // 原有逻辑 } // 去掉末尾逗号 ) - 将箭头函数替换为普通
function,适配Nashorn的ES5语法:function(err, conversionResult) { if (!conversionResult.result) { console.log('Could not convert', conversionResult.reason); } else { fs.writeFileSync( 'outputfile1.json', JSON.stringify(conversionResult.output[0].data) ); console.log('The collection object is: ', conversionResult.output[0].data); } }
2. 修正函数名匹配问题
Java代码中调用正确的函数名YamlToJson:
invocable.invokeFunction("YamlToJson", "value");
3. 解决Node.js模块依赖问题
Nashorn无法直接使用Node.js模块,有两种替代方案:
- 方案一:Java处理文件IO:在Java代码中读取
output.json并传递给JS函数,处理完成后由Java写入结果文件,避免JS中使用fs。 - 方案二:替换JS引擎:改用
GraalVM JavaScript引擎,它支持Node.js兼容模式,可直接加载Node.js核心模块和第三方包。
修正后的完整JS代码示例
var YamlToJson = function (name) { var fs = require('fs'), Converter = require('openapi-to-postmanv2'), openapiData = fs.readFileSync('./output.json', { encoding: 'UTF8' }); Converter.convert( { type: 'json', data: openapiData }, {}, function(err, conversionResult) { if (!conversionResult.result) { console.log('Could not convert', conversionResult.reason); } else { fs.writeFileSync( 'outputfile1.json', JSON.stringify(conversionResult.output[0].data) ); console.log('The collection object is: ', conversionResult.output[0].data); } } ); };
内容的提问来源于stack exchange,提问作者Aniket Thorat
相关产品推荐
相关产品推荐

