Creating gems-wrappers for executable java jars

Today I want to share little trick that lets us to use ruby distribution system (rubygems) as delivery mechanism for java libraries/executables. The idea was borrowed from redcar project which in turn was borrowed/extracted from ruby-processing gem. I am trying to extract important logic into separate jar_wrapper gem, so it can be easily used in any ruby project.

Install jar_wrapper with this command:

1
$ gem install jar_wrapper

Now you can use it in your program directly or create gem for delivering java jar file(s). For example, this code will upload remote file:

1
2
3
4
5
6
7
8
9
10
11
require 'jar_wrapper' wrapper = JarWrapper::Runner.new wrapper.java_opts = ["-Xmx1024m", "-Xss1024k" ] # install jar file from source URL into target file: source = "http://selenium.googlecode.com/files/selenium-server-2.37.0.zip" target = "selenium-server.zip" wrapper.install source, target

You can aldo execute jar file (a.k.a. “java -jar jar_file.jar”)

1
2
3
wrapper.jar_file = "jar_file.jar" wrapper.run

Or you can execute java jar file by specifying main class (a.k.a. “java -cp someclasspath main_class”):

1
2
3
4
wrapper.classpath = "some_class_path" wrapper.main_class = "main_class" wrapper.run

If you need more details on how to use it for practical gem, visit selenium gem. This gem downloads and create launcher for standalone selenium server.