[Solved-Workaround] OOo ClassLoader interferes with JavaMail

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

[Solved-Workaround] OOo ClassLoader interferes with JavaMail

Post by ptsenter »

Hi,
I have a big Java app, which uses OpenOffice (now at 3.0). So far so good.
OOo forces me to use its own class loader.
Recently, I added JavaMail (Sun's mail.jar 1.4.1) to my app.
If I run the app from within NetBeans 6.1 (which, I believe, bypasses OOo class loader) there is no problems in both debug and release mode.
If I run the app from command line I got

javax.mail.NoSuchProviderException: smtp

If I run JavaMail in debug mode I got

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
java.lang.NoSuchMethodException: com.sun.mail.smtp.SMTPTransport.<init>(javax.ma
il.Session, javax.mail.URLName)


issued prior to a previous message.

If I run an app with JavaMail but without OOo I have no problems whatsoever.
I believe CustomClassLoader prevents JavaMail from finding javamail.providers or anything under meta-inf in mail.jar or anywhere else.

Who has any idea how to proceed?

Thank you.
Last edited by ptsenter on Thu Nov 27, 2008 5:44 am, edited 1 time in total.
hol.sten
Volunteer
Posts: 495
Joined: Mon Oct 08, 2007 1:31 am
Location: Hamburg, Germany

Re: OOo ClassLoader interferes with JavaMail

Post by hol.sten »

ptsenter wrote:OOo forces me to use its own class loader.
Why and how?
ptsenter wrote:Recently, I added JavaMail (Sun's mail.jar 1.4.1) to my app.
...
javax.mail.NoSuchProviderException: smtp
If I remember correctly, I solved this problem by introducing my own DataContentHandlerFactory. Give this thread a try: [Java] Send an email from an OOo Writer document: http://user.services.openoffice.org/en/ ... 13&p=17088. The thread covers a script written in Java for sending a text email from an OOo Writer document and runs as a macro in OpenOffice.org. For sending a mail the script uses the JavaMail API.
OOo 3.2.0 on Ubuntu 10.04 • OOo 3.2.1 on Windows 7 64-bit and MS Windows XP
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Found Workaround]OOo ClassLoader interferes with JavaMail

Post by ptsenter »

First, to answer your questions:
why - have no idea, never bothered to find out;
how - a year ago (that's when I incorporated OOo into my app) I found a set of classes, which included OOo custom loader, and intructions how to update Java app's manifest to use this loader to run my main class; it worked and I never looked back. Today one can install OOo plug-in for NetBeans, which will do exactly the same - it generates custom loader.

The problem you solved is not the same - you run Writer and from within Writer you call Java macro which happens to use JavaMail. BTW, I liked the way you attached Java debugger to OOo.

My problem - my app is written in Java, which happened to use Calc to read-write Calc documents. At the same time and totally independent of OOo my app trying to use JavaMail.

After farther debugging I believe that custom loader is still the culprit:
now I know javamail.providers is found and read properly.
But I used a statement

Transport.send(msg);

in my program, which supposed to implicitly instantiate SMTPTransport, establish connection to my server and issue SMTPTransport.sendMsg(msg, recipients). The key is message

java.lang.NoSuchMethodException: com.sun.mail.smtp.SMTPTransport.<init>(javax.ma
il.Session, javax.mail.URLName).

Apparently, Transport found some SMTPTransport, but a wrong one thanks to custom loader, because it could not find proper constructor.
Instead, I've done everything above myself explicitly and it works.
My program now is less flexible, but a deadline is looming and I'm not complaining much any more.

Thanx anyway for attaching a remote debugger.
ksk_tom
Posts: 2
Joined: Wed Apr 29, 2009 3:28 pm

Re: [Solved-Workaround] OOo ClassLoader interferes with JavaMail

Post by ksk_tom »

Hi there,
Apparently, Transport found some SMTPTransport, but a wrong one thanks to custom loader, because it could not find proper constructor.
Instead, I've done everything above myself explicitly and it works.
My program now is less flexible, but a deadline is looming and I'm not complaining much any more.
i have a similar problem with OO and javamail (1.4.1).

I tried to use Store.getStore("imap") and receive a similar NoSuchMethodException.

@ptsenter (or anybody else): Can you explain, what you mean with "done everything above myself explicitly and it works..."?
I have no idea how to load an IMAPStore explicitly.

thanks for your help

tom
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Solved-Workaround] OOo ClassLoader interferes with JavaMail

Post by ptsenter »

Hi,
I don't know about IMAPStore, but this is what I did in my case:

instead of

javax.mail.Transport.send(msg);

I put this code

URLName urlName = new URLName("myurl");
SMTPTransport smtpTrans = new SMTPTransport(session, urlName);
Socket socket = new Socket("myurl", 25);
smtpTrans.connect(socket);
smtpTrans.sendMessage(msg, InternetAddress.parse(to, false));


which I call "explicit instantiation of SMTPTransport". I guess similar steps can be taken for IMAPStore - check documentation.

From your message I'm not sure if you're using custom OOo loader generated by a wizard in NetBeans. If you do there is a better solution - you can do one of the next:

- first, you can manually fix generated Loader.java - insert

Thread.currentThread().setContextClassLoader(cl);

after line 140

ClassLoader cl = getCustomLoader();

- second, this is a better one in case you need to re-generate custom loader, insert

Thread.currentThread().setContextClassLoader(yourclassname.class.getClassLoader());

at the beginning of main method of your top class yourclassname.

Enjoy.
ptsenter
Posts: 22
Joined: Thu Feb 21, 2008 9:02 pm

Re: [Solved-Workaround] OOo ClassLoader interferes with JavaMail

Post by ptsenter »

I just checked documentation and I really don't see static getStore method in Store class, but there is such instance method in Session class, which is used in all examples.
I don't understand how such construct got even compiled.
ksk_tom wrote:Hi there,
Apparently, Transport found some SMTPTransport, but a wrong one thanks to custom loader, because it could not find proper constructor.
Instead, I've done everything above myself explicitly and it works.
My program now is less flexible, but a deadline is looming and I'm not complaining much any more.
i have a similar problem with OO and javamail (1.4.1).

I tried to use Store.getStore("imap") and receive a similar NoSuchMethodException.

@ptsenter (or anybody else): Can you explain, what you mean with "done everything above myself explicitly and it works..."?
I have no idea how to load an IMAPStore explicitly.

thanks for your help

tom
ksk_tom
Posts: 2
Joined: Wed Apr 29, 2009 3:28 pm

Re: [Solved-Workaround] OOo ClassLoader interferes with JavaMail

Post by ksk_tom »

Thanks for your help ptsenter, that line works for me

Code: Select all

Thread.currentThread().setContextClassLoader(yourclassname.class.getClassLoader());
...and sorry for confusion if i wrote

Code: Select all

Store.getStore("imap")
...that happens if fingers faster then my brain :oops:
Here the full snippet:

Code: Select all

   session = Session.getDefaultInstance(props, null);  
// Get the store
    Provider imapProvider = session.getProvider("imap");
    store = session.getStore(imapProvider);
OOo 2.3.X on Ms Windows XP + Ubuntu 9.04
Post Reply