Using the Java WEB start deployment JRuby Application

Source: Internet
Author: User
Tags java web

You usually need a command to get into the combined world of Ruby and Java:

Include Java

This allows you to instantiate Java classes, invoke their methods, and even inherit them as if they were just plain ruby objects. But there are some subtle differences, and this article will show you how to manage them so you can design new applications at the fastest speed and deploy them to your customers.

This article is based on a simple application that uses JRuby and swing to implement a simple ObjectSpace browser. Ruby's ObjectSpace feature provides a way to access all the objects in the system. For example, we can print all the strings in use like this:

ObjectSpace.each_object(String) do |string|
puts string
end

When the program runs in my IRB session, approximately 28,000 strings are generated. By using swing and JRuby, we can display different classes on a very good graphical interface, including their instances and the methods available. You can even click on them in the rightmost panel to invoke the parameterless method:

JRuby's objectspace support is disabled by default, due to the performance problems it produces at runtime and, of course, other reasons.

I want to point out some of the interesting details of its implementation, and give some hints on how to start using the Java integration features in JRuby.

Java integration

Once you have integrated Java into your scripts, you can inherit existing Java classes. We just need to do this by specifying the fully qualified name of the Java class. In this example application, the main window inherits the JFrame. The class also contains javax.swing and java.awt packages, so you don't have to use the full name of the class every time.

class MainWindow < javax.swing.JFrame
include_package 'javax.swing'
include_package 'java.awt' ...

As an alternative, you can also use the Include_class feature to include the specified class, so that you don't pollute the namespace because you don't use certain classes.

Invoking the parent class's constructor is just like normal ruby code, which means that we can set the caption of the window by calling super ("JRuby Object Browser") in the first line of the Initialize method.

Because the class contains the entire javax.swing package, instantiating the Java class becomes very straightforward:

list_panel = JPanel.new
list_panel.layout = GridLayout.new(0, 3)

If you take a closer look at the second line, you may feel that we have directly accessed the layout attribute of JPanel, but that is not the case. JRuby adds some handy ways to Java objects, so the above statements can be written in our familiar way:

List_panel.setlayout (gridlayout.new (0, 3))

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.