Cheap web site hosting service by

 Back to Index

2.4 Calling Java from Perl

Next, let's look at how to invoke Java from Perl.

2.4.1 Java in Perl in Java

Remember the issues from 2.1.2 - this is unstable unless you are calling Java from Perl methods that are themselves embedded in a Java program.

2.4.2 Java in Perl: Simple Constructors

Use JPL::Class to load the class:

use JPL::Class "java::awt::Frame";

Invoke the constructor to create an instance of the class:

my $f = java::awt::Frame-new;>

You've got a reference to a Java object in $f, a Perl scalar. I think this is cool.

2.4.3 Constructors that Take Parameters

If the constructor has parameters, look up the method signature with getmeth:

my $new = getmeth("new", ['java.lang.String'], []);

The first argument to getmeth is the name of the method. The second argument is a reference to an array that contains a list of the argument types. The final argument to getmeth is a reference to an array containing a single element with the return type. Constructors always have a null (void) return type, even though they return an instance of an object.

Invoke the method through the variable you created:

my $f = java::awt::Frame->$new( "Frame Demo" );

Because Java supports method overloading, the only way Java can distinguish between different methods that have the same name is through the method signature. The getmeth function simply returns a mangled, Perl-friendly version of the signature. JPL's AutoLoader takes care of finding the right class.

For example, the method signature for $new is (Ljava/lang/String;)V. In Perl, this is translated to new__Ljava_lang_String_2__V. Sure, it means something to Java, but thanks to getmeth and JPL's AutoLoader, we don't have to worry about it!

2.4.4 More on getmeth

The getmeth function is not just for constructors. You'll use it to look up method signatures for any method that takes arguments.

To use getmeth, just supply the Java names of the types and objects in the argument or return value list. Here are a few examples:

  • Two int arguments, void return type:

        $setSize = getmeth("setSize", ['int', 'int'], []);  

  • One argument (java.awt.Component), with a return type of the same:

        $add = getmeth("add", ['java.awt.Component'],

  • Two arguments, a String object and a boolean value, and a void return type:

        $new = getmeth("new",
        ['java.lang.String', 'boolean'], []);  

  • A String argument with a java.lang.Class return type:

        $forName = getmeth("forName",

  • No arguments, but a boolean return value:

        $next = getmeth("next", [], ['boolean']);  

2.4.5 Instance Variables

Java instance variables that belong to a class can be reached through $self and a method with the same name as the instance variables:

    $frame->$setSize( $self->width, $self->height );  

Here is an example:

    class VarDemo {

    int foo = 100;

    perl int perlChange() {{
        my $current_value = $self->foo;

        # Change foo to ten times itself.

        $self->foo( $current_value * 10 );


    void executeChange() {



    public static void main(String[] args) {

        VarDemo demo = new VarDemo();



Note. JPL creates these methods with the same name as the variable. You can also supply a value to set the variable's value. If you create a method with this name, it will collide with the one that JPL defines.


    * FrameDemo - create and show a Frame in Perl.

    public class FrameDemo {

        int height = 50;
        int width = 200;
        perl void make_frame () {{

        # Import two Java classes.

        use JPL::Class "java::awt::Frame";
        use JPL::Class "java::awt::Button";

        # Create a Frame and a Button. The two calls to new()
        # have the same signature.

        my $new = getmeth("new", ['java.lang.String'], []);
        my $frame = java::awt::Frame->$new( "Frame Demo" );
        my $btn = java::awt::Button->$new( "Do Not Press Me" );

        # Add the button to the frame.

        my $add = getmeth("add", ['java.awt.Component'],
        $frame->$add( $btn );

        # Set the size of the frame and show it.

        my $setSize = getmeth("setSize", ['int', 'int'], []);
        $frame->$setSize($self->width, $self->height);


    public static void main(String[] argv) {

        FrameDemo demo = new FrameDemo();



2.4.6 Summary: Calling Java from Perl

  1. Use JPL::Class to specify a Java class to import.
  2. You can directly invoke constructors and methods that take no arguments.
  3. If the constructor or method takes arguments, use getmeth to look up its signature.
  4. Use $self to access Java instance variables and methods.




Cheap domain name:
Domain name services from just
$8.95/year only

Buy domain name registration and cheap domain transfer at low, affordable price.

2002-2004 Web Site Hosting Service


Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to