Talk:Java Integration

From JRubyWiki

Jump to: navigation, search

OK, this is a good start on a starter, but it doesn't tell me how to do much of anything.

I have a java application. Suppose it is fully formed and I want to add a little Ruby to it.

I write

MyRubyClass < SomeJavaClass ... end

How to I instantiate that from Java and then call methods on the instance?

If SomeJavaClass is an interface, you should be able to instantiate it and pass it back out to Java through, for example, BSF's scripting interfaces. Support for concrete classes is coming as well. Headius

"If SomeJavaClass is an interface, you should be able to instantiate it and pass it back out to Java through, for example, BSF's scripting interfaces. Support for concrete classes is coming as well."

Sounds great. How?

Contents

Setting variables from Java (or passing parameters)

I embedded the Jruby interpreter package (jruby-complete.jar - when will it be officially supported?) in our Java application. We don't use Java 6 at the moment, so I used the direct method to instantiate the interpreter. So far so good - it works. But from here I'm pretty much on my own. I scanned through the javadoc, but couldn't find a method to set variables in the instantiated interpreter. The Java 6 ScriptEngine has getContext().setAttribute(), is there something similar that I could use? Yes, I could solve this problem via static Java classes, but that's not nice. For example, I need to pass the running script the directory path it was loaded from. Thanks for any ideas.

org.jruby.exceptions.RaiseException when running the smple code

I was trying to run the sample code "RubyFromJava" but I got error like below:

org.jruby.exceptions.RaiseException /Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:1177:in `get_proxy_class': cannot load Java class RubyFromJava (NameError) from /Users/enebo/jruby/releases/jruby-0_9_8/src/builtin/javasupport.rb:1177:in `const_missing' from <script>:8 ...internal jruby stack elided... from org.jruby.Ruby.eval(Ruby.java:273) from org.jruby.Ruby.evalScript(Ruby.java:266) from com.esri.yingqi.jruby.test.RubyFromJava.main(RubyFromJava.java:47)

Does any one know why that is happening? I copy exact the same code from article.

Thanks!

How to debug JRuby called from Java?

I am using JRuby (through the standard JRE 6 support) to add some Ruby classes to a big existing Java application.

This application is started as usual as any Java app (by running a main method), and it eventually calls my Ruby code. Is there a way to debug Ruby code which is being called from Java?

If not, are there plans for JRuby to eventually support that? And in any case, is there any hack/workaround?

Thanks,

  Luis.

Chinese Character Problem.

To use the JRuby 1.1b with Java6, I checked out the JRubyScriptEngine from the cvs and made the patch as that described in the section "Java6" in this article. But I got a charactor problem:

     ScriptEngineManager m = new ScriptEngineManager();   
     ScriptEngine rbEngine = m.getEngineByName("ruby");
     try {
       rbEngine.eval("puts \"中文\n\"");
       rbEngine.eval("print \"中文\n\"");
       rbEngine.eval("print \"xdf\n\"");
     } catch (ScriptException e) {
       e.printStackTrace();
     }

I tested this in Eclipse, and I got a "??????" where it should be a "中文"。 Any ideas? Thanks!

Personal tools