Ever need to include a Java jar file in a JRuby script? As I work to convert my Mac speech recognition server to JRuby, the first task I need to tackle is to include the Sphinx-4 jar files in my JRuby path. You can include one Java jar file in your JRuby script path very easily, like this:
require 'lib/sphinx4.jar'
where the jar file 'sphinx4.jar' is in a local subdirectory named lib, which is cool. But if you need to include many Java jar files into your JRuby script at one time, here's a very cool way to do that:
Dir["lib/*.jar"].each { |jar| require jar }
That is pretty awesome. I wish I had thought of it myself, but I found in on blogs.sun.com.
Putting all this together, the beginning of my new JRuby script looks like this:
require 'java' Dir["lib/*.jar"].each { |jar| require jar } include_class 'java.awt.Dimension' include_class 'java.awt.Rectangle' include_class 'edu.cmu.sphinx.frontend.util.Microphone' include_class 'edu.cmu.sphinx.recognizer.Recognizer' include_class 'edu.cmu.sphinx.result.Result' include_class 'edu.cmu.sphinx.util.props.ConfigurationManager'
Note that it is important to put your "require" statements before your "include" statements.