JRuby is a Java implementation of Ruby that runs atop the Java Virtual Machine. The JRuby wiki has this to say:
JRuby is a 100% Java implementation of the Ruby programming language. It is Ruby for the JVM.
JRuby provides a complete set of core “builtin” classes and syntax for the Ruby language, as well as most of the Ruby Standard Libraries. The standard libraries are mostly Ruby’s own complement of .rb files, but a few that depend on C language-based extensions have been reimplemented. Some are still missing, but we hope to implement as many as is feasible.
Since it runs on the JVM, it benefits from tooling and optimisations that are built into the Java Virtual Machine; this can lead to better performance. It also provides us a way for us to call Java classes and libraries from our Ruby programs. This makes it attractive even though Ruby 2.7 is here and Ruby 3.0 is on its way.
Note: This post was written for JRuby 9.2.10.0 – I uninstalled it and installed JRuby 9.2.11.0 since the release came out quickly. I won’t update the screenshots below but I have noted 9.2.11.0 at some points below.
Ok, so it’s Day 0 – the day we set up JRuby on Windows and run a simple script. We will do the following:
- Download and install JRuby on Windows
- Run a Ruby script
- Install a gem
- Set up pik for multiple JRubies
- Mapping Ruby commands to JRuby
Download and install JRuby on Windows
Getting JRuby running on Windows is really straighforward. Directly on the home page on the top right, you will see the links to get different packages. For Windows, you need to get one of the two exe versions:
- exe – this is the version for 32-bit Windows
- exe(x64) – this is the version for 64-bit Windows
Get the correct version for your computer and run it. Installing JRuby requires administrator permissions to install.
The default installation does two things:
- It installs JRuby into
c:\jruby-version
– as at the time of writing, it would create the directory intoc:\jruby-9.2.10.0
since I’m installing version 9.2.10.0. - It adds this JRuby installation to the PATH on Windows. So, if you open a command prompt and run
jruby -v
you should see something like the screenshot below.
The downloads page for JRuby also provides other downloads and older versions, if you need something else.
I won’t walk through the individual steps, but you are likely to see similar screens as your installation proceeds.
Good! We are ready to try running a simple script.
Note: if you used 9.2.11.0 (which was released on 2nd Marh 2020), JRuby should return 9.2.11.0 as the version and the directory path would also be C:\9.2.11.0
Create and run a simple script
Ok, hello world time. We all know that we just need this for the simplest Ruby script.
1
puts 'Hello, world'
Then, you can run this by doing:
> jruby hello_world.rb
If you tried both ruby and JRuby exactly the same way, you will see the same output results.
E:\blog\jruby\day0>ruby hello_world.rb
Hello, world
E:\blog\jruby\day0>jruby hello_world.rb
Hello, world
You will notice one thing at this point – the JRuby version takes longer to run. This is something you will hear about in every JRuby talk – startup takes a bit longer and JRuby is best (at this point) for server deployments or for desktop scripts that need to run for longer. A single-line “Hello World” is probably one of the worst things that you could use to form an opinion about JRuby’s comparitive performance. We will come back to performance in a different post.
Install a gem
All Ruby programmers will install and use gems. In MRI, we use gem install [gem_name]
which is shortcut for running the system command gem
(which could be done as ruby -S gem install [gem_name]
). JRuby works exactly the same as the second way (but just with jruby instead of ruby).
So, let’s do it on both Ruby and JRuby – for simplicity, let’s install sinatra
on both.
E:\blog\jruby\day0>jruby -S gem install --no-document sinatra
Fetching sinatra-2.0.8.1.gem
Fetching tilt-2.0.10.gem
Fetching rack-protection-2.0.8.1.gem
Fetching rack-2.2.2.gem
Fetching ruby2_keywords-0.0.2.gem
Fetching mustermann-1.1.1.gem
Successfully installed rack-2.2.2
Successfully installed tilt-2.0.10
Successfully installed rack-protection-2.0.8.1
Successfully installed ruby2_keywords-0.0.2
Successfully installed mustermann-1.1.1
Successfully installed sinatra-2.0.8.1
6 gems installed
The equivalent JRuby command changes gem install → jruby -S gem install and that’s all it takes.
E:\blog\jruby\day0>jruby -S gem install sinatra
Fetching: rack-2.2.2.gem (100%)
Successfully installed rack-2.2.2
Fetching: tilt-2.0.10.gem (100%)
Successfully installed tilt-2.0.10
Fetching: rack-protection-2.0.8.1.gem (100%)
Successfully installed rack-protection-2.0.8.1
Fetching: ruby2_keywords-0.0.2.gem (100%)
Successfully installed ruby2_keywords-0.0.2
Fetching: mustermann-1.1.1.gem (100%)
Successfully installed mustermann-1.1.1
Fetching: sinatra-2.0.8.1.gem (100%)
Successfully installed sinatra-2.0.8.1
6 gems installed
So, we know how to install gems now. Let’s install a gem that is very helpful on Windows.
Installing pik when using multiple JRuby Installations
pik is a gem that allows you to switch between different Ruby installations on a Windows computer. Although it is no longer maintained according to the github page, I found that it still works fine for me. It also works with JRuby by updating the PATH and other variables.
Open a command prompt that has JRuby on the path and do the equivalent of gem install pik
E:\blog>jruby -S gem install pik
Fetching: pik-0.2.8.gem (100%)
----------------------------------------------------------------------------
* If you're upgrading from a version <= 0.1.1, you'll want to delete the pik.bat file
from all of your ruby versions. Gem uninstall should do the trick.
* Install pik to a location that's in your path, but someplace other than your ruby\bin dir
If you're upgrading from a more recent version, pik_install will overwrite the older files as needed.
>path
PATH=C:\tools\;C:\ruby\186-p368-mingw32\bin;C:\WINDOWS\system32;C:\WINDOWS
>pik_install C:\tools
* If this is a first-time install, add all the versions of ruby that you want to use with pik
>pik add
Adding: 186: ruby 1.8.6 (2009-03-31 patchlevel 368) [i386-mingw32]
Located at: c:/ruby/186-p368-mingw32/bin
>pik add C:\ruby\IronRuby-091\bin
Adding: 091: IronRuby 0.9.1.0 on .NET 2.0.0.0
Located at: C:/ruby/IronRuby-091/bin
>pik add C:\ruby\jruby-1.4.0RC1\bin
Adding: 140: jruby 1.4.0RC1 (ruby 1.8.7 patchlevel 174) (2009-09-30 80c263b) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java]
Located at: C:/ruby/jruby-1.4.0RC1/bin
----------------------------------------------------------------------------
Successfully installed pik-0.2.8
1 gem installed
If you’re installing 9.2.10.0, you might also see this at the end of the installation trace:
Parsing documentation for pik-0.2.8
Before reporting this, could you check that the file you're documenting
has proper syntax:
C:/jruby-9.2.10.0/bin/jruby.exe -c lib/pik/commands/config_command.rb
RDoc is not a full Ruby parser and will fail when fed invalid ruby programs.
The internal error was:
(NoMethodError) undefined method `[]' for nil:NilClass
ERROR: While executing gem ... (NoMethodError)
undefined method `[]' for nil:NilClass
First – don’t worry. The gem is installed and it can be used. Second, it seems to be due to bad RDoc – you can simply skip generating documents by doing this instead:
>jruby -S gem install --no-document pik
Once this is done, you need to add the current installation to your pik (adjust the path to your JRuby executable). Note that this requires administrator access for your command prompt.
E:\blog>pik add c:\jruby-9.2.10.0\bin
** Adding: 921: jruby 9.2.10.0 (2.5.7) 2020-02-18 fffffff Java HotSpot(TM) 64-Bit Server VM 25.91-b14 on 1.8.0_91-b14 +jit [mswin32-x86_64]
Located at: C:\jruby-9.2.10.0\bin
After this, you should be able to see something like this on the command line.
C:\WINDOWS\system32>>pik jr
Select which Ruby you want:
1. 928: jruby 9.2.8.0 (2.5.3) 2019-08-12 a1ac7ff Java HotSpot(TM) 64-Bit Server VM 25.91-b14 on 1.8.0_91-b14 +jit [mswin32-x86_64]
2. 921: jruby 9.2.10.0 (2.5.7) 2020-02-18 fffffff Java HotSpot(TM) 64-Bit Server VM 25.91-b14 on 1.8.0_91-b14 +jit [mswin32-x86_64]
3. 172: jruby 1.7.27 (1.9.3p551) 2017-05-11 8cdb01a on Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14 +jit [Windows 10-amd64]
4. 911: jruby 9.1.12.0 (2.3.3) 2017-06-15 33c6439 Java HotSpot(TM) 64-Bit Server VM 25.91-b14 on 1.8.0_91-b14 +jit [mswin32-x86_64]
Enter a number to switch to that JRuby. The next time you do a > jruby -S gem list
it will use that JRuby.
If you’re using JRuby 9.2.11.0, then make sure that you change the line above to be pik add c:\jruby-9.2.11.0\bin
Mapping the Ruby commands to JRuby
The short rule is this: irb is jirb, the rest are jruby -S command
. Some of this is from https://github.com/jruby/jruby/wiki/GettingStarted#running-rake-gem-rails-etc
This simply gives us this:
Ruby Command | JRuby Command |
---|---|
irb | jirb |
gem install | jruby -S gem install |
gem list | jruby -S gem list |
rails new project | jruby -S rails new project |
bundle install | jruby -S bundle install |
What about Bundler?
Keep in mind that bundler needs to be installed before that last command runs.
E:\blog\jruby\day0>jruby -S bundle install
jruby: No such file or directory -- bundle (LoadError)
E:\blog\jruby\day0>jruby -S gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 37 seconds
1 gem installed
Look at the Gemfile.lock and you should see something like this in the file which shows you that it’s JRuby (actually, Java)
PLATFORMS
java
What’s next?
In the next post, we look at running scripts and migrating to JRuby fromm Ruby.
Links and References
Here are the main links:
- JRuby Website: https://www.jruby.org/
- Download JRuby: https://www.jruby.org/download
- Getting started pages from the wiki: https://github.com/jruby/jruby/wiki/GettingStarted
- The wiki is at: https://github.com/jruby/jruby/wiki
- Other Walkthroughs and Tutorials: https://github.com/jruby/jruby/wiki/WalkthroughsAndTutorials
OK, so we have done enough for Day 0 – we know how to install JRuby, run a script with JRuby, install gems directly, switch between different JRuby installation, and also use Bundler. We are ready for more!