Search code examples
javajakarta-eeglassfishgmailgmail-api

Java GMail api - java.lang.VerifyError: Cannot inherit from final class


I'm using glassfish server 4.1 with java JDK 1.8. I use maven to build and deploy.

I made a form that triggers a controller to send an email using gmail api.

The form that triggers the controller:

<form method="post" action=GmailController>
        <td>Test API gmail</td>
        <td><input name="email" value="${requestScope.get("email")}" /></td>
        <td><input type="submit" value="Send email" /></td>
</form>

The controller "GmailController":

@WebServlet("/GmailController")
public class GmailController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String email = request.getParameter("email");

    try {
        sendAnEmail("email_body", email, email); //custom method that uses gmail API
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

The sendAnEmail method that calls GMail API functions:

public static void sendAnEmail(String body, String from, String to) throws Exception {
    Gmail service = getGmailService();
    MimeMessage msg = createEmail(to, from, "Area", body);
    sendMessage(service, "[email protected]", msg);

}

When I submit the form that triggers the controller, I get a HTTP 500 error and glassfish displays :

java.lang.VerifyError: Cannot inherit from final class

When I tested GMail API outside of my java glassfish web application, it worked correctly, by calling sendAnEmail in a java main.

I don't understand what can be causing this error because my class doesn't inherit from any other class except from HttpServlet.

When I comment the sendAnEmail() call, there is no errors, so the bug must not be because of HttpServlet inheritance.

What can be causing this error ? How can I debug or get more informations about it ?

In server.log I don't get more informations, it shows the same error and tells the thread has stopped.

After some testing, exact same code on another computer and OS gives :

java.lang.NoSuchMethodError: com.google.api.client.util.ClassInfo.getFieldInfos()Ljava/util/Collection;

Instead.

google-api-services-oauth2, google-api-services-gmail and google-api-client all are version 1.23.0.


Solution

  • Error: java.lang.VerifyError: Cannot inherit from final class

    Based from this thread, you have managed to create a class that extends a superclass, where the superclass has been declared as final.

    The most likely cause is that you have a conflict between your build classpath and your launch classpath. In other words, you are compiling your subclass against a version of the superclass that is not final, and then running against a version that is final. The verifier is saying (correctly) that this is wrong.

    Error: java.lang.NoSuchMethodError: com.google.api.client.util.ClassInfo.getFieldInfos()Ljava/util/Collection;

    Based from this forum, your code is (indirectly) calling a method that didn't exist. Check your dependencies that you're using in your classpath.

    Hope this helps!