Forum: JRuby getClass().getRessource('monFichier.txt')

Posted by Jluc Andrei (kantenajla)
on 2010-08-24 22:21
Hello.
I'm working with jruby (without rails).
I need to get a ressource file in my jar.

In java, I can do this with getClass().getRessource('monFichier.txt')
How can i do this with jRuby?
Posted by Nick Sieger (Guest)
on 2010-08-24 22:53
(Received via mailing list)
On Tue, Aug 24, 2010 at 1:21 PM, Jluc Andrei <lists@ruby-forum.com> 
wrote:
> Hello.
> I'm working with jruby (without rails).
> I need to get a ressource file in my jar.
>
> In java, I can do this with getClass().getRessource('monFichier.txt')
> How can i do this with jRuby?

Probably the simplest way would be to do:

    require 'jruby'
    JRuby.runtime.jruby_class_loader.get_resource("monFichier.txt)

/Nick

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

    http://xircles.codehaus.org/manage_email
Posted by Jluc Andrei (kantenajla)
on 2010-08-25 11:47
Nick Sieger wrote:

> Probably the simplest way would be to do:
> 
>     require 'jruby'
>     JRuby.runtime.jruby_class_loader.get_resource("monFichier.txt)
> 

thanks it's work, but only when i am in a jarfile

with javawebstart it doesn't work

my code:
  fichier_base = 
JRuby.runtime.jruby_class_loader.get_resource('lib/lanceur.db.h2.db')
  File.syscopy fichier_base, File.join($HOME, 'base')

the error stack:

classpath:/META-INF/jruby.home/lib/ruby/1.8/ftools.rb:59:in `syscopy': 
No such file or directory - 
/home/kantena/http:/192.168.2.9/monbuild/coucou.jar!/lib/lanceur.db.h2.db 
(Errno::ENOENT)
  from classpath:/chemins.rb:12
  from classpath:/chemins.rb:2:in `require'
  from Main.rb:2
  ...internal jruby stack elided...
  from #<Class:01x12b72f4>.syscopy(classpath:/chemins.rb:12)
  from (unknown).(unknown)(classpath:/chemins.rb:2)
  from Kernel.require(Main.rb:2)
  from (unknown).(unknown)(:1)

the path is wrong
Posted by Nick Sieger (Guest)
on 2010-08-25 19:26
(Received via mailing list)
On Wed, Aug 25, 2010 at 2:47 AM, Jluc Andrei <lists@ruby-forum.com> 
wrote:
> with javawebstart it doesn't work
>
> my code:
>  fichier_base =
> JRuby.runtime.jruby_class_loader.get_resource('lib/lanceur.db.h2.db')
>  File.syscopy fichier_base, File.join($HOME, 'base')

Instead try:

fichier_base = 
JRuby.runtime.jruby_class_loader.get_resource('lib/lanceur.db.h2.db')
File.open(File.join($HOME, 'base', 'lanceur.db.h2.db'), "w") do |out|
  begin
    stream = fichier_base.open_stream
    out << stream.to_io.read
  ensure
    stream.close
  end
end

We're using java.net.URL#openStream here to read the contents rather
than expecting that the file exists locally such that it can be
copied.

/Nick

>  ...internal jruby stack elided...
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.