Java Spring boot executable JAR file not running into another machine - java

I am trying to package a Java spring boot (Maven) project into JAR file. So that I can take that JAR file into another computer and simply run it. The file is created inside "target" folder. I can run the project fine with following:
java -jar target/project.jar
but whenever I take the Jar file to another place (like, into another PC) and try to run like this:
java -jar project.jar
it's showing White Label Error 404 page at - localhost:8080
How to package the project to run as standalone JAR file without any such errors?
Here's my application.properties file content:
spring.datasource.url=jdbc:mariadb://localhost:3306/sample
spring.datasource.username=root
spring.datasource.password=****
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDB103Dialect
spring.jpa.hibernate.ddl-auto=update
## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB
## File Storage Properties
# All files uploaded through the REST API will be stored in this directory
file.upload-dir=/Users/noticepush/notices
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
And this is my pom.xml content:
<?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.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tech</groupId>
<artifactId>StressDetection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>StressDetection</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Any suggestion for me?
Note: Please don't suggest WAR packaging. I am trying to distribute the project as JAR package.

The issue is that when you use java -*.jar to deploy any springboot application , the jsp files will not be present in the embedded tomcat and while trying to serve the request you will get a 404 PAGE NOT FOUND. This is because of the jar packaging ,that the jsp files are not getting copied from the WEB-INF folder. If you keep the jsp files under the META-INF/resources folder while using jar as packaging it should work.
Related Question : Why does Spring boot not support jsp while it can render the page if we add proper jar reference

spring.datasource.url=jdbc:mariadb://localhost:3306/sample Your application has a dependency in some local database.
It can be that in your computer the database exists and is reachable by the application so the application can start and play correctly.
On other computers the database may not exist or may not be reachable from the application so Spring context fails during initialization and therefore the web application is not reachable from you!
If as pointed in comments this is not the issue, then there can be another issue as well.
You can replace the following in your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
with
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
So that the final Jar is a repackaged Fat jar which will contain also all runtime dependencies needed.

I get such error in the past
You are having this error because there is no defaut webPage for spring boot Screenshot with the error
To fix it , you just need to add a simple html file (index.html) in you src/main/resources/statics directory

You might have to configure a view resolver in this case. Typically we place JSP files in the ${project.basedir}/main/webapp/WEB-INF/jsp/. In such a case, you will have to add following 2 configuration parameters in application.properties.
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

Related

"Spring Boot Project" Error: Could not find or load main class fyp.fms.FleetManagementSystemApplication

I have gone through all the articles available in and out stack over flow. But my problem is not resolved. I have a working java 8 which works fine on my all applications with in eclipse and also i have tried to run with cmd. it means class path, environment variable etc, all works fine. Now i want to create a Spring boot project, so when ever i run an spring boot project using both Run As Java Application & Spring Boot App, It Gives This Error "Error: Could not find or load main class package.classname" Here is the Screen Shot:
Note: I have tried almost every thing, like clean project, checking environment variables, updated eclipse etc. This problem is with only a spring boot application. All other Java applications works fine.
Here is my pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.1.RELEASE
fyp
fms
0.0.1-SNAPSHOT
FleetManagementSystem
Fleet Management System On Spring Boot. A Final Year Project.
<properties>
<java.version>1.8</java.version>
<start-class>fyp.fms.FleetManagementSystemApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
Add following property to pom.xml
<properties>
<start-class>fyp.fms.FleetManagementSystemApplication</start-class>
</properties>
Add this build plugin if you don't have it in your pom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
run mvn clean install
Run application using mvn springboot:run
Can you try below steps:
Right click on your project. Select "Run As" -> "Maven build...". Then in "Goals" field, enter "spring-boot:run". Apply & Run.

After deploying war file in Tomcat api not working

I'm trying to deploy app in my local tomcat but have some problems. I use: Tomcat 9, Spring boot, ReactJS and Webpack. When I run embedded Tomcat (in Eclipse) all be ok - API working good, but when I build war file and paste it to my local Tomcat - API not working, all request failed.
How I build war file:
run mvn "clean install";
paste war file to my local Tomcat directory "webapps";
wait for deploying;
go to "http://localhost:8080/web_importer/#/importCandidates". But in this moment when i run in Eclipse my app i use another path: "http://localhost:8080/#/importCandidates" and API use this path to (example) "http://localhost:8080/api/originator", not "http://localhost:8080/web_importer/api/originator".
How I can change this path?
application.properties:
server.port = 8080
management.server.port: 8995
management.server.address: 127.0.0.1
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
spring.http.encoding.force=true
OriginatorController.java:
#RequestMapping(value = "/api/originator", method = RequestMethod.GET)
public List<OriginatorModel> retrieveOriginators() {
logger.info("Performing /api/originator GET request");
return originatorService.retrieveOriginators();
}
My pom.xml:
<groupId>com.kddb_web_importer</groupId>
<artifactId>web_importer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>web_importer</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
When you start the application from Eclipse, it uses a standalone server, which is directly accessed via localhost and the URL is http://localhost:8080/api/originator.
When you run your local tomcat instance, http://localhost:8080 is the base tomcat URL. What follows next is the name of your application, in this case web_importer. So the URL becomes http://localhost:8080/web_importer/api/originator and this is why you get 404 Not Found.
It seems that your frontend is calling the API directly at http://localhost:8080/api/originator. You need to change your base API URL in your frontend configuration when you want to use the tomcat-deployed version of your API.

How to deploy jsp in spring boot

I want to deploy web application that use jsp files using executable jar(Spring boot)
Project structure:
1)in src\main\resources\META-INF\resources\WEB-INF\jsp folder i put my jsp files
2)app.prop file
spring.mvc.view.prefix : /WEB-INF/jsp/
spring.mvc.view.suffix : .jsp
3)Controller:
#Controller
#RequestMapping("test")
public class TestController {
#RequestMapping(method = RequestMethod.GET)
public String test(){
// System.out.println("Inside controller");
return "test";
}
}
When i build it using mvn package it create jar file, to run jar file I have two options:
1)java -jar MyJar
2)mvn spring-boot:run
When i use mvn spring-boot:run and go to localhost:8080/test my browser show me content of test jsp file
When i use java -jar Myjar.jar and go to the same url i gor an 404 error
There was an unexpected error (type=Not Found, status=404).
/WEB-INF/jsp/test.jsp
I can't always use mvn spring-boot:run because i will deploy jar to remote server. How to solve this problem?
BTW this is my pom
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
java -jar Myjar.jar command do not deploy your application to embeded tomcat. If you want to use separate application server you can find out your application output in
/target
directory and you need to deploy it.

How to deploy spring boot app project to Heroku

I have created a simple server that uses JPA repository and returns a response in Json. It includes methods post and get. It is a spring starter boot app project and everything works on my localhost (I use postman to send and receive json objects). My problem is when I try to deploy to Heroku I run into many problems. I added a jetty-runner dependency and plugin. I also created a procfile as shown below:
web: java $JAVA_OPTS -jar target/dependency/jetty-runner.jar --port $PORT target/*.war
Initially the target folder was empty and foreman start web was saying that it couldn't find my war file, so I added the following line to pom.xml:
<packaging>war</packaging>
But the project created an error, so I disabled maven nature of the project, then configured maven again and then it included pom.properties and pom.xml in the target folder. When I tried to deploy it, it said it was unable to access jetty jar file in target/dependency because there was no folder like that. So I did maven install and it installed the missing folders. Now I get the error that No transaction manager can be found, so I installed a dependency for jetty-plus and Atomikos. But now I still get the error that there is not transaction manager found and there is a java.net.bindexception.
I feel like I am really on the wrong path. I was wondering if anyone can tell me from the beginning on how to deploy a spring starter boot project to heroku. Any help would really be appreciated.
This is my pom.xml 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>org.test</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Challenge-Server1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.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.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>copy</goal></goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-runner</artifactId>
<version>7.4.5.v20110725</version>
<destFileName>jetty-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I found out that the deploying a spring boot application is a little different then deploying spring boot mvc project. Basically you don't need a jetty-runner dependency or plugin, you just do the following steps:
Configure a mvn project
mvn clean and install
Declare Procfile as: web: java $JAVA_OPTS -Dserver.port=$PORT -jar target/*.jar
All found from this site:
http://web.archive.org/web/20171018145733/http://nicholaspaulsmith.com/spring-boot-on-heroku/
I outlined some details in my blog
https://exampledriven.wordpress.com/2016/11/04/spring-boot-heroku-example/
The main point is to use the heroku maven plugin like this
heroku plugins:install heroku-cli-deploy
mvn clean install
# Creates an app with the specified name, without setting up a git remote
heroku create <APP-NAME> --no-remote
#deploys the jar file
heroku deploy:jar target/demo-0.0.1-SNAPSHOT.jar --app <APP-NAME>
but there are more details like how to set up a CI/CD pipeline

JSP class undefined type after project conversion to maven

Im trying to compile this project h__p://ross-warren.co.uk/my-twitter-clone-using-jsp-and-cassandra/
I have downloaded it, and spend almost one day searching for all dependency jar files and still I was not able to run that project. When I did some search I have discover maven. So I've downloaded it, in eclipse I click convert to maven or something, I've added some dependecy records to pom.xml and I still Im not able to run the project. There are some errors in jsp files. These errors werent there befoe.
for example in file LogChech.jsp
<jsp:useBean id="User"
class="uk.co.ross_warren.litter.stores.UserStore"
scope="session"
></jsp:useBean>
I have error Undefined type: UserStore.
I dont know where is the problem but I think there is somethign wrong with packages, becase before project conversion I had all java files in packages (at least eclipse showed package icon) and now there is only normal folder icons and folder tree structure with all java files however the first line in java files is package something which is fine I think.
thank you for your help
EDIT
this is my pom.xml
<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>Cinnamon</groupId>
<artifactId>Cinnamon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>1.0-4</version>
</dependency>
</dependencies>
<packaging>war</packaging>
</project>
This is the first image how it looked like before maven conversion
http://oi39.tinypic.com/2q84o5z.
And this is how it looks like now
http://oi44.tinypic.com/14nmgyf.
ok stack overflow says I cant submit images, so please take that url and add jpg extension to see image
The standard maven directory layout expects java sources to be in src/main/java and web resources in src/main/webapp. So your java sources directly in src weren't even compiled. These directories can be configured in the build section of your pom.xml. To set the directory for web resources you also have to configure the maven-war-plugin.
Edit: It seems you were also missing some dependencies, the pom below compiles without errors on the command line. I'm not an eclipse user myself but this should work in eclipse too.
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Cinnamon</groupId>
<artifactId>Cinnamon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<directory>build</directory>
<sourceDirectory>src</sourceDirectory>
<outputDirectory>build/classes</outputDirectory>
<!-- not used in your project -->
<testSourceDirectory>srcTest</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>me.prettyprint</groupId>
<artifactId>hector-core</artifactId>
<version>1.0-4</version>
</dependency>
<dependency>
<groupId>org.expressme</groupId>
<artifactId>JOpenId</artifactId>
<version>1.08</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
</project>

Categories

Resources