blue_cowdawg has asked for the wisdom of the Perl Monks concerning the following question:

I have a jar file that I have put a slew of class files in with one of those being a singleton class that does all sorts of magical things for me.

I try to instantiate that singleton from within JavaInline by doing something rather simple (sort of) as a proof of concept. So here's some code:

#!/usr/bin/perl -w use Inline Java => << 'End_Of_Java_Code'; import org.bcdc.hbm.services.*; class FooBar { public PeopleService getPersonService() { return PeopleService.getInstance(); } } End_Of_Java_Code use Data::Dumper; my $foo = new FooBar(); $foo -> setThing("This is thing!"); $foo -> setOtherThing(17); my $psvc = $foo->getPersonService(); printf "%s\n",$psvc->getPersonNameFromID(1);
What I'm seeing instead of the person's name in the database who's person_id is 1 is:
main::java::lang::NoClassDefFoundError=HASH(0x8f530e4)

It would appear the singleton is not being instantiated. Any ideas folks?


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Replies are listed 'Best First'.
Re: Inline::Java and jar files?
by mpeters (Chaplain) on May 01, 2007 at 01:07 UTC
    Make sure your .jar file is found in a directory listed in your CLASSPATH variable. It's sort of like the PERL5LIB var for Perl as a mechanism to extend @INC.

    -- More people are killed every year by pigs than by sharks, which shows you how good we are at evaluating risk. -- Bruce Schneier
          Make sure your .jar file is found in a directory listed in your CLASSPATH variable.

      Hmmm... actually CLASSPATH points directly at the jar file... Changing to point to the directory the jar sits in did not change the misbehavior of the script...


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
        Not the CLASSPATH environment variable. The one you tell Inline::Java aboout:
        use Inline Java => <<'END', CLASSPATH => '/path/to/my.jar'; /* java code here */ END
        Phil
        The Gantry Web Framework Book is now available.
        Then org.bcdc.hbm.services doesn't exist, or doesn't provide PeopleService ....
SOLUTION (was Re: Inline::Java and jar files?)
by blue_cowdawg (Monsignor) on May 01, 2007 at 22:43 UTC

    A couple of emails later to the folks at inline@perl.org and some poking around of my own I found the solution. Part A is to make sure the class path gets set correctly and includes not only the jar that I want to use but anything the jar references.

    Secondly, just because I've got the class path correct does not mean that it will totally understand the object that you are calling in. Use the study export from Inline::Java to cause it to "study" the object and bind it to Perl.

    The end result code looks like:

    #!/usr/bin/perl -w BEGIN { my $libdir='/home/pberghol/src/www.bayshoredogclub.org/WebRoot/WEB +-INF/lib'; my @jars=(); opendir LIBDIR,$libdir or die $1; while( my $fname = readdir(LIBDIR)){ next unless $fname =~ m@\.jar$@; push @jars,$libdir . "/" . $fname; } $ENV{CLASSPATH}=join(":",@jars,"/home/pberghol/tmp/bcdchbm.jar"); } use Inline Java => << 'End_Of_Java_Code' ,CLASSPATH=> $ENV{CLASSPATH}; import org.bcdc.hbm.services.PeopleService; class FooBar { public FooBar(){ } public PeopleService getPersonService() { return PeopleService.getInstance(); } } End_Of_Java_Code use Inline::Java qw(study_classes); use Data::Dumper; study_classes(['org.bcdc.hbm.services.PeopleService']); my $foo = new FooBar(); my $psvc = $foo->getPersonService(); print Dumper($psvc); printf "%s\n",$psvc->getPersonNameFromID(1); print Dumper($foo);
    Now if I could only remember why I started down this path... Eh... need more coffee.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg