Home > OS >  Spring Boot Application - Running the jar file locally gives JNI error (NoClassDefFoundError: javax/
Spring Boot Application - Running the jar file locally gives JNI error (NoClassDefFoundError: javax/

Time:09-22

Migrated a legacy spring project to spring boot. Used the beans from the legacy xml file by importing the xml file as resource with @ImportResource annotation. Legacy spring project had web.xml file with servlet mapping and this is handled by creating a servlet bean in the application main class. Now the application is running fine locally and it is creating a jar file in the target folder. But when I try to run the jar file with command (java -jar myapp.jar), it gives an error as shown below.

Error: A JNI error has occured, please check your installation and try again
Exception in thread "main"  java.lang.NoClassDefFoundError: javax/servlet/Servlet
at java.lang.class.getDeclaredMethods0(Native Method).

The main class and the config file looks like below.

Application.java:

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

@Bean
public ServletRegistrationBean dispatcherApiServlet() { 
  ServletRegistrationBean servRegBean = new ServletRegistrationBean(); 
  servRegBean.setServlet(new ServletContainer()); 
  servRegBean.addUrlMappings("/api/*"); 
  servRegBean.addInitParameter("javax.ws.rs.Application",
  "com.api.AppResourceConfig "); 
  servRegBean.addInitParameter("jersey.config.server.wadl.disableWadl",
  "true"); 
  servRegBean.setLoadOnStartup(1);
  return servRegBean;
     }
    }

AppResourceConfig.java:

public class AppResourceConfig extends ResourceConfig {
    public AppResourceConfig {}{
      super();
    property("jersery.config.beanValidation.enableOutputValidationErrorEntity.server");
    packages("com.api");
    register(GsonProvider.class);
    register(RequestContextFilter.class);
    register(NotFoundExceptionMapper.class);
    register(DefaultExceptionMapper.class);
    }
    
}

I have multiple versions of Java in my system: java 8 and java 18. I have set the environment variable to point to java 8 and when check the java versions by using command java -version and javac -version and both are showing the same version (1.8). So this should not be an issue with multiple versions of java. For your information, I am using macbook to run my application

CodePudding user response:

This issue has been resolved after updating spring-boot-maven-plugin dependency with execution goal repackage as shown below. Also removed maven-jar-plugin dependency as
M. Deinum suggested.

<plugins>
 <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</plugins>

Thank you everyone for your inputs.

CodePudding user response:

Are you using maven ? If you are building a standlone jar., make sure to use maven assembly command instead of clean install

If bundles all the dependencies.

https://maven.apache.org/plugins/maven-assembly-plugin/usage.html

  • Related