manurajhada
manurajhada

Reputation: 5380

java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V

I was using mail-1.4.jar version to read gmail inbox. In order to configure listener, I have upgraded it to javax.mail-1.6.0.jar. After this upgrade, email reader method has started throwing this error. Even after lots of google and several trials related to maven dependencies and version changes I am not able to solve this.

java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V

Below is the maven dependency in my pom.xml

<dependency>
      <groupId>com.sun.mail</groupId>
      <artifactId>javax.mail</artifactId>
      <version>1.6.0</version>
</dependency>

Below the Java code :

Properties props = new Properties();
props.put("mail.store.protocol", "imaps");
Session session = Session.getInstance(props, null);
Store store = session.getStore();
try {
 store.connect("imap.gmail.com", userName, password);
}catch(Exception e){
 e.printStackTrace();
 logger.debug("IMAP Email Listener connection fail");
}

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);

Message[] msgs = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

for (int i = 0; i < msgs.length; i++) {
  try {
   String subject = msgs[i].getSubject();
   logger.debug("subject " + i + ":" + subject);
   Object content = msgs[i].getContent(); // Here is the exception comes
   logger.debug("content" + i + ":" + content.toString());
  } catch(Exception e) {

  }
}

When I run the same code in stand alone java class everything works fine. But the same code doesn't work when I deploy war file in tomcat.

Stacktrace:

java.lang.NoSuchMethodError: javax.mail.internet.ParameterList.combineSegments()V
 com.sun.mail.imap.protocol.BODYSTRUCTURE.parseParameters(BODYSTRUCTURE.java:420)
 com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:239)
 com.sun.mail.imap.protocol.BODYSTRUCTURE.<init>(BODYSTRUCTURE.java:110)
 com.sun.mail.imap.protocol.FetchResponse.parseItem(FetchResponse.java:256)
 com.sun.mail.imap.protocol.FetchResponse.parse(FetchResponse.java:212)
 com.sun.mail.imap.protocol.FetchResponse.<init>(FetchResponse.java:96)
 com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:409)
 com.sun.mail.iap.Protocol.command(Protocol.java:355)
 com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2151)
 com.sun.mail.imap.protocol.IMAPProtocol.fetch(IMAPProtocol.java:2143)
 com.sun.mail.imap.protocol.IMAPProtocol.fetchBodyStructure(IMAPProtocol.java:1717)
 com.sun.mail.imap.IMAPMessage.loadBODYSTRUCTURE(IMAPMessage.java:1552)
 com.sun.mail.imap.IMAPMessage.getDataHandler(IMAPMessage.java:808)
 javax.mail.internet.MimeMessage.getContent(MimeMessage.java:927)

Upvotes: 2

Views: 1843

Answers (1)

Pierre B.
Pierre B.

Reputation: 12933

When I run the same code in stand alone java class everything works fine. But the same code doesn't work when I deploy war file in tomcat.

Probably caused by one of the following:

  • Your Tomcat server is using a previous or improper version of javax.mail. Make sure a javax.mail jar is not somewhere on the CLASSPATH of your server, such as in CATALINA_HOME/lib folder
  • Maybe your app is packaged using an improper version of javax.mail. Check the generated package (WAR file?) to see if it includes the javax.mail jar

EDIT: to easily find from which JAR file your class is loaded from, add a debug output in your app on startup or before the class is supposed to be used such as:

System.out.println(ParameterList.class.getResource("Paramete‌rList.class"));

And deploy your app on the bogus server. It will show you from which (wrong) JAR the class is loaded from.

Upvotes: 3

Related Questions