Jvm - Perl extension for Java VM invocation
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!");
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.
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]#
Initialize JVM.
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);
Invoke static method $method which has the signature $methodSignature of class $class.
return current JVM version.
return value of static member $member of class $class.
set static member of class $class to $value.
This function invokes "System.out.println($obj)" to dump the java object $obj.
The global variables below are optional.
The path(s) where the Java VM searches for java class files
$Jvm::CLASSPATH = "/home/java/classes";
The path(s) where the Java VM searches for JNI libraries
$Jvm::LIBPATH = "/home/java/classes/native";
Ye, Wei w_e_i_y_e@yahoo.com Alain Knaff alain@knaff.lu
Claes Jacobsson (claes@contiller.se) - $Jvm::CLASSPATH and $Jvm::LIBPATH
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.
3 POD Errors
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
'=item' outside of any '=over'
To install jobject, copy and paste the appropriate command in to your terminal.
cpanm
cpanm jobject
CPAN shell
perl -MCPAN -e shell install jobject
For more information on module installation, please visit the detailed CPAN module installation guide.