Quantcast

Load static classpath resources

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Load static classpath resources

Stan Carney-4
Has anybody loaded files off the classpath in Grails before?

In Java I'm used to doing something like this:

file: com/blah/MyFile.txt

package com.blah

public class MyClass(){

    public void someting(){
       URL url = MyClass.class.getResource("/com/blah/MyFile.txt")
    }
}


Similar code in grails always returns null. I have tried
getResourceAsStream, copying the file to the root of the project, etc...
They all still return null.

Thoughts?

Stan

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Load static classpath resources

burtbeckwith
If it's in the same package as your class, then it'd be


String contents = getClass().getResourceAsStream('MyFile.txt').text


Burt


On Thursday 02 July 2009 10:26:33 pm Stan Carney wrote:
> Has anybody loaded files off the classpath in Grails before?
>
> In Java I'm used to doing something like this:
>
> file: com/blah/MyFile.txt
>
> package com.blah
>
> public class MyClass(){
>
> public void someting(){
> URL url = MyClass.class.getResource("/com/blah/MyFile.txt")
> }
> }
>
>
> Similar code in grails always returns null. I have tried
> getResourceAsStream, copying the file to the root of the project, etc...
> They all still return null.
>
> Thoughts?
>
> Stan



signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Load static classpath resources

Stan Carney-4
Thanks for your response Burt.

It is in the same package but I'm beginning to think the file isn't
being copied correctly. Well at least for tests it isn't. Using intellij
or stopping grails test-app before it deletes
~/.grails/1.1.1/projects/ach/classes I can confirm that my file isn't there.

Do I have to add it as a resource to be copied or something?

Stan


Burt Beckwith wrote:

> If it's in the same package as your class, then it'd be
>
>
> String contents = getClass().getResourceAsStream('MyFile.txt').text
>
>
> Burt
>
>
> On Thursday 02 July 2009 10:26:33 pm Stan Carney wrote:
> > Has anybody loaded files off the classpath in Grails before?
> >
> > In Java I'm used to doing something like this:
> >
> > file: com/blah/MyFile.txt
> >
> > package com.blah
> >
> > public class MyClass(){
> >
> > public void someting(){
> > URL url = MyClass.class.getResource("/com/blah/MyFile.txt")
> > }
> > }
> >
> >
> > Similar code in grails always returns null. I have tried
> > getResourceAsStream, copying the file to the root of the project,
> etc...
> > They all still return null.
> >
> > Thoughts?
> >
> > Stan
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Load static classpath resources

Stan Carney-4
I found the issue: http://jira.codehaus.org/browse/GRAILS-4646

Stan

Stan Carney wrote:

> Thanks for your response Burt.
>
> It is in the same package but I'm beginning to think the file isn't
> being copied correctly. Well at least for tests it isn't. Using
> intellij or stopping grails test-app before it deletes
> ~/.grails/1.1.1/projects/ach/classes I can confirm that my file isn't
> there.
>
> Do I have to add it as a resource to be copied or something?
>
> Stan
>
>
> Burt Beckwith wrote:
>> If it's in the same package as your class, then it'd be
>>
>>
>> String contents = getClass().getResourceAsStream('MyFile.txt').text
>>
>>
>> Burt
>>
>>
>> On Thursday 02 July 2009 10:26:33 pm Stan Carney wrote:
>> > Has anybody loaded files off the classpath in Grails before?
>> >
>> > In Java I'm used to doing something like this:
>> >
>> > file: com/blah/MyFile.txt
>> >
>> > package com.blah
>> >
>> > public class MyClass(){
>> >
>> > public void someting(){
>> > URL url = MyClass.class.getResource("/com/blah/MyFile.txt")
>> > }
>> > }
>> >
>> >
>> > Similar code in grails always returns null. I have tried
>> > getResourceAsStream, copying the file to the root of the project,
>> etc...
>> > They all still return null.
>> >
>> > Thoughts?
>> >
>> > Stan
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Loading...