java.lang.LinkageError:加载器约束冲突:之前为名称为“javax/mail/Session”的不同类型启动加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34542266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.LinkageError: loader constraint violation:previously initiated loading for a different type with name "javax/mail/Session"
提问by user2782405
I am getting the following error when I try to send email using javax.mail-api
:
当我尝试使用以下方法发送电子邮件时出现以下错误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.access0(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.doProceed(MethodInfo.java:279)
at org.apache.camel.component.bean.MethodInfo.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:
我的Maven依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
</dependency>
Please help.
请帮忙。
采纳答案by Jan
As suggested in comments, add your dependency to javamail as provideddependency:
正如评论中所建议的,将您的依赖项添加到 javamail 作为提供的依赖项:
<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.
这将跳过添加重复的 jars,然后由不同的类加载器加载。
If not somehow forced to use old version of javamail you should update to latest which is currently
如果没有以某种方式被迫使用旧版本的 javamail,您应该更新到当前最新版本
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
<scope>provided</scope>
</dependency>
回答by Addison
I had a very similar problem (with org.keycloak.keycloak-saml-adapter-api-public
), and solved it by editing my standalone.xml
, and removingthis line:
我有一个非常相似的问题(与org.keycloak.keycloak-saml-adapter-api-public
),并通过编辑我的standalone.xml
, 并删除这一行来解决它:
<profile>
<subsystem xmlns="urn:jboss:domain:ee:4.0">
<global-modules>
<module name="name.of.offending.package"/><!--REMOVE THIS LINE-->
</global-modules>
...
You'll have to restart your server after this change. Good luck.
在此更改后,您必须重新启动服务器。祝你好运。