Hello World Program in JRuby

In this tutorial of JRuby we are going to tell you from the scratch for example how to install JRuby into your computer and setting path for JRuby.

Hello World Program in JRuby

Hello World Program in JRuby

     

JRuby Introduction

JRuby team developed JRuby, which is a pure implementation of Ruby language into Java programming language. JRuby supports various features of both Java and Ruby such as Object Oriented Programming and duck-typing as Ruby. As JRuby is integrated with Java Technology, it can be directly called by Java Program. 

In this tutorial of JRuby we are going to tell you from the scratch for example how to install JRuby into your computer and setting path for JRuby. After that we have mentioned about how to check if the JRuby is installed correctly in your computer or not.

Now After installing and checking the JRuby into your computer, we will show you how to write your first JRuby "Hello World" Example. So go through this tutorial to find out more about JRuby and writing your first application in JRuby.

Running first JRuby program

Before start to write and execute your JRuby program you have to set environment for JRuby program . You must follow these few simple steps to set environment for your first JRuby application to execute successfully as follows:

  • Download jruby-bin-1.0.1.zip
  • Extract this zip file and put this folder to your directory( e.g. "C:\jruby-bin-1.0.1" in our example)
  • Set JRUBY_HOME to jruby directory ( e.g. "C:\jruby-bin-1.0.1\jruby-1.0.1" in our example)
  • Set your Path environment variable to your jruby's bin directory ( e.g. "C:\jruby-bin-1.0.1\jruby-1.0.1\bin" in our example)
  • Now open command prompt and run "jruby - version" command to check that JRuby is working or not. Output would be like this
C:\JRuby>jruby -version
ruby 1.8.5 (2007-08-23 rev 4201) [x86-jruby1.0.1]


Running first HelloWorld program

1. Create HelloWorld.rb

require "java"

stringHello= "Hello World"
stringDate = java.util.Date.new

puts "#{stringHello.to_s}"
puts "Date := #{stringDate.to_s}"

First line in our program require "java" enables use of java classes in Ruby program. In this example we have created two string objects one contains string "Hello World" and another contains "current date". "to_s" is being used to convert  returning value to string. To print expression's vale we have used puts.

Execute HelloWorld.rb with jruby command and you will get following output:

C:\JRuby>jruby HelloWorld.rb
Hello World
Date := Tue Sep 23 18:12:26 GMT+05:30 2008

Download Sample Code