Cannot run executable jar packaged with Maven and Spring Boot - java

I'm trying to package my webapp in a fat jar in order to be executed with java -jar my-app.jar. Everything works when I do mvn spring-boot:run but when I try to run the packaged jar with:
$ mvn clean package
$ java -jar target/my-app.jar
Spring Boot logo is shown but afer 3 or 4 seconds, it stops, without start any server.
This 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>com.digitalmenu</groupId>
<artifactId>services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jooq</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
EDITED
After enable logs, I see this error:
org.springframework.context.ApplicationContextException: Unable to
start embedded container; nested exception is
org.springframework.boot.context.embedded.EmbeddedServlet
ContainerException: Unable to start embedded Tomcat
Error creating bean with name 'jerseyConfig' defined in URL...
I have a class JserseyConfig...
I have tried this but still not working...

Finally, I found the answer! Thanks to the comment of the OP in this question's accepted answer.
I was registering the Jersey resources with:
packages("com.myapp.resources");
Registering each one individually everything works:
register(Res1.class);
register(Res2.class);
...

Related

Spring Boot create .jar file (Maven)

I did not want to ask this question but I tried all the possible solutions on the StackOverFlow. With all the codes I tried the problem was:
Here 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 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.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ahmetbesli</groupId>
<artifactId>nobet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>nobetci</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am trying to create the jar file with Intellij build artifacts.
I tried to add maven plugin for creating jar file.
I tried to define the mainClass also. But it didn't work.
spring-boot Maven: How to create executable jar with main class?
Create Jar of maven spring boot project
Build jar and run spring boot from cmd
Thanks for your help.
You should NOT use task jar in IntelliJ like that:
Instead, we can build jar by this command: ./mvnw clean package
And start service as usual: java -jar demo-0.0.1-SNAPSHOT.jar
Update: Also can use package in IntelliJ:

Spring Data Cassandra - build fails when dependency is added

I was following this guide https://www.baeldung.com/spring-data-cassandra-tutorial to try and get started with using Cassandra paired with spring boot, however, the Cassandra connector dependency:
<!-- https://mvnrepository.com/artifact/com.datastax.oss/java-driver-core -->
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
<version>4.14.0</version>
</dependency>
Doesn't seem to contain the necessary modules for adding an AbstractCassandraConfiguration. When adding spring-boot-starter-data-cassandra as a dependancy it gives me the required modules to add Cassandra configurations, but my build fails with the error:
java: error reading `C:\Users\X\.m2\repository\com\google\code\findbugs\jsr305\3.0.2\jsr305-3.0.2.jar; zip END header not found`
Here 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 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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.morpheus</groupId>
<artifactId>reportdbgateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reportdbgateway</name>
<description>Charlie&apos;s software engineering assignment cassandra db gateway</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My first approach would be going to:
C:\Users\X.m2\repository\com\google\code\findbugs\jsr305\3.0.2
and delete:
jsr305-3.0.2.jar
and then run a maven clean install or your goal
Sometimes the jars are downloaded corrupted

Why JVM cannot find the definition of SpringApplication class?

It might look like a duplicate of this question, but the solutions from it didn't work for me. I am getting an Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at com.example.demo.Application.main error when I try to run the application using spring-boot. 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>com.example.blog</groupId>
<artifactId>module-8-16-springboot-webflow-thymeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>org.springframework.js</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- And other dependencies -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
JDK 1.8 and JRE 8 are installed on the system and worked fine before. I deleted the C:\Users\{username}\.M2\repository\org\springframework\boot directory on the system and forced Maven to reload the dependencies. Then I decompiled the file C:\Users\{username}\.M2\repository\org\springframework\boot\spring-boot\2.0.1.RELEASE\spring-boot-2.0.1.RELEASE.jar and still found it in it has this SpringApplication class that the JVM cannot find. I also explicitly added spring-boot-2.0.1.RELEASE.jar to the project, but the error remained the same. There is only a suspicion that I am using Open JDK, and not Oracle, which did not install on the system, but simply downloaded the deployed files and placed them in the folder I needed (the JDK installer for some reason does not work for me). Does anyone have any suggestions on what this problem may be related to?

run Spring Boot using Java -jar

I have a spring boot application that is working property, however I want to run it on one of my servers using the "java -jar" command
I did read this
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html
which says I can run the application by
$ java -jar target/myapplication-0.0.1-SNAPSHOT.jar
However I don't see a jar file under the target folder.
Any thoughts on how I can run this by using the java -jar command?
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>com.myapp</groupId>
<artifactId>MyApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyApp</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.plivo/plivo-java -->
<dependency>
<groupId>com.plivo</groupId>
<artifactId>plivo-java</artifactId>
<version>3.0.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
You can do the Clean install from Your Maven or Gradle Build . Not from Your Eclipse Clean Build.
If You do the the clean from your eclipse It will recompile all your Project.
If You do the clean insatll for your Maven(right click the pom.xml file and do the clean install ) then it will generate jar or war file to target folder

Dependency error in Spring Boot and Elastic Search maven application

I am creating application using Spring Boot, Spring Data, and Elasticsearch.
I am following this guide. I'm using Maven as my build tool. I am using IntelliJ IDEA as my primary IDE, and I created the Maven project using my IDE.
Here is my pom.xml file, that I created according to the tutorial.
<?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>1</groupId>
<artifactId>springboot-springdata-elasticsearch</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am facing this error message:
Project '''org.springframework.boot:spring-boot-starter-parent:1.5.1.RELEASE''' not found.
Inspects a Maven model for resolution problems.
As well as this one:
Dependency '''org.springframework.boot:spring-boot-starter-data-elasticsearch:''' not found.
Inspects a Maven model for resolution problems.
And this one too:
Dependency '''org.springframework.boot:spring-boot-starter-test:''' not found.
Inspects a Maven model for resolution problems.
And the same error messages for other dependencies.

Categories

Resources