java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z - spring

I have written a spring application.
In the main method I am getting the following error:
Exception in thread "main" java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z
at org.springframework.context.event.GenericApplicationListenerAdapter.supportsSourceType(GenericApplicationListenerAdapter.java:79)
at org.springframework.context.event.AbstractApplicationEventMulticaster.supportsEvent(AbstractApplicationEventMulticaster.java:282)
at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:214)
at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:185)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:121)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.visa.redis.Application.main(Application.java:27)
Following is my Application class:
#EnableAutoConfiguration
#ComponentScan("com.test")
public class Application {
static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
SeperateRead seperateRead = applicationContext.getBean(SeperateRead.class);
try {
seperateRead.execute();
} catch (Exception e) {
logger.error(e.getMessage());
System.exit(1);
}
System.exit(0);
}
}
Following is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>redis-client</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I looked at the possible causes for this error over internet, looks like some version mismatch. But I am not able to figure out the problem.
Any help is appreciated. Thanks in advance.

Related

Spring boot gives AbstractJdbcConfiguration errors on JUnit Test

Im trying to test my Repository with junit.
I have parent project Maven and some subproject for user activty.
this is parent pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.testpr</groupId>
<artifactId>testprWebParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>testprWebParent</name>
<description>parent testpr web parent</description>
<properties>
<java.version>19</java.version>
</properties>
<modules>
<module>testprBackEnd</module>
<module>testprFrontEnd</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>com.testprcommon</groupId>
<artifactId>testprCommon</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
and this is a child or subproject pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.testpr</groupId>
<artifactId>testprWebParent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>testprBackEnd</artifactId>
<name>testprBackEnd</name>
<description>testpr Admin Project</description>
<properties>
<java.version>19</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The child project is in the parent folder and also used tag from parant project.
I made a test class in child project inside eclipse and if i run Junit it gives me this
error :
java.lang.NoClassDefFoundError: org/springframework/data/jdbc/repository/config/AbstractJdbcConfiguration .....
Okey kinda confusing error log but after i change my test class from this
#DataJdbcTest
#AutoConfigureTestDatabase(replace = Replace.NONE)
public class RoleRepositoryTests {
#Autowired
private RoleRepository repo;
#Test
public void testCreateFirstRole() {
Role roleAdmin = new Role("Admin","For Test");
Role savedRole = repo.save(roleAdmin);
assertThat(savedRole.getId()).isGreaterThan(0);
}
}
to
#SpringBootTest
#AutoConfigureTestDatabase(replace = Replace.NONE)
public class RoleRepositoryTests {
#Autowired
private RoleRepository repo;
#Test
public void testCreateFirstRole() {
Role roleAdmin = new Role("Admin","For Test");
Role savedRole = repo.save(roleAdmin);
assertThat(savedRole.getId()).isGreaterThan(0);
}
}
And my application.properties from this :
spring.jpa.properties.hibernate.dialect = com.mysql.cj.jdbc.Driver
to this :
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
Junit Test Runs Successfully.

Spring Boot: Could not resolve matching constructor when using scope: prototype

Problem
I'm having an issue where, any bean I define with the scope = prototype causes a BeanCreationException:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myClass' defined in class path resource [application-context.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:218)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1166)
at com.example.coaching.BeanScopeDemoAppKt.main(BeanScopeDemoApp.kt:10)
at com.example.coaching.BeanScopeDemoAppKt.main(BeanScopeDemoApp.kt)
Example
This is the most basic example I could create that reproduced the problem...
MyClass.java:
Variation 1
public class MyClass {
public String doNothing() {
return "Do nothing";
}
}
Variation 2
public class MyClass {
public MyClass() {}
public String doNothing() {
return "Do nothing";
}
}
application-context.xml
<bean id="myClass"
class="com.example.coaching.models.MyClass"
scope="prototype">
</bean>
MyApp.kt
class MyApp
fun main() {
val context = ClassPathXmlApplicationContext("beanScope-applicationContext.xml")
val myClass = context.getBean("myClass", MyClass::class) as MyClass
println(myClass.doNothing())
}
Other Info
I experienced this in a more complex project, but made this one to try to narrow down the problem and expected I would find something, but I have no clue.
If I change the scope to singleton, it works fine.
Happens with xml configuration as well as annotations
Tried creating MyClass in both Kotlin and Java
Here's my pom.xml just in case
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Demo Annotations</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<kotlin.version>1.4.31</kotlin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

Not able to connect with ElasticSearch 7.3.0 for logger dependency error in elasticSearch client

I'm running a simple method that will connect with elasticsearch and feed data to a particular index with the pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Following is the code block that I have run
package com.example.demo;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class Es {
public static void main(String[] args) {
try {
Client client = new PreBuiltTransportClient(Settings.builder().put("client.transport.sniff", true)
.put("client.transport.ignore_cluster_name", true).build())
.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
+ "\"message\":\"trying out Elasticsearch\"" + "}";
IndexResponse response = client.prepareIndex("twitter", "_doc").setSource(json, XContentType.JSON).get();
// Index name
System.out.println(response.getIndex());
// Type name
System.out.println(response.getType());
// Document ID (generated or not)
System.out.println(response.getId());
// Version (if it's the first time you index this document, you will get: 1)
System.out.println(response.getVersion());
// status has stored current instance statement.
System.out.println(response.status());
client.close();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
When i run the main method it will show the following error
Exception in thread "main" java.lang.NoSuchMethodError: 'org.apache.logging.log4j.Logger org.elasticsearch.common.logging.Loggers.getLogger(java.lang.String)'
at org.elasticsearch.transport.netty4.Netty4InternalESLogger.<init>(Netty4InternalESLogger.java:34)
at org.elasticsearch.transport.netty4.Netty4Utils$1.newInstance(Netty4Utils.java:53)
at io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:93)
at io.netty.util.internal.logging.InternalLoggerFactory.getInstance(InternalLoggerFactory.java:86)
at io.netty.util.internal.PlatformDependent.<clinit>(PlatformDependent.java:70)
at io.netty.util.ConstantPool.<init>(ConstantPool.java:32)
at io.netty.util.AttributeKey$1.<init>(AttributeKey.java:27)
at io.netty.util.AttributeKey.<clinit>(AttributeKey.java:27)
at org.elasticsearch.transport.netty4.Netty4Transport.<clinit>(Netty4Transport.java:231)
at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:56)
at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:89)
at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:271)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1654)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
at org.elasticsearch.plugins.PluginsService.getPluginSettings(PluginsService.java:89)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:156)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:296)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
at com.example.demo.Es.main(Es.java:20)
I am also changing the ElasticSearch client dependency version to 6.9 to 7.3 but getting the same error.
Changing the logger dependency to
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
instead of using log4j-to-slf4j, slf4j-api & logback-classic

Spring Boot on Embedded Tomcat with JSTL still throws java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config

I have an application which leverages PrimeFaces, elastic search,Spring boot.
Following is it's pom.xml,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.acn.hps.aops.demo</groupId>
<artifactId>PSCommandCenter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CommandCentre</name>
<description>Command Center Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.geocoder-java</groupId>
<artifactId>geocoder-java</artifactId>
<version>0.16</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<!-- Primefaces -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.3.4</version>
</dependency>
<!-- JSF dependencies -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.8</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Elastic Search dependencies -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>primefaces</id>
<url>http://repository.primefaces.org</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
</project>
Following is its spring configuration,
#SpringBootApplication
public class CommandCentreApplication extends WebMvcConfigurerAdapter{
public static void main(String[] args) {
SpringApplication.run(CommandCentreApplication.class, args);
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/home.jsf");
}
#Bean
ServletRegistrationBean facesServletRegistration() {
return new ServletRegistrationBean() {
#Override
public void onStartup(ServletContext servletContext)
throws ServletException {
FacesInitializer facesInitializer = new FacesInitializer();
facesInitializer.onStartup(Collections.singleton(CommandCentreApplication.class),
servletContext);
servletContext.setInitParameter("primefaces.THEME", "redmond");
servletContext.setInitParameter("javax.faces.FACELETS_REFRESH_PERIOD", "0");
servletContext.setInitParameter("facelets.SKIP_COMMENTS", "true");
}
};
}
#Bean
ServletRegistrationBean pushServletRegistration(){
ServletRegistrationBean pushServlet = new ServletRegistrationBean(new PushServlet(), "/primepush/*");
pushServlet.addInitParameter("org.atmosphere.annotation.packages", "org.primefaces.push");
pushServlet.addInitParameter("org.atmosphere.cpr.packages", "com.acn.hps.aops.demo.map.service.channel");
pushServlet.setAsyncSupported(true);
pushServlet.setLoadOnStartup(0);
pushServlet.setOrder(Ordered.HIGHEST_PRECEDENCE);
return pushServlet;
}
}
and my elastic search's configuration,
#Bean
public Client client(){
TransportClient client= new TransportClient();
TransportAddress address = new InetSocketTransportAddress(PropertiesLoader.esIP, PropertiesLoader.esPort);
client.addTransportAddress(address);
return client;
}
This same application runs as expected when I execute it from my IDE (Eclipse) but when I execute the application from its jar then I am getting the following exception ,
java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_45].
I searched for this exception and I found that jstl's jar is added to avoid this error. But I already have jstl's jar in tomcat's jar. And also if this is the problem then I should be getting the same exception while executing from my IDE.
You can refer this github code which i used as outline for my application. I am facing the same issue in this also.
When using spring-boot-starter as a parent, running
mvn clean install
should produce a fat jar (that means all dependencies are copied into that jar).
More informations can be found here:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html
Maybe this part in the spring-boot-starter plugin within the pom.xml
<configuration>
<executable>true</executable>
</configuration>
is messing with this. Try to remove that part and build the jar again.

Getting error running spring boot application using -jar

This question has been asked over and over, however I couldn't find my answer. I have an application using spring boot, which intellij can run it without any issue, however java -jar echohostname.jar give me this error:
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
Here is my Main class:
public class ApplicationMain {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringConfiguration.class);
ConfigurableApplicationContext applicationContext = builder.run(args);
}
This is my Controller class:
#RestController
public class Controller {
#Autowired
#Qualifier("getSigarProxy")
private SigarProxy sigarProxy;
#RequestMapping(value = "/hostname", method = RequestMethod.GET)
public String hostName() throws SigarException {
return "HELLO THERE";
//return sigarProxy.getNetInfo().getHostName();
}
}
And this is spring configuration class:
#SpringBootApplication
#Configuration
#ComponentScan("controller")
public class SpringConfiguration {
#Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
#Bean
public Sigar getSigar() {
return new Sigar();
}
#Bean
public SigarProxy getSigarProxy() {
return SigarProxyCache.newInstance(getSigar(), 1000);
}
#Bean
#Scope("prototype")
public HttpHeaders getHttpHeaders() {
return new HttpHeaders();
}
#Bean
#Scope("prototype")
public AsyncRestTemplate getRestTemplate() {
return new AsyncRestTemplate();
}
}
POM file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>echoHostname</groupId>
<artifactId>echoHostname</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
UPDATED MAIN:
public class ApplicationMain {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringConfiguration.class, args);
}
}
UPDATED POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>echoHostname</groupId>
<artifactId>echoHostname</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>
Any help is appreciated.
Add class level #SpringBootApplication annotation in ApplicationMain.java
Add the below plugin in your pom.xml. Build the maven project and execute the jar command
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
This page has more information on maven assembly plugin - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
In a spring-boot project, you need that the parent pom be
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
You can remove the spring dependencies, like context, core and beans.
Add to the pom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
And build the jar again.

Resources