24

This is the code :

      Session session = Session.getDefaultInstance(props, null);
      Store store = session.getStore("imaps");
      store.connect("imap.gmail.com", "****@gmail.com", "****");
      System.out.println(store);
      Folder folder = store.getDefaultFolder();
      folder = folder.getFolder("INBOX");
      folder.open(Folder.READ_ONLY);

      System.out.println("Message Count: "+folder.getMessageCount());
      System.out.println("Unread Message Count: "+folder.getUnreadMessageCount());


           Message[] messages = folder.getMessages();  --> here the code stops.

      FetchProfile fp = new FetchProfile();
      fp.add(FetchProfile.Item.ENVELOPE);
      folder.fetch(messages, fp);

      for (int i = 0; i< messages.length; i++) 
      { 
          System.out.println("From:"+ messages[i].getFrom()); 
          }

The code gives out the following excption and stops at the point shown.

Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource at com.google.code.com.sun.mail.imap.MessageCache.getMessage(MessageCache.java:129) at com.google.code.com.sun.mail.imap.IMAPFolder.getMessage(IMAPFolder.java:1394) at openReports.OpenReports.main

Martin
  • 37,119
  • 15
  • 73
  • 82
RaviKiran
  • 585
  • 2
  • 7
  • 16

4 Answers4

44

In case you use maven you can add manually

<dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
</dependency>
vorburger
  • 3,439
  • 32
  • 38
jediz
  • 4,459
  • 5
  • 36
  • 41
  • My project used ant. Just needed to add `` to the `.classpath` file. – Logg Feb 17 '19 at 05:38
  • 1
    same for gradle -> implementation 'javax.activation:activation:1.1.1' – ekarankow Oct 10 '19 at 10:00
  • Just for completeness, here's the artifact on sonatype with code for all build systems: https://search.maven.org/artifact/javax.activation/activation/1.1.1/jar – Pranav Nov 14 '19 at 20:00
8

I added activation.jar to buildpath and the problem is solved.

So i used 2 jars java-mail-ima.** .jar, activation.jar (for further referebces).

RaviKiran
  • 585
  • 2
  • 7
  • 16
  • 2
    Note that `javax.activation` is included in JDK 6 so if you use a newer JDK you won't need the activation jar. – Martin Nov 19 '11 at 06:31
  • Yeah .. That helped. It reduces the number of jars.. Thanks alot. – RaviKiran Nov 19 '11 at 08:38
  • @Martin if we not used activation jar with higher JDK version. Still it shows no class found. – Abhishek Jul 20 '17 at 05:09
  • 22
    Note that this was removed again from Java 9 onwards. You will get the same error again after upgrading to JDK 9 or greater. – jbx Apr 15 '18 at 21:56
  • This showed up for me in the R language using the `rJava` library. I wonder how I can port this solution there... – Hack-R Feb 16 '19 at 01:43
3

I solved a similar NoClassDefFoundError: javax/activation/DataSource problem in a mixed Java/Scala project by switching to Java 8 (it was Java9 by default).

Discussion: https://github.com/highsource/jsonix-schema-compiler/issues/81

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

On jdk 1.8 I solved this problem by reinstalling jdk and check path in JAVA_HOME and JRE_HOME

JavaSash
  • 19
  • 4