Discussion:
[Classpathx-javamail] clueless newbie -- how to open mbox?
Matej Cepl
2007-05-02 12:48:03 UTC
Permalink
Hi, can somebody help me with this:

[***@chelcicky mdir2mbox]$ cat -b Mdir2mbox.java
1 import java.util.*;
2 import javax.mail.*;

3 public class Mdir2mbox {
4 public static void main(String[] args) throws \
MessagingException {
5 String urlStr = "/home/matej/.eclipse\
/workspace/Mdir2mbox/examples/test";
6 Properties properties = new Properties();
7 Session session = Session.getInstance(properties);
8 URLName mboxURL = new URLName("mbox://"+urlStr);
9 Store store = session.getStore(mboxURL);
10 Folder folder = store.getDefaultFolder();
11 if(!folder.exists())
12 folder.create(Folder.HOLDS_MESSAGES);
13 folder.open(Folder.READ_WRITE);
14 System.out.println(folder.getMessageCount());
15 }

16 }
[***@chelcicky mdir2mbox]$ ls -lh examples/test
-rw-r--r-- 1 matej matej 22K kvě 1 01:02 examples/test
[***@chelcicky mdir2mbox]$ java Mdir2mbox
Exception in thread "main" java.lang.NullPointerException
at Mdir2mbox.main(Mdir2mbox.java:12)
[***@chelcicky mdir2mbox]$ rpm -q classpathx-mail
classpathx-mail-1.1.1-4jpp.2
[***@chelcicky mdir2mbox]$ java -version
java version "1.4.2"
gij (GNU libgcj) version 4.1.1 20070105 (Red Hat 4.1.1-51)

Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[***@chelcicky mdir2mbox]$

Whatever I do I don't see what I am doing wrong in this case. There is not
much examples on how to use java.mail with folders, but when I look at this
(among others)
http://article.gmane.org/gmane.comp.java.classpath.extensions.javamail/116 it
seems to me, that I am not doing anything extremely silly (it's Fedora Core 6).

Is there any hope for me, please?

Thanks a lot,

Matěj Cepl
Chris Burdess
2007-05-02 13:11:25 UTC
Permalink
Post by Matej Cepl
1 import java.util.*;
2 import javax.mail.*;
3 public class Mdir2mbox {
4 public static void main(String[] args) throws \
MessagingException {
5 String urlStr = "/home/matej/.eclipse\
/workspace/Mdir2mbox/examples/test";
6 Properties properties = new Properties();
7 Session session = Session.getInstance
(properties);
8 URLName mboxURL = new URLName
("mbox://"+urlStr);
9 Store store = session.getStore(mboxURL);
10 Folder folder = store.getDefaultFolder();
11 if(!folder.exists())
12 folder.create(Folder.HOLDS_MESSAGES);
13 folder.open(Folder.READ_WRITE);
14 System.out.println(folder.getMessageCount());
15 }
16 }
-rw-r--r-- 1 matej matej 22K kvě 1 01:02 examples/test
Exception in thread "main" java.lang.NullPointerException
at Mdir2mbox.main(Mdir2mbox.java:12)
The mbox provider is not really designed to work like that: you
initialise it with a URL to a directory, and call getFolder to get
individual mailboxes.

I don't really understand how you can have an NPE at line 12 if you
don't have one at line 11, but still.

Try it with something like

Store store = session.getStore(new URLName("mbox:///home/
matej/.eclipse/workspace/Mdir2mbox/examples/"));
Folder test = store.getFolder("test");
test.open(Folder.READ_WRITE);

If you think the current behaviour is wrong, let me know how you
think it should work and I'll see what I can do about it.
Matej Cepl
2007-05-02 18:39:36 UTC
Permalink
Post by Chris Burdess
Try it with something like
Store store = session.getStore(new URLName("mbox:///home/
matej/.eclipse/workspace/Mdir2mbox/examples/"));
Folder test = store.getFolder("test");
test.open(Folder.READ_WRITE);
If you think the current behaviour is wrong, let me know how you
think it should work and I'll see what I can do about it. =
OK, I got your point (and you are probably right, I just hoped it
actually works this way a minute I begun to think about set of
folders), but still no luck. However, I have a different
exception now ;-):

[***@hubmaier kmail2tbfolders]$ gcj --version
gcj (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[***@hubmaier kmail2tbfolders]$ gcj -C test.java
[***@hubmaier kmail2tbfolders]$ gij test
Exception in thread "main" javax.mail.MessagingException: Folder is read-only
at gnu.mail.providers.mbox.MboxFolder.open\
(classpathx-mail-1.3.1-monolithic-1.1.1.jar.so)
at test.main(test.java:12)
[***@hubmaier kmail2tbfolders]$ cat -b test.java
1 import java.util.*;
2 import javax.mail.*;

3 public class test {
4 public static void main(String[] args) \
throws MessagingException {
5 String urlStr = "/home/matej/.eclipse\
/workspace/kmail2tbfolders/examples/";
6 Properties properties = new Properties();
7 Session session = Session.getInstance(properties);
8 URLName storeURL = new URLName("mbox://"+urlStr);
9 Store store = session.getStore(storeURL);
10 Folder test = store.getFolder("test");
11 test.open(Folder.READ_WRITE);
12 }
13 }
[***@hubmaier kmail2tbfolders]$ gij --version
java version "1.4.2"
gij (GNU libgcj) version 4.1.1 20070105 (Red Hat 4.1.1-51)

Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[***@hubmaier kmail2tbfolders]$ ls -lh /home/matej/.eclipse\
/workspace/kmail2tbfolders/examples/test -rw-rw-rw- 1 matej matej
2,0M kvě 2 20:23
/home/matej/.eclipse/workspace/kmail2tbfolders/examples/test
[***@hubmaier kmail2tbfolders]$

Any ideas? BTW, is there an equivalent of bash command
readlink -f (it would be very helpful)?

Best,

Matěj
Matej Cepl
2007-05-02 18:51:43 UTC
Permalink
Post by Matej Cepl
OK, I got your point (and you are probably right, I just hoped
it actually works this way a minute I begun to think about set
of folders), but still no luck. However, I have a different
When (following http://tinyurl.com/ysqmfx ) I tried other
variant, I got back to the square one:

[***@hubmaier kmail2tbfolders]$ cat -b test.java
1 import java.util.*;
2 import javax.mail.*;

3 public class test {
4 public static void main(String[] args) throws
MessagingException {
5 String urlStr
= ``/home/matej/.eclipse/workspace\
/kmail2tbfolders/examples/``';
6 Session session = Session.getInstance(new
Properties());
7 URLName storeURL = new
URLName(``'mbox://``'+urlStr);
8 Store store
= session.getStore(storeURL);
9 Folder test
= store.getDefaultFolder();
10 test = test.getFolder(``'test``');
11 test.open(Folder.READ_WRITE);
12 try { 13
test.open(Folder.READ_WRITE);
14 } catch (MessagingException ex) {
15 test.open(Folder.READ_ONLY);
16 }
17 }
18 }
[***@hubmaier kmail2tbfolders]$ gcj -C test.java
[***@hubmaier kmail2tbfolders]$ gij test
Exception in thread ``'main``'
java.lang.NullPointerException
at test.main(test.java:11)
[***@hubmaier kmail2tbfolders]$
Chris Burdess
2007-05-03 12:59:33 UTC
Permalink
Post by Matej Cepl
When (following http://tinyurl.com/ysqmfx ) I tried other
1 import java.util.*;
2 import javax.mail.*;
3 public class test {
4 public static void main(String[] args) throws
MessagingException {
5 String urlStr
= ``/home/matej/.eclipse/workspace\
/kmail2tbfolders/examples/``';
6 Session session = Session.getInstance(new
Properties());
7 URLName storeURL = new
URLName(``'mbox://``'+urlStr);
8 Store store
= session.getStore(storeURL);
9 Folder test
= store.getDefaultFolder();
10 test = test.getFolder(``'test``');
11 test.open(Folder.READ_WRITE);
12 try { 13
test.open(Folder.READ_WRITE);
14 } catch (MessagingException ex) {
15 test.open(Folder.READ_ONLY);
16 }
17 }
18 }
You will need to put store.connect() and store.close(), just like
with any other JavaMail provider.
Chris Burdess
2007-05-03 13:18:31 UTC
Permalink
Post by Chris Burdess
Post by Matej Cepl
When (following http://tinyurl.com/ysqmfx ) I tried other
1 import java.util.*;
2 import javax.mail.*;
3 public class test {
4 public static void main(String[] args) throws
MessagingException {
5 String urlStr
= ``/home/matej/.eclipse/workspace\
/kmail2tbfolders/examples/``';
6 Session session = Session.getInstance(new
Properties());
7 URLName storeURL = new
URLName(``'mbox://``'+urlStr);
8 Store store
= session.getStore(storeURL);
9 Folder test
= store.getDefaultFolder();
10 test = test.getFolder(``'test``');
11 test.open(Folder.READ_WRITE);
12 try { 13
test.open(Folder.READ_WRITE);
14 } catch (MessagingException ex) {
15 test.open(Folder.READ_ONLY);
16 }
17 }
18 }
You will need to put store.connect() and store.close(), just like
with any other JavaMail provider.
Note also that you are trying to open the folder twice, which will fail.

You also need to explicitly close the folder (in a finally clause or
whatever) as otherwise the mailbox lock file will remain and you
won't be able to open it again in another program run.
Matej Cepl
2007-05-03 13:52:27 UTC
Permalink
Post by Chris Burdess
You will need to put store.connect() and store.close(), just
like with any other JavaMail provider.
Chmm, chmm (source is on http://matej.ceplovi.cz/tmp/test.java)
I get this:

[***@hubmaier kmail2tbfolders]$ gij test
Exception in thread ``main``' java.lang.NoClassDefFoundError:
javax.mail.internet.MimeMessage
at java.lang.Class.initializeClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
at
gnu.mail.providers.mbox.MboxFolder.open\
(classpathx-mail-1.3.1-monolithic-1.1.1.jar.so)
at test.main(test.java:13)
Caused by: java.lang.ClassNotFoundException:
javax.activation.DataSource not found in
gnu.gcj.runtime.SystemClassLoader{urls=\
[file:./,file:/usr/share/java/classpathx-mail-1.3.1-monolithic.jar],
parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[],
parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.VMClassLoader.defineClass(libgcj.so.7rh)
at java.lang.ClassLoader.defineClass(libgcj.so.7rh)
at
java.security.SecureClassLoader.defineClass(libgcj.so.7rh)
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
...4 more
[***@hubmaier kmail2tbfolders]$

Should I install some other package (shouldn't it be a dependency
of the Fedora package then)?

Thanks for all the help,

Matej
Chris Burdess
2007-05-03 14:05:38 UTC
Permalink
Post by Matej Cepl
Chmm, chmm (source is on http://matej.ceplovi.cz/tmp/test.java)
javax.mail.internet.MimeMessage
at java.lang.Class.initializeClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
at
gnu.mail.providers.mbox.MboxFolder.open\
(classpathx-mail-1.3.1-monolithic-1.1.1.jar.so)
at test.main(test.java:13)
javax.activation.DataSource not found in
gnu.gcj.runtime.SystemClassLoader{urls=\
[file:./,file:/usr/share/java/classpathx-mail-1.3.1-
monolithic.jar],
parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[],
parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.VMClassLoader.defineClass(libgcj.so.7rh)
at java.lang.ClassLoader.defineClass(libgcj.so.7rh)
at
java.security.SecureClassLoader.defineClass(libgcj.so.7rh)
at java.net.URLClassLoader.findClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.ClassLoader.loadClass(libgcj.so.7rh)
at java.lang.Class.initializeClass(libgcj.so.7rh)
...4 more
Should I install some other package (shouldn't it be a dependency
of the Fedora package then)?
I don't know how Fedora organises its packages. There are 4 jars you
need in your classpath:

inetlib.jar
activation.jar
gnumail.jar
gnumail-providers.jar

Loading...