当前位置:网站首页>[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
2022-07-05 22:18:00 【Programmer community】
One 、 Error message
stay Java Class , call Groovy Script , The following error occurred ;
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)
Two 、 Solution
Groovy_Demo Is the name of the project root directory ;
The mistake itself is simple , But it involves Java And Groovy Different path finding mechanisms ;
Java class JavaClass be located Groovy_Demo\src\main\groovy Under the table of contents , To be in the right place Java Call middle note in class Script.groovy Script ;
The full path must be used here “src/main/groovy/Script.groovy” , To find “Script.groovy” Script ;
Java In the class Groovy Script , Need to use “src/main/groovy/Script.groovy” route ;
import groovy.lang.Binding;import groovy.lang.GroovyShell;import java.io.File;import java.io.IOException;public class JavaClass {
public void startScript() throws IOException {
// Notice that you create groovy.lang.Binding Binding binding = new Binding(); // Set up args Parameter to Binding Medium variable Among members binding.setVariable("args", new String[]{
"arg0", "arg1"}); // perform Groovy Script 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(); } }}
And in the Groovy Call in script Another one Groovy Script , If two Groovy The script is in the same directory , You can use relative paths directly " Script.groovy " Call ;
Reference resources 【Groovy】Groovy Script call ( Groovy Another script is invoked. Groovy Script | Binding scope binding Variable analysis | Binding class variables Member analysis ) Blog source code ;
边栏推荐
- "Chris Richardson microservices series" uses API gateway to build microservices
- K210 learning notes (IV) k210 runs multiple models at the same time
- [Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
- Matlab draws a cute fat doll
- Talking about MySQL index
- Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
- Type of fault
猜你喜欢
Three "factions" in the metauniverse
Pl/sql basic case
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
Blocking protocol for concurrency control
Leetcode simple question: the minimum cost of buying candy at a discount
Unique occurrence times of leetcode simple questions
Overview of database recovery
Interview questions for famous enterprises: Coins represent a given value
实战:fabric 用户证书吊销操作流程
随机推荐
Official clarification statement of Jihu company
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
boundary IoU 的计算方式
[Yugong series] go teaching course in July 2022 004 go code Notes
K210学习笔记(四) K210同时运行多个模型
Overview of concurrency control
Oracle views the data size of a table
Sub total of Pico development
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Create a virtual machine on VMware (system not installed)
Solutions for unexplained downtime of MySQL services
Nacos 的安装与服务的注册
Metaverse Ape上线倒计时,推荐活动火爆进行
AD637 usage notes
Sentinel production environment practice (I)
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Stored procedures and stored functions
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
Character conversion PTA
每日刷题记录 (十四)