java.lang.LinkageError: loader constraint violation:previously initiated loading for a different type with name "javax/mail/Session"

15,888

As suggested in comments, add your dependency to javamail as provided dependency:

<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.1</version>
        <scope>provided</scope>
</dependency>

This will skip adding duplicate jars which would then be loaded by different classloaders.

If not somehow forced to use old version of javamail you should update to latest which is currently

<dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.5</version>
        <scope>provided</scope>
</dependency>
Share:
15,888
user2782405
Author by

user2782405

Updated on July 03, 2022

Comments

  • user2782405
    user2782405 almost 2 years

    I am getting the following error when I try to send email using javax.mail-api:

    Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) previously initiated loading for a different type with name "javax/mail/Session"
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
            at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
            at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
            at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
            at com.sun.mail.util.PropUtil.getBooleanSessionProperty(PropUtil.java:86)
            at javax.mail.internet.MimeMessage.initStrict(MimeMessage.java:320)
            at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:195)
            at sendEmail(manage.java:216)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
            at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
            at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
    

    Code:

    Public void sendEmail() {
        String to = "[email protected]";
        String from = "[email protected]";
        final String username = "[email protected]";
        final String password = "password";
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", "smtp.office365.com");
        properties.put("mail.smtp.port", "587");
        Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            System.out.println("Inside test");
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("Testing Subject");
            message.setText("This is message body");
            Transport.send(message);
            System.out.println("Sent message successfully....");
         } catch (MessagingException e) {
             throw new RuntimeException(e);
         }
    }
    

    My maven dependency:

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.5</version>
    </dependency>
    

    Please help.

    • Jan
      Jan over 8 years
      Check all yout jars. You probably have another mail.jar (not -api) sitting around. If thats the case mark mail-api jar "provided" to skip packaging that
    • Ralf
      Ralf over 8 years
      Are you using plain vanilla Felix as the runtime? Or a container like Karaf? Your code works for me in Karaf 4, using javax.mail instead of javax.mail-api as a bundle dependency. As Jan already mentioned, somehow you end up with two versions on your bundle classpath.
    • user2782405
      user2782405 over 8 years
      @Jan: I tried adding "provided" but I am getting same error.
    • Jan
      Jan over 8 years
      Check your classpath - for everything mail-* and make sure it's all in server libs or application libs but not mixed
    • user2782405
      user2782405 over 8 years
      @Ralf: I am using karaf as well (version 3.0.4). Can you please share your dependency code. I tried but my project fails to build without javax.mail-api.
    • heenenee
      heenenee over 8 years
      @user2782405 You should put your solution in an answer and accept it.
    • Bill Shannon
      Bill Shannon over 8 years
      As described on the JavaMail project page, the javax.mail-api artifact is only for compiling against and includes only the standard javax.mail API definitions. The javax.mail artifact includes the entire runtime library.
  • Bill Shannon
    Bill Shannon over 8 years
    You probably want to use the current version - 1.5.5.
  • user2782405
    user2782405 over 8 years
    @BillShannon: I tried with version 1.5.5 but I am getting same error.
  • Bill Shannon
    Bill Shannon over 8 years
    I don't understand. I thought when you said "I used different Maven dependency and it worked" it meant that you had solved the problem. If not, please describe exactly what you're doing now and exactly what error you're getting. If your solution works with version 1.5.1 but fails with version 1.5.5, please tell me more.
  • Jan
    Jan over 8 years
    @BillShannon The way I understood the issue, there were conflicting javamail versions somewhere in the runtime environment. Adding 1.5.5 might still lead to duplicate versions (1.5.1 in runtime, 1.5.5 from dependency)
  • Bill Shannon
    Bill Shannon over 8 years
    Well, with "provided" scope, you're getting the version that comes with the runtime you're using. If your runtime comes with 1.5.1, then you're right that you should use 1.5.1 in your dependency. With "provided" scope, you're not going to end up with two versions, you're only going to get the version that's in the runtime, not the version you mention in the dependency. Ideally, your dependency should mention the same version that your runtime provides.
  • Jan
    Jan over 8 years
    @BillShannon user claims to have tried that and failed (see comments)
  • Bill Shannon
    Bill Shannon over 8 years
    As I understood it, user tried that without "provided" scope, and with changing the version, and that failed. As expected. The version isn't the solution to the original problem, "provided" scope is. But in general, given a choice, user should use the latest version, not some older version.
  • Jan
    Jan over 8 years
    @BillShannon I agree - check the comments on the question - third comment, directed at me: "I tried adding "provided" but I am getting same error. I'll edit the answer nontheless.
  • Bill Shannon
    Bill Shannon over 8 years
    Never mind. It looks like the question was edited to remove that text. If "provided" doesn't work, I have no idea what's wrong.
  • user2782405
    user2782405 over 8 years
    @BillShannon and Jan: Sorry for the delay. I tried using version 1.5.5 with and without "provided" but it didn't work. I was getting same error as before (loader constraint violation). When I changed the version to 1.5.1 then it worked. I don't know why.
  • Bill Shannon
    Bill Shannon over 8 years
    The "loader constraint violation" means you have two copies of the JavaMail classes available to your application. When using "provided" scope, the JavaMail jar file is not included with your application, so there's no way it can be the source of the problem. Unless you have another dependency that also includes the javax.mail classes. What are the other dependencies?
  • user2782405
    user2782405 over 8 years
    <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>${camel-version}</version> </dependency><dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-blueprint</artifactId> <version>${camel-version}</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.6.4</version> <scope>compile</scope> </dependency>