ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException

后端 未结 4 889
轮回少年
轮回少年 2020-11-30 07:48

I am getting a strange ClassFormatError when using the javaMail api to send email on my spring mvc web app.

Below is my mail-cfg.xml

<         


        
相关标签:
4条回答
  • 2020-11-30 08:16

    You normally get this error when you have multiple conflicting JAR's in your class path. One of the more common reasons is if you include an artifact in your classpath that is supposed to be provided by the application container. Looking at your POM, you have several redundant and overlapping dependencies. Remove these:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.3</version>
    </dependency>
    <dependency>
        <groupId>javaee</groupId>
        <artifactId>javaee-api</artifactId>
        <version>5</version>
    </dependency>
    

    And change the scope of your Java EE dependency to 'provided':

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    

    Finally, make sure you are deploying to a Java EE 6 capable web server, and the error should go away.

    --- Edit ---

    Just noticed that you are compiling to a Java 5 compliance level. Change this:

    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>
    

    And make the source and target values 1.6.

    0 讨论(0)
  • 2020-11-30 08:21

    I was having a similar issue,... so, when I was running JUnit an error occured:

    Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/HttpServlet
    

    I solve it adding the following dependency (first one):

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2.2</version>
        <scope>test</scope>
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 08:29

    I too faced the same kind of exception when running junits with jmock and maven:

    Exception in constructor: testDoExecute (java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/servlet/http/Cookie at java.lang.ClassLoader.defineClass1(Native Method)

    It is due to classes your code try to access which are not available in the javaee api jar used in the project because your code compiles against incomplete classes, like the JavaEE6 Api and your Unit tests try to access code thats not there. JUnit will simply fail and mark the test as error, printing something like the above exception.

    It could be solved either by adding javax.servlet jar/dependency or changing javaee jar version shown below:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    
    **OR** 
    
    <dependency>
        <groupId>javax.j2ee</groupId>
        <artifactId>j2ee</artifactId>
        <version>1.4</version>
        <scope>provided</scope>
    </dependency>
    

    PS: Please add the jar version compatible with your other existing jars in the project.

    0 讨论(0)
  • 2020-11-30 08:40

    The javax:javaee-api is intended for compiling, not running, including unit tests. If you need classes suitable for running against, run against a Java EE application server.

    Also, the correct name is Java EE, not JEE.

    0 讨论(0)
提交回复
热议问题