FaqJRubyAndIRB

How can I access Java from the JRuby IRB?

Let's say your Ruby program uses some of your own Java classes (not the ones in the standard Java libraries). You go into the Projects customizer, choose the Java tab and you can add JAR files there that are automatically added to the classpath in JRuby when you run your Ruby program. As a result, your ruby code can call into the Java classes.

Unfortunately, the Java settings tab does not work for the "IRB console" window (see this issue). The reason for this is that JRuby in the IRB window is running in the same Java VM as the IDE, and the extra jars are not loaded into the IDE's module code yet.

For now, you can use the following workaround to access Java from the JRuby IRB: Copy your jars into an IDE startup directory (platform7/lib). Jars in that directory will be visible to all code running in the IDE.

$ cp myjar1.jar myjar2.jar ~/my/install/netbeans/platform7/lib/

You can now start the IDE, open up the IRB window from Window | Other and load classes from the jars etc.

irb(main):001:0> require 'java'
=> true
irb(main):002:0> include_class 'foo.bar.Baz'
=> ["foo.bar.Baz"]
irb(main):003:0> puts Baz.message
I'm a String-returning-method in Java class Baz
=> nil
irb(main):004:0> 

Note that this workaround affects the IRB window only. You still need to customize the Java JAR path in the project customizer for the classes to be available when running your project.

For more about using Java from the IRB, see Getting Your Feet Wet With Java in JRuby.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo