Hello,
I currently have a need to be able to import classes dyncamically. Now one
of these classes contains Java and uses Inline::Java. I've not yet
successfully found a way to do this.
Using an example module as follow:
package Foo;
use strict;
use warnings;
# Setup Inline Java
use Inline Java => "DATA",
AUTOSTUDY => 1,
SHARED_JVM => 1,
START_JVM => 0,
DEBUG => 1,
DIRECTORY => '/tmp',
EXTRA_JAVAC_ARGS => '-client';
sub new {
my $class = shift;
return bless { JAVA_OBJECT => Foo::Bar->new()} , $class;
}
sub jvm_version {
my ( $self ) = @_;
return $self->{JAVA_OBJECT}->version();
}
1;
__DATA__
__Java__
public class Bar {
public String version() {
return System.getProperty("java.version") ;
}
}
__END__
Here is the output of attempts to use this module in a number of ways.
perl -MFoo -e '$foo = new Foo(); print $foo->jvm_version() . "\n";'
[perl][1] validate done.
[perl][1] Starting load.
[perl][1] starting JVM...
[perl][1] client/server mode
[perl][1] connected to already running JVM!
[perl][1] using jdat cache
[perl][1] load done.
1.5.0_15
[perl][1] killed by natural death.
[perl][1] JVM non-owner exiting...
[perl][1] exiting with 0
perl -e ' BEGIN { require "Foo.pm"; } $foo = new Foo; print
$foo->jvm_version() . "\n";'
[perl][1] validate done.
[perl][1] Starting load.
[perl][1] starting JVM...
[perl][1] client/server mode
[perl][1] connected to already running JVM!
[perl][1] using jdat cache
[perl][1] load done.
1.5.0_15
[perl][1] killed by natural death.
[perl][1] JVM non-owner exiting...
[perl][1] exiting with 0
perl -MUNIVERSAL::require -e '$class = "Foo"; $class->require(); $foo =
new Foo(); print $foo->jvm_version() . "\n";'
Can't locate object method "new" via package "Foo::Bar" (perhaps you
forgot to load "Foo::Bar"?) at /Network/Users/mcollard/lib/Foo.pm line 22.
One or more DATA sections were not processed by Inline.
perl -e '$class = "Foo"; eval "use $class"; $foo = new Foo(); print
$foo->jvm_version() . "\n";'
Can't locate object method "new" via package "Foo::Bar" (perhaps you
forgot to load "Foo::Bar"?) at /Network/Users/mcollard/lib/Foo.pm line 22.
One or more DATA sections were not processed by Inline.
perl -e ' require "Foo.pm"; $foo = new Foo; print $foo->jvm_version() .
"\n";'
Can't locate object method "new" via package "Foo::Bar" (perhaps you
forgot to load "Foo::Bar"?) at /Network/Users/mcollard/lib/Foo.pm line 22.
One or more DATA sections were not processed by Inline.
So from this, whenever I try to use or require the Foo module at any point
after BEGIN, it seems to fail. Notably, I also get no output on stdout
about connecting to a JVM etc.
Can anyone point me in the right direction or suggest another avenue that
may suit the needs of having a batch process which dynamically loads
classes depending on a number of factors. I know it's vague description :)
Regards
-- Michael Collard