You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bsf-user@jakarta.apache.org by wolverine my <wo...@gmail.com> on 2006/10/19 06:59:13 UTC

How to access JRuby object from Java program?

Hi!

I have a JRuby script as shown below:

   require 'java'
   include_class 'java.util.HashMap'
   $map2 = HashMap.new


and I would like to retrieve the map2 object in the following Java application:

   BSFManager manager = new BSFManager();
   manager.registerBean("map2", HashMap.class);
   ...
   HashMap map2 = (HashMap) manager.lookupBean("map2");


and the ClassCastException is thrown from the last line:

   Exception in thread "main" java.lang.ClassCastException: java.lang.Class


Do you have any idea of what could be the problem?
How can I pass the object created in JRuby and return it to Java application?

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org


Re: How to access JRuby object from Java program?

Posted by wolverine my <wo...@gmail.com>.
Hi!

Yes, I tried according to the example and the following code prints
out the HashMap's element added in the Ruby script:

        BSFManager manager = new BSFManager ();

        HashMap map2 = new HashMap();
        manager.declareBean("map2", map2, map2.getClass());

        FileReader in = new FileReader("test.rb");
        String script = IOUtils.getStringFromReader(in);

        manager.exec("ruby", "test.rb", -1, -1, script);
        System.out.println("map2=" + map2);


the test.rb contains the following lines:

        require 'java'
        include_class 'java.util.HashMap'

        $map2.put('key', 'value')


OK, the above code hardcodes one bean called "map2" which the script
author has to manipulate in the script file. Is there any way for a
program to find out what are the beans created by the scripts and then
retrieve them one by one (without hardcode the names)?




On 10/20/06, Rony G. Flatscher <Ro...@wu-wien.ac.at> wrote:
> Hi Wolverine,
>
> have you looked up the documentation and the sample from the JRuby project?
>
> Also a very brief overview of how one can access/retrieve BSF beans is
> given in <http://jruby.sourceforge.net/docs/bsf.shtml>.
>
> Please let us know whether this hint was helpful for you!
>
> Regards,
>
> ---rony
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bsf-user-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org


Re: How to access JRuby object from Java program?

Posted by "Rony G. Flatscher" <Ro...@wu-wien.ac.at>.
Hi Wolverine,

have you looked up the documentation and the sample from the JRuby project?

Also a very brief overview of how one can access/retrieve BSF beans is
given in <http://jruby.sourceforge.net/docs/bsf.shtml>.

Please let us know whether this hint was helpful for you!

Regards,

---rony


---------------------------------------------------------------------
To unsubscribe, e-mail: bsf-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bsf-user-help@jakarta.apache.org