当前位置:网站首页>【错误记录】Groovy工程中的文件查找策略 ( main 函数中需要使用 src/main/groovy/Script.groovy | Groovy 脚本直接使用代码相对路径 )
【错误记录】Groovy工程中的文件查找策略 ( main 函数中需要使用 src/main/groovy/Script.groovy | Groovy 脚本直接使用代码相对路径 )
2022-04-21 14:00:00 【韩曙亮_】
文章目录
一、报错信息
在 Java 类中 , 调用 Groovy 脚本 , 出现如下错误 ;
java.
io.
FileNotFoundException:
Y:
\002_WorkSpace
\003_IDEA
\Groovy_Demo
\Script.
groovy (
Y:
\002_WorkSpace
\003_IDEA
\Groovy_Demo
\Script.
groovy)
at
groovy.
lang.
GroovyCodeSource.
<
init
>(
GroovyCodeSource.
java:
108)
at
groovy.
lang.
GroovyShell.
evaluate(
GroovyShell.
java:
470)
at
JavaClass.
startScript(
JavaClass.
java:
17)
at
JavaClass.
main(
JavaClass.
java:
22)
- 1.
- 2.
- 3.
- 4.
- 5.

二、解决方案
Groovy_Demo 是工程根目录名称 ;
这个错误本身很简单 , 但是涉及到 Java 与 Groovy 的路径查找机制的不同 ;
Java 类 JavaClass 位于 Groovy_Demo\src\main\groovy 目录下 , 要在该 Java 类中调用同目录的 Script.groovy 脚本 ;
此处必须使用完整的路径 “src/main/groovy/Script.groovy” , 才能查找到 “Script.groovy” 脚本 ;
Java 类中调用 Groovy 脚本 , 需要使用 “src/main/groovy/Script.groovy” 路径 ;
import
groovy.
lang.
Binding;
import
groovy.
lang.
GroovyShell;
import
java.
io.
File;
import
java.
io.
IOException;
public
class
JavaClass {
public
void
startScript()
throws
IOException {
// 注意这里创建 groovy.lang.Binding
Binding
binding
=
new
Binding();
// 设置 args 参数到 Binding 中的 variable 成员中
binding.
setVariable(
"args",
new
String[]{
"arg0",
"arg1"});
// 执行 Groovy 脚本
GroovyShell
shell
=
new
GroovyShell(
JavaClass.
class.
getClassLoader(),
binding);
File
file
=
new
File(
"src/main/groovy/Script.groovy");
shell.
evaluate(
file);
}
public
static
void
main(
String[]
args) {
try {
new
JavaClass().
startScript();
}
catch (
IOException
e) {
e.
printStackTrace();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
而在 Groovy 脚本中调用 另外一个 Groovy 脚本 , 如果两个 Groovy 脚本在同一个目录中 , 可以直接使用相对路径 " Script.groovy " 进行调用即可 ;
参考 【Groovy】Groovy 脚本调用 ( Groovy 脚本中调用另外一个 Groovy 脚本 | 绑定作用域 binding 变量分析 | Binding 类 variables 成员分析 ) 博客的源码 ;
版权声明
本文为[韩曙亮_]所创,转载请带上原文链接,感谢
https://blog.51cto.com/u_14202100/5237115
边栏推荐
猜你喜欢

comparison of infinitesimal

一个悄然崛起的国产软件

无人驾驶虚拟仿真(十四)--图像处理之交通标志牌识别2

Flow analysis (CTF)

Monotonicity and concavity of function

函数的连续性

ROS2学习笔记(十)-- ROS2 launch启动文件

C语言实现杨辉三角

MySQL 5.7 Optimization: explain the execution plan in nearly 10000 words

The common interface of priority queue (heap) introduces the storage of heap and the creation of heap
随机推荐
Implementation of mine sweeping with C language
Redisjson: a redis that can store JSON
手把手教你使用rand函数实现猜数字游戏
uiautomator2 自动化测试工具使用
MySQL builds master-slave competition master of MHA cluster
洛必达法则
The common interface of priority queue (heap) introduces the storage of heap and the creation of heap
ROS2学习笔记(七)-- 自定义动作实现路口转弯
Go 对文件的相关操作整理(持续更新)
剑指office-割线子
Redis 如何从海量数据中查询某一个 Key ?
MysQL读写分离服务器--maxscale服务
对机器学习的理解
暴力破解美团最新JVM面试题:无限执行
山高路远不畏险
pytorch机器学习之numpy基础
How does redis query a key from massive data?
MySQL data backup management
glusterfs存储搭建初步测试
C语言乘法表