Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17705

Kotlin class servlet provoke NoClassDefFoundError

In a sample Java servlet project in my IntelliJ IDE, i have created this class in Kotlin :

@WebServlet(name = "TwitterAPIServlet", description = "This is used to test the servlet api", urlPatterns = ["/twitterAPIServlet"])
class TwitterAPIServlet : HttpServlet() {
    @Throws(IOException::class)
    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {

        // Print answer
        val out = resp.writer
        out.println("Request Done : </br>")
    }
}

When i am trying to call this with my .jsp page, i have this error :

2020-05-10 15:30:34.218:WARN:oejs.HttpChannel:qtp60559178-27: /twitterAPIServlet
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
    at main.java.co.rezo.api.internal.v1.TwitterAPIServlet.doGet(TwitterAPIServlet.kt)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)

When i am trying the same code in Java, it's working :

@WebServlet(
        name = "TwitterAPIServlet",
        description = "This is used to test the servlet api",
        urlPatterns = "/twitterAPIServlet")
public class TwitterAPIServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        out.println("Request Done : </br>");
    }
}

What can i do to make my Kotlin code working?

Upvotes: 3

Views: 245

Answers (1)

madhead
madhead

Reputation: 33412

Most probably the issue is that Kotlin's standard library is not included in the generated WAR (or is not on a classpath if you run the exploded app). Make sure to include it in your build.

Instructions for Gradle can be found in the official examples repository: kotlin-examples.

Maven is similar: https://kotlinlang.org/docs/reference/using-maven.html.

In case of pure IDEA (though it's recommended to use one of the build tools above), check the projects settings:

enter image description here

You should see Kotlin's stdlib included in the artifact:

enter image description here

Upvotes: 1

Related Questions