Load static classpath resources

View: New views
4 Messages — Rating Filter:   Alert me  

Load static classpath resources

by Stan Carney-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: Load static classpath resources

by Burt Beckwith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.
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

Re: Load static classpath resources

by Stan Carney-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: Load static classpath resources

by Stan Carney-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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