# Getting resources

**URL:** <https://www.ruby-forum.com/t/getting-resources/116710>\
**Category:** JRuby\
**Created:** [October 8, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/getting-resources/116710 "2007-10-08T22:38:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thewordnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@thewordnerd](https://www.ruby-forum.com/u/thewordnerd)\
**Post date:** [October 8, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/getting-resources/116710/1 "2007-10-08T22:38:24Z")

</div>

I’m not sure if I’m missing something obvious here. I’m attempting to  
adapt code of the following form:

Application.class.getResource(“config/application.conf”);

or

getClass().getResource("/config/application.conf");

but am having problems. More specifically, I can’t seem to find the  
getResource() method from JRuby. I’ve tried:

Application.class.getResource()  
Application.getClass.getResource()  
Application.java\_class.getResource()

and none of these seem to find getResource(). This is on JRuby trunk.  
What am I missing?

Thanks.

* * *

To unsubscribe from this list please visit:

```
http://xircles.codehaus.org/manage_email
```

---

<div class="post-metadata">

**Author:** ![thewordnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@thewordnerd](https://www.ruby-forum.com/u/thewordnerd)\
**Post date:** [October 8, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/getting-resources/116710/2 "2007-10-08T23:00:07Z")

</div>

I recently ran into this same issue, but I was able to get what I  
needed using something like:

jc = Java::JavaClass.for\_name(“classname”)  
jc.java\_class.java\_method(“getResource”).invoke(jc)

I was also a bit confused why none of the things you tried below  
worked, but I never got around to posting.

Related to this, is there a reason why JRuby doesn’t set the current  
Java thread’s contextClassLoader to the JRubyClassLoader? This caused  
all kinds of problems for me because I was trying to use the Spring’s  
ClassPathXmlApplicationContext, but it seems that internally, Spring  
keeps trying to load stuff with the Thread’s contextClassLoader so  
resources that I had added to the $CLASSPATH were not being found. I  
wound up with something like:

load\_from\_class = Java::JavaClass.for\_name  
(“org.springframework.context.support.ClassPathXmlApplicationContext”)  
class\_loader = load\_from\_class.java\_class.java\_method  
(“getClassLoader”).invoke(load\_from\_class)

JThread.currentThread().setContextClassLoader(class\_loader)

@@app\_context = ClassPathXmlApplicationContext.new(paths.to\_java  
(:string), ClassPathXmlApplicationContext.java\_class)

I’m still not sure if that’s going to come back and bite me eventually.

-lenny

On Oct 8, 2007, at 4:38 PM, Nolan D. wrote:

> getResource() method from JRuby. I’ve tried:
> 
> * * *
> 
> To unsubscribe from this list please visit:
> 
> [http://xircles.codehaus.org/manage\_email](http://xircles.codehaus.org/manage_email)

* * *

To unsubscribe from this list please visit:

```
http://xircles.codehaus.org/manage_email

```

---

<div class="post-metadata">

**Author:** ![thewordnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@thewordnerd](https://www.ruby-forum.com/u/thewordnerd)\
**Post date:** [October 9, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/getting-resources/116710/3 "2007-10-09T18:25:24Z")

</div>

On Oct 8, 2007, at 3:59 PM, Lenny M. wrote:

> jc = Java::JavaClass.for\_name(“classname”)  
> jc.java\_class.java\_method(“getResource”).invoke(jc)

> Ewww. Much as I’m looking forward to writing future apps in Ruby,  
> I’m not looking forward to having to do _that_. 😕 I use the  
> getClass().getResource() pattern fairly often.

Is this a bug in the Java integration? Has it already been filed, or  
should I? Not sure why self.class.getResource shouldn’t work for Ruby  
subclasses of Java classes. Maybe self.java\_class.getResource() at  
worst.

* * *

To unsubscribe from this list please visit:

```
http://xircles.codehaus.org/manage_email
```

---

<div class="post-metadata">

**Author:** ![thewordnerd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbe5ce/32.png) [@thewordnerd](https://www.ruby-forum.com/u/thewordnerd)\
**Post date:** [October 9, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/getting-resources/116710/4 "2007-10-09T19:13:33Z")

</div>

I’ve seen the same behavior; get\_resource always returns nil when I  
could  
run the same thing in Java and have it work. I thought it might be a  
bug,  
but never got around to posting it here.

My intermediate solution was to make a pure Java class that did just the  
getResource part for me. But using something like  
self.java\_class.get\_resource does look better than what I did. I’m going  
to  
switch to that for now.
