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