Reusing Java code in a Ruby on Rails application

Source: Internet
Author: User
Tags variables require zip java web ruby on rails apache tomcat java se


Before you start



About this tutorial



Ruby on Rails (rails) is a full-stack WEB application framework written in Ruby, and Ruby is a rich, free, extensible, portable, object-oriented scripting language. Rails is very popular among WEB application developers. With it, you can quickly and efficiently develop Web applications and deploy them to any web container, such as ibm®websphere® or Apache Tomcat.



Prior to the advent of Rails and similar web application development frameworks, the standard tools for Web application development are the Java language, because the Java language is platform-independent and has a complete set of APIs. Many Java Web applications are still running, which results in a lot of useful, well-written Java code (referred to as legacy code in this tutorial) for good usability. Legacy Java code is usually packaged in a set of JAR files.



If you change the WEB application development platform to Rails, you can reuse legacy Java code. Ruby Java Bridge (RJB) is a toolkit that enables you to load JAR files into your Rails application and access the methods and variables in your Rail application. This tutorial explains how to configure and use RJB in a Rails application.



Goal



In this tutorial, you will learn how to:



Download, compile, and install RJB



Set up RJB to access the shared Java library



Load Legacy Java code into a Rails application and access



This tutorial does not delve into the features of Rails. One of the advantages of Rails, compared to other Web frameworks, is that the number and quality of documents used for the platform are high.



Prerequisite



This tutorial assumes that the reader is basically familiar with the Java language, Ruby, and Ruby on Rails.



System Requirements



This tutorial assumes that you are using the linux® system (however, the steps on Windows® are essentially the same). This tutorial assumes that you have a working Ruby on Rails.



RJB requires that the Java SDK be installed on the system. If you need a Java SDK, you can download the latest Java SE SDK for your platform and install it right away.



RJB Installation and Setup



This section takes you through the download, installation, compilation, and setup of RJB.



Download RJB



RJB can be downloaded in the form of a standard Ruby Gem package or its own compiled source code archive. For the demo, I recommend downloading the source code archive, so I'll use this approach. With less gossip, download the RJB 1.1.3 source. zip file (the latest RJB version is available at the time of this tutorial).



Be sure to set or update the following environment variables, which are required to install RJB:



Java_home must point to the Java SDK installation directory.



PATH must include $JAVA _home/bin.



For example, in Bash (Linux only), assuming that the Java SDK has been installed to/USR/LOCAL/JDK60, execute the following command:



[root@san]# export  JAVA_HOME=/usr/local/jdk60

[root@san]# export  PATH=$PATH:$JAVA_HOME/bin



Compiling and installing RJB



The next step is to compile and install RJB by executing the following command:



[root@san]# unzip rjb- 1.1.3.zip

[root@san]# cd rjb-1.1.3

[root@san]#  ruby setup.rb config

[root@san]# ruby setup.rb  setup

[root@san]# ruby setup.rb install



Confirm Installation Success



To confirm the success of the RJB installation, first invoke the Ruby Interactive console IRB:



[root@san]# IRB



Then enter require ' RJB ':



irb(main):001:0> require  'rjb'

=> true

irb(main):002:0>exit 



If the Require ' RJB ' command returns TRUE, it means the Ruby installation recognizes the newly installed RJB library. You can now start using RJB in your application.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.