Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Hi,
I want to open a file in the same dir as the calling script:
new File("config.groovy")
It is not found because it´s not in D:\JIRA, which is my dev install dir. The sources are in another dir, D:\Development\JIRA and are set in sentenv.bat.
The production system is linux, so I can´t hardcode any paths.
new File(".").getCanonicalPath() resolves to D:\JIRA
What can I do to load that file?
You should use something like:
this.class.getResource("config.groovy").text
getResource will use the classloader to return a URL which is just a pointer to a resource. Then we are just loading the content doing .text
Sometimes you don't know the absolute/relative path or is different on other platforms, so specifying a resource from the classpath to be loaded is useful.
ok, this works, i´m new to Java and Groovy, what am I doing there exactly?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Have updated the answer with more details.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this
.class is coming from the fact that every script is compiled into the main class and extended from object?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
this.class just gets the current class which has access to the classloader associated with that class. Both files are on the classpath so it works.
We could have done MyClass.class.getResource("config.groovy").text which would have given the same.
If you search online theres a ton of information about this if you want further details.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What i meant is this:
http://groovy-lang.org/structure.html#_script_class
Then you have have the this context and could call class. If I got that right :o
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I want to use
this
.
class
.getResource(
"config.groovy"
).text
def config = new ConfigSlurper()
config.parse(this.class.getResource("config.groovy").text) throws an exception
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We can't guess what the exception is... can you tell us?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah sure, sorry:
http://pastebin.com/pbDphq9y
Created a second question about that topic, but I can´t find it anymore.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I answered the other question on this topic you created, so let's end this thread here...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.