Search code examples
javajersey-2.0classnotfoundexceptionbazel

Bazel java.lang.ClassNotFoundException: javax.servlet.http.HttpSessionIdListener


I just started porting my project to bazel. I have a java project. In my main file i have this code

ServletContextHandler handler = new ServletContextHandler(ServletContextHandler.SESSIONS);

I am able to build the code well...however when i try to run i get the following exception

java.lang.ClassNotFoundException: javax.servlet.http.HttpSessionIdListener

I am using the dependency

maven_jar(
        name = "javax_servlet_javax_servlet_api",
        artifact = "javax.servlet:javax.servlet-api:3.1.0",
        repository = "http://uk.maven.org/maven2/",
        sha1 = "6bf0ebb7efd993e222fc1112377b5e92a13b38dd",
    )

Which is supposed to have this file. What could possibly be the problem.

java_binary(

        name = "app",
        main_class = "com.example.Main",
        runtime_deps = [":allFiles"],
    )

    java_library(
        name = "allFiles",
        srcs = glob(["src/main/java/**/*.java"]),
        resources = glob(["src/main/resources/**/*"]),
        deps = [
            "//thirdparty:javax_servlet_javax_servlet_api",
            ...
    ])

The above is what i have in by BUILD file. The thing is that the java library allFiles Builds fine. But when i run app i get the exception. I know that the main file is called because i can see the line in the Main.java file that throws the exception.


Solution

  • I figured out the run time error. Initially when i generated my dependencies from maven pom file. It gave me

    maven_jar(
            name = "javax_servlet_javax_servlet_api",
            artifact = "javax.servlet:javax.servlet-api:3.0.1",
            repository = "http://uk.maven.org/maven2/",
            sha1 = "6bf0ebb7efd993e222fc1112377b5e92a13b38dd",
        )
    

    But the version needed by jetty server was 3.1.0 so i changed the code to

     maven_jar(
                name = "javax_servlet_javax_servlet_api",
                artifact = "javax.servlet:javax.servlet-api:3.1.0",
                repository = "http://uk.maven.org/maven2/",
                sha1 = "6bf0ebb7efd993e222fc1112377b5e92a13b38dd",
            )
    

    But i failed to chane the sha1. Removing sha1 resolved the issue. Now there are neither build nor runtime errors.

    Finally

     maven_jar(
                    name = "javax_servlet_javax_servlet_api",
                    artifact = "javax.servlet:javax.servlet-api:3.1.0",
                    repository = "http://uk.maven.org/maven2/",
                )