Current dir in Script Runner

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?

1 answer

1 accepted

1 vote

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?

Have updated the answer with more details.

this.class is coming from the fact that every script is compiled into the main class and extended from object?

 

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.

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

I want to use

this.class.getResource("config.groovy").text
with the ConfigSlurper, but I don´t know how to put that in there, any idea?
def config = new ConfigSlurper()
config.parse(this.class.getResource("config.groovy").text) throws an exception

We can't guess what the exception is... can you tell us?

Ah sure, sorry:
http://pastebin.com/pbDphq9y

Created a second question about that topic, but I can´t find it anymore. sad

I answered the other question on this topic you created, so let's end this thread here...

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

226 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you