当前位置:网站首页>[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 ;
边栏推荐
- ICMP introduction
- Daily question brushing record (XIV)
- K210学习笔记(四) K210同时运行多个模型
- Depth first DFS and breadth first BFS -- traversing adjacency tables
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- C language knowledge points link
- Hcip day 16
- AD637使用筆記
- Damn, window in ie open()
- AD637使用笔记
猜你喜欢
2022软件测试工程师涨薪攻略,3年如何达到30K
Installation of VMware Workstation
Leetcode simple question: the minimum cost of buying candy at a discount
点到直线的距离直线的交点及夹角
The difference between MVVM and MVC
Metaverse Ape获Negentropy Capital种子轮融资350万美元
Alternating merging strings of leetcode simple questions
Sparse array [matrix]
Type of fault
K210学习笔记(四) K210同时运行多个模型
随机推荐
119. Pascal‘s Triangle II. Sol
Leetcode simple question ring and rod
科技云报道:算力网络,还需跨越几道坎?
Business learning of mall commodity module
Kubernetes Administrator certification (CKA) exam notes (IV)
Common interview questions of JVM manufacturers
K210学习笔记(四) K210同时运行多个模型
How to view Apache log4j 2 remote code execution vulnerability?
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
AD637 usage notes
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Reptile practice
What if win11 is missing a DLL file? Win11 system cannot find DLL file repair method
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
笔记本电脑蓝牙怎么用来连接耳机
Pl/sql basic syntax
Summary of concurrency control
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Unique occurrence times of leetcode simple questions