问题
I am trying to send am email SSL email in Netbeans and I have added both javaee.jar and mail.jar to my project but I am getting the following error
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at GUI.CompilerForm.<init>(CompilerForm.java:43)
at Compiler.Main.main(Main.java:19)
From my research I have found out that I am going to have to run my code inside a Java EE container but I have no experience with this. I have looked this up online with little success, only finding out about Java EE containers.
So how do you add code to a Java EE container and then use this in a project?
回答1:
how to add Java EE container to java project
No, please don't do that. Your sole intent seems to run a Java SE (desktop) application, not a Java EE (web) application. You should not run a Java SE application as a Java EE application.
You should rather find the right solution for the concrete problem you're facing instead of asking how to achieve the wrong solution.
As to your concrete problem,
Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException
This basically means that the mentioned class (the javax/mail/MessagingException) in the current runtime classpath only contains the class and method signatures, but no concrete code at all. In other words, the mentioned class in the current runtime classpath is empty. And indeed, the javaee.jar which you've there (apparently you downloaded it in a wrong attempt to solve compile/import problems) contains basically the abstract Java EE API without any concrete implementation code.
This is not right. Remove that javaee.jar file altogether. Just the mail.jar which you can download from JavaMail website is sufficient. Your concrete problem is caused because the javaee.jar file, which also contains the javax.mail API (but without implementation), got precedence in classloading over mail.jar file which contains the concrete javax.mail implementation. The javaee.jar file should only be used as a compiletime dependency in Java EE projects, not as a runtime dependency of Java SE projects.
来源:https://stackoverflow.com/questions/12163374/netbeans-how-to-add-java-ee-container-to-java-project