Re: New to Inline::Java and a quick question

[email protected] (Russ Tremain) Fri, 13 Dec 2013 12:29:50 -0800
Newsgroups perl.inline
Message-ID <p06240807ced11c677577@[10.0.100.199]>
you should also always check for exceptions as 
per my previousl example.  i.e., in your 
perlAPI.pm:

sub new {

     my $self = {};
     bless($self, $class);
     
     eval {
         $self->{CONNECTION} = new perlAPI::MyJAXB();
     };

     if ($@){
         if (Inline::Java::caught("java.lang.Exception")) {
             my $msg = $@->getMessage() ;
             printf "PERL CAUGHT java exception: '%s'\n", $msg;
         }
         else{
             #unexpected Inline::Java exception:
             printf "PERL CAUGHT UNEXPECTED EXCEPTION: '%s'\n", $@;
         }

         return undef;
     }  

     return $self;
}

your current code is ignoring the exception silently.

Maybe Patrick can help on why Inline::Java is not 
catching the java exception, but at any rate, the 
error from eval is the same as the one from java.

-Russ

At 12:12 PM -0800 12/13/13, David Wang wrote:
>Hi Sorry for the spam. I found the solution.
>
>In Perl, we need to add the class loader, can't 
>use the default one. Therefore, to make the long 
>story short, the code where is called in 
>perlAPI.pm should look like the following.
>
>     ClassLoader cl = sample.ObjectFactory.class.getClassLoader();
>
>     System.out.println("============================> checkpoint1");
>     JAXBContext context = JAXBContext.newInstance("sample", cl);
>     System.out.println("==============================>checkpoint2");
>
>
>
>
>
>From: David Wang <[email protected]>
>To: Patrick LeBoutillier <[email protected]>
>Cc: Russ Tremain <[email protected]>; "[email protected]" <[email protected]>
>Sent: Friday, December 13, 2013 11:28 AM
>Subject: Re: New to Inline::Java and a quick question
>
>
>Hi Patrick and the rest
>
>I made a very simple example. This example shows 
>how a Main.java calls the JAXB stuff and the 
>same calls in Perl script failed.
>
>Once you download the file, untar, to compile and run in Java, you do
>javac -d classes Main.java
>java -cp .:classes Main
>
>To run in perl
>perl main.pl
>
>You will see that the following line always fails in perl .
>JAXBContext context = JAXBContext.newInstance("sample");
>
>JAXB is a very powerful utility in Java that 
>saves lots of coding to parse XML (you basically 
>don't need to write any code to parse, its all 
>done automatically). I have way too many 
>existing perl scripts that can take the 
>advantage of JAXB if this would work.
>
>Thanks for your help.
>
>David
>
>
>From: Patrick LeBoutillier <[email protected]>
>To: David Wang <[email protected]>
>Cc: Russ Tremain <[email protected]>; "[email protected]" <[email protected]>
>Sent: Friday, December 13, 2013 3:45 AM
>Subject: Re: New to Inline::Java and a quick question
>
>
>Hi,
>
>If I run this:
>
>use strict ;
>
>use Inline (
>     Java => 'DATA',
>     DEBUG => 0,
>     AUTOSTUDY => 1,
>);
>
>
>my $t = new MyJAXB() ;
>
>
>__DATA__
>__Java__
>import java.io.BufferedReader;
>import java.io.File;
>import java.io.FileInputStream;
>import java.io.FileNotFoundException;
>import java.io.FileReader;
>import java.io.IOException;
>import java.math.BigInteger;
>
>import javax.xml.bind.JAXBContext;
>import javax.xml.bind.JAXBException;
>import javax.xml.bind.Unmarshaller;
>import javax.xml.parsers.DocumentBuilder;
>import javax.xml.parsers.DocumentBuilderFactory;
>import org.w3c.dom.Document;
>import org.w3c.dom.NamedNodeMap;
>import org.w3c.dom.Node;
>import org.w3c.dom.NodeList;
>import org.xml.sax.SAXException;
>
>import java.util.*;
>
>public class MyJAXB {
>   public MyJAXB() throws javax.xml.bind.JAXBException,
>       FileNotFoundException, SAXException
>   {
>     System.out.println("checkpoint1");
>     JAXBContext context = 
>JAXBContext.newInstance("com.example.foo:com.example.bar");
>     System.out.println("checkpoint2");
>
>   }
>}
>
>I get this:
>
>checkpoint1
>Unexpected exception of type 
>'javax.xml.bind.JAXBException': 
>"com.example.foo" doesnt contain 
>ObjectFactory.class or jaxb.index at 
>/usr/local/lib64/perl5/site_perl/5.10.0/x86_64-linux-thread-multi/Inline/Java/Object.pm 
>line 49
>  at <http://t.pl/>t.pl line 10
>  at <http://t.pl/>t.pl line 10
>
>Don't know enough about JAXB to continue...
>
>Patrick
>
>
>
>
>
>On Fri, Dec 13, 2013 at 2:06 AM, David Wang 
><<mailto:[email protected]>[email protected]> 
>wrote:
>
>Hi Russ
>
>Thanks. Pls do the following
>
>In your perlAPI.pm add the following (in the same file).
>
>==> perl script calls
>
>eval {$self->{CONNECTION} = new perlAPI::MyJAXB();};
>
>==> The following is the Java JAXB stuff
>__DATA__
>__Java__
>import java.io.BufferedReader;
>import java.io.File;
>import java.io.FileInputStream;
>import java.io.FileNotFoundException;
>import java.io.FileReader;
>import java.io.IOException;
>import java.math.BigInteger;
>
>import javax.xml.bind.JAXBContext;
>import javax.xml.bind.JAXBException;
>import javax.xml.bind.Unmarshaller;
>import javax.xml.parsers.DocumentBuilder;
>import javax.xml.parsers.DocumentBuilderFactory;
>import org.w3c.dom.Document;
>import org.w3c.dom.NamedNodeMap;
>import org.w3c.dom.Node;
>import org.w3c.dom.NodeList;
>import org.xml.sax.SAXException;
>
>import java.util.*;
>
>public class MyJAXB {
>   public MyJAXB() throws javax.xml.bind.JAXBException,
>       FileNotFoundException, SAXException
>   {
>     System.out.println("checkpoint1");
>     JAXBContext context = 
>JAXBContext.newInstance("com.example.foo:com.example.bar");
>     System.out.println("checkpoint2");
>
>   }
>}
>
>
>  "checkpoint2" will never be reached. If you do 
>the DEBUG =>4, you will see the error
>
>>>[perl][3]   perl doesn't know about 
>>>'javax.xml.bind.JAXBException' ('perlAPI::javax::xml::bind::JAXBException')
>>>
>
>use Inline (
>     Java => 'DATA',
>     J2SDK => $ENV{JAVA_HOME},
>     CLASSPATH => 'classes',
>
>     DEBUG => 4,
>);
>
>Pls let me know how it goes. I really hope its 
>something I did wrong meaning it works on your 
>end.
>
>
>From: Russ Tremain <<mailto:[email protected]>[email protected]>
>To: David Wang <<mailto:[email protected]>[email protected]>
>
>Cc: "<mailto:[email protected]>[email protected]" 
><<mailto:[email protected]>[email protected]>
>Sent: Thursday, December 12, 2013 7:32 PM
>
>Subject: Re: New to Inline::Java and a quick question
>
>
>can you publish your full example?  I can try it 
>my environment to see if it works for me.
>
>Also, when you installed inline::Java, did it pass all the tests?
>
>At 5:18 PM -0800 12/12/13, David Wang wrote:
>
>>Thanks. my perl version is v5.8.8 . It works 
>>with Java fine. Just not with JAXB at this 
>>current
>>
>moment.
>
>
>
>From: Russ Tremain <<mailto:[email protected]>[email protected]>
>To: David Wang <<mailto:[email protected]>[email protected]>
>Cc: David Mertens 
><<mailto:[email protected]>[email protected]>; 
>"<mailto:[email protected]>[email protected]" 
><<mailto:[email protected]>[email protected]>
>Sent: Thursday, December 12, 2013 5:05 PM
>Subject: Re: New to Inline::Java and a quick question
>
>
>#yiv3571647833 #yiv3571647833 -- blockquote, 
>#yiv3571647833 dl, #yiv3571647833 ul, 
>#yiv3571647833 ol, #yiv3571647833 li 
>{padding-top:0;padding-bottom:0;} #yiv3571647833 
>Re: New to Inline::Java and a quick question
>
>one thing I can tell you is I have never gotten 
>Inline::Java to work with any perl later than 
>5.8.9.
>
>I have used it extensively with JDBC.pm.  Example of this can be found here:
>
>
>         <https://github.com/russt/sqlpj>https://github.com/russt/sqlpj
>
>
>best of luck!
>
>-Russ
>
>
>
>At 4:57 PM -0800 12/12/13, David Wang wrote:
>
>>Thanks
>>
>
>I am not sure if anybody has any experience with 
>perl Inline::Java to call Java JAXB . Basically, 
>I have added some code in my perl script to call 
>some of my Java API that calls JAXB.
>
>
>System.out.println("before·");
>
>JAXBContext context = JAXBContext.newInstance("SomeXMLPackage");
>
>System.out.println("after·");
>
>
>and it fails on the JAXB statement above. I then 
>added Debug and AUTOSTUDY  option in perl like 
>below
>
>
>use Inline (
>
>     Java => 'DATA',
>
>     J2SDK => $ENV{JAVA_HOME},
>
>     CLASSPATH => 'classes',
>
>     AUTOSTUDY => 1,
>
>     DEBUG => 4,
>
>);
>
>
>then I see
>
>[java][3]   packet sent is ok java_object:1:1:javax.xml.bind.JAXBException
>
>[perl][3]   packet recv is ok java_object:1:1:javax.xml.bind.JAXBException
>
>[perl][3]   checking if stub is array...
>
>[perl][3]   perl doesn't know about 
>'javax.xml.bind.JAXBException' 
>('perlAPI::javax::xml::bind::JAXBException')
>
>[perl][2]  autostudying javax.xml.bind.JAXBException...
>
>[perl][3]   perl doesn't know about 
>'javax.xml.bind.JAXBException' 
>('perlAPI::javax::xml::bind::JAXBException')
>
>[perl][3]   reporting on javax.xml.bind.JAXBException
>
>
>I hope perl inline works with JAXB . BTW, my 
>Java API works fine meaning I use a Java main to 
>call this
>
>API and the xml file is loaded without problem.
>
>
>thanks,
>
>
>David
>
>
>
>
>
>
>From: David Mertens 
><<mailto:[email protected]>[email protected]>
>To: David Wang <<mailto:[email protected]>[email protected]>
>Cc: "<mailto:[email protected]>[email protected]" 
><<mailto:[email protected]>[email protected]>
>Sent: Thursday, December 12, 2013 5:37 AM
>Subject: Re: New to Inline::Java and a quick question
>
>
>Yep, as a rule, if you need to install a Perl 
>module, a simple "cpan Module::Name" should do 
>it. If you are on a Unixish system, you may have 
>installation permission issues, in which case 
>there are known work-arounds. But I'm sure 
>you'll ask when you come to those. :-)
>
>Good luck, and don't be afraid to ask more questions as they come up!
>
>David
>
>
>
>On Wed, Dec 11, 2013 at 7:38 PM, David Wang 
><<mailto:[email protected]>[email protected]> 
>wrote:
>
>Ok, I figured it out. Hope it can be documented somewhere.
>
>
>Download Inline-0.53
>
>go to the dir
>
>cpan Inline::Java  << was instructed by some nice guy
>
>
>Thanks
>
>
>David
>
>
>
>
>From: David Wang <<mailto:[email protected]>[email protected]>
>To: "<mailto:[email protected]>[email protected]" 
><<mailto:[email protected]>[email protected]>
>Sent: Wednesday, December 11, 2013 4:25 PM
>Subject: New to Inline::Java and a quick question
>
>
>Hi
>
>
>I am very new to this stuff, just started to 
>hear about it today. I looked around the web and 
>I find the installation
>
>instructions for inline::c like the following
>
>
>perl Makefile.PL;
>
>make;
>
>make test;
>
>make install;
>
>
>But I can't find anywhere else that tells us how 
>to install inline::Java ? BTW, I found the 
>latest Inline is Inline-0.53.tar.gz  and I use 
>Redhat 5
>
>
>Thanks
>
>
>David
>
>
>
>
>
>
>
>
>
>--
>  "Debugging is twice as hard as writing the code in the first place.
>   Therefore, if you write the code as cleverly as possible, you are,
>   by definition, not smart enough to debug it." -- Brian Kernighan
>
>
>
>
>
>
>
>--
>=====================
>Patrick LeBoutillier
>Rosemère, Québec, Canada