Alain Knaff > Jvm > Jvm

Download:
Jvm-0.9.3.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.9.3   Source  

NAME ^

Jvm - Perl extension for Java VM invocation

SYNOPSIS ^

  use Jvm;

  # Initialize the JVM
  new Jvm();

  ###################################
  #invoke static method of Java class
  ###################################
  #Equivalent Java code:
  #   Thread.sleep(1000);
  Jvm::call("java.lang.Thread", "sleep", "(J)V", 1000);

  ##########################
  #Java instance manipulate
  ##########################
  #Equivalent Java code:
  # Integer obj = new Integer(99);
  # String s = i.toString();
  $obj = new Jvm("java.lang.Integer", "(I)V", 99);
  $s = $obj->toString("()Ljava/lang/String;");

  #######################
  #get/set static member
  #######################
  #Equivalent Java code:
  # System.out.println("Hello world!");
  $out = Jvm::getProperty("java.lang.System", "out", "Ljava/io/PrintStream;");
  $out->println("(Ljava/lang/String;)V", "Hello world!");

DESCRIPTION ^

This package allows you to invoke Java API in Perl. You can invoke java methods of the standard Java classes as well as your own Java program.

Java Signature ^

You have to specify Java method signature when call Java API. This is because a java class may have more than 1 methods which share the same method name. Consider the following example:

    public class Foo {
        public static void test(int i) {}
        public static void test(byte b) {}
    };

Foo class has 2 methods which share the same method name test. You have to use method signature to specify which method you are going to call. Here is a sample to invoke them respectively:

 Jvm::call("Foo","test","(I)V", 1234567);   #(I)V: means input 'Integer', output 'Void'
 Jvm::call("Foo","test","(B)V", 22);        #(B)V: means input 'Byte', output 'Void'

Java Signature rule is simple, mapping table between signature and method is available at http://java.sun.com/j2se/1.3/docs/guide/jni/spec/types.doc.html#16432

If you don't want to learn the signature mapping table, you can use 'javap' tool comes with JDK to print out all the signatures in your class, usage is

 javap -s Your_java_class

Here is an example:

  [root@yw Jvm]# javac Foo.java
  [root@yw Jvm]# javap -s Foo
  Compiled from Foo.java
  public class Foo extends java.lang.Object {
      public static void test(int);
        /*   (I)V   */
      public static void test(byte);
        /*   (B)V   */
      public Foo();
        /*   ()V   */
  }
  [root@yw Jvm]# 

Function List ^

new Jvm();

Initialize JVM.

$obj = new Jvm($class, $constructorMethodSig, @args);

create a Java object, whose class name is $class, constructor has $constructorMethodSig signature, and @args are arguments for constructor. Then later you can invoke method XXX of this instance: $result = $obj->XXX($methodSignature, @args);

$ret = call($class, $method, $methodSignature, @args);

Invoke static method $method which has the signature $methodSignature of class $class.

$ver = getVersion();

return current JVM version.

$value = getProperty($class, $member, $memberSignature);

return value of static member $member of class $class.

setProperty($class, $member, $memberSignature, $value);

set static member of class $class to $value.

dump($obj)

This function invokes "System.out.println($obj)" to dump the java object $obj.

Global variables ^

The global variables below are optional.

CLASSPATH

The path(s) where the Java VM searches for java class files

$Jvm::CLASSPATH = "/home/java/classes";

LIBPATH

The path(s) where the Java VM searches for JNI libraries

$Jvm::LIBPATH = "/home/java/classes/native";

AUTHOR ^

Ye, Wei w_e_i_y_e@yahoo.com Alain Knaff alain@knaff.lu

CREDITS ^

Claes Jacobsson (claes@contiller.se) - $Jvm::CLASSPATH and $Jvm::LIBPATH

SEE ALSO ^

perl(1).

Java JNI Specification http://java.sun.com/j2se/1.3/docs/guide/jni/

JPL JPL is a package, which allows you to invoke Java in Perl as well as embed Perl in java. It's bundled with Perl5.6, you can get it at: http://users.ids.net/~bjepson/jpl/cvs.html Compare to Jvm, it's more complex.

syntax highlighting: