How to run Spring Boot JavaFX Maven project with Java 17? - spring-boot

When I try to run a Spring Boot JavaFX project with
mvn spring-boot:run
I get an error "JavaFX runtime components are missing". The JDK version is 17.
I did an online search on the error message. Two solutions I find. One is to make the application modular. I don't see that is a valid solution for the Spring Boot application. And the other one is to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules".
I also try to run the project with JDK only
java -jar app.jar
Although there aren't any errors, the GUI doesn't show up.
How to resolve this issue? A good solution would be that it is easy to run without many downloading, local setup etc.

Since Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application, you will need to specifically include it.
Regarding, "to make the application modular":
Spring 5 and Spring Boot are not friendly with Java Platform Module System (JPMS). It won't really be built for modules until Spring 6/Springboot 3 is released.
I do try the modular approach with Spring Boot 2. However, compilation
failed due to a known Lombok
error.
Regarding, "to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules":
You can use non-modular approach without --module-path and --add-modules.
Maven configuration is pretty straightforward.
Firstly, Add the JavaFX modules you need as maven dependencies. For instance:
<properties>
<java.version>17</java.version>
<javafx.version>17.0.2</javafx.version>
</properties>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
The project can be built and run using Spring Boot Maven Plugin
NOTE: There is a maven plugin available for running JavaFX 11+ applications. So far, it is not required since the application will be packed and run using Spring Boot.
You may find a working example here, showcasing how to bootstrap JavaFX applications within Spring Boot application.
How to bootstrap JavaFX application within Spring Boot application?
The bootstrap process is heavily inspired by Mr. Awesome Josh Long’s
Spring Tips:
JavaFX
installment.
Instead of calling SpringBootApplication.run() use a custom bootstrap class inheriting from JavaFX Application. This is needed to initialize JavaFX correctly.
JavaFxApplication class does the heavy lifting for creating a proper JavaFX application with initialized Spring Context. It's responsible for:
Set Spring Boot web server type to NONE.
Programmatically create a Spring Boot context in the Application#init() method.
Kick off application logic by sending a StageReadyEvent containing the primary Stage as payload.
Support graceful shutdown for both Spring context and JavaFX platform.

Im literally searching for the same...
But I found there are some mods to do in your run-config to get this problem solved

Related

Selenium 4.1 and Spring boot Web driver version issue

I tried to migrate gradle project with spring boot from selenium 3 to selenium 4
(implementation 'org.seleniumhq.selenium:selenium-java:4.1.1')
But chrome, firefox and edge web drivers remains from selenium version 3
(org.seleniumhq.selenium:selenium-chrome-driver:3.141.59).
If I remove spring boot dependency from project, they updates to 4.1.1
Currently using gradle version "7.3.3".
Spring boot "2.6.3".
Spring dependency management "1.0.11.RELEASE".
Any ideas why this happens, I hoped that dependency hell dissapeared with spring boot creation)
Thanks in advance!
I have the same promble, I found in spring-boot-starter-parent, there is a spring-boot-dependencies in ~/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.6.3/spring-boot-dependencies-2.6.3.pom, which declared <selenium.version>3.141.59</selenium.version> in properties, so you can try add
<properties>
<selenium.version>4.1.2</selenium.version>
</properties>
in your project pom.xml.
Spring Boot comes with a set of versions that are know to work together. But you can override them in your build script. In case of Gradle, add to build.gradle:
ext['selenium.version'] = '4.1.2'
It is because Spring Boot comes packed with a set of predefined dependecies that you can find here: https://docs.spring.io/spring-boot/docs/2.6.7/reference/htmlsingle/#appendix.dependency-versions
In this page, you will also find, how to change predefined version. In general, you need to add .version to your dependency name and, in case of multiple word names, you change spaces to dashes (not needed in case of selenium).
So, to change version to most recent, type in build.gradle ext['selenium.version'] = '4.4.0' at 0 indent level. (don't add it, say inside dependencies {})

Use entity and repository definitions from Spring Boot outside of web application

I have a Spring Boot 2.1 web application. It works great. I can package it as either a WAR or a JAR.
In my pom.xml file I use:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
This application has many entity classes and quite a few Spring Data repositories. Almost always we deploy it as a WAR file.
Here's the question: sometimes we need to do a command line or batch process on our database. For example, we might want to run a process to resize all the images which are stored in the database, and that should be run by an administrator from the command line.
It would be great if mvn install would install a JAR file in the local Maven repository and I could use that artifact in another project to access my entity and repository definitions. I've tried many things, but whenever I build my project as a JAR file, and I look at the JAR, all my classes are within BOOT-INF/classes, which doens't allow them to be referenced from another project.
I was able to fix that by using a repackage goal in the spring-boot-maven-plugin. However, when I did that, it did generate a jar file but my CLI application couldn't start correctly with the repository beans created.
I read in the Spring Boot documentation:
Like a war file, a Spring Boot application is not intended to be used
as a dependency. If your application contains classes that you want to
share with other projects, the recommended approach is to move that
code into a separate module. The separate module can then be depended
upon by your application and other projects.
Is there any simpler way to do this, such that I don't have to create yet another project and manage that? Or is it a good practice to have a separate project for entities and Spring Data repositories?
So, in short words you just want to have a library with your entity and repositories? Then it should be enough to configure a simple maven project, a standard one, not inheriting from Spring Boot.
As spring Boot uses Spring Data JPA under the covers, you just need Spring Data JPA declarations, so add the dependency marking it as provided.
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>Some reference version<version>
<scope>provided<scope>
</dependency>
<dependencies>
This allows not to make the project include the Spring Data JPA dependency itself, as it will be included by the end project, which uses Spring Boot. However, you'll need to declare a reference version to use, you could take the one your current Spring Boot project uses (you can have a look in the maven dependency tree).
https://spring.io/projects/spring-data-jpa
Difference between maven scope compile and provided for JAR packaging

Spring Boot project depending on a spring project

fair warning: I am new to Spring and maven.
The project:
The client has provided us with a web project that uses jsp to render front ends and uses spring-context only to manage some of their classes as Spring Beans. This project uses xml configuration. Here is the maven section that includes the spring-context:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
Our job is to create an api layer for this project to open up access to the project's resources. The plan is to create a modern Spring Boot api using the spring-boot-web starter.
The problem:
The jsp project is split up into sub-modules. We are trying to take their business logic submodule as a maven dependency in our api project (it defines it's own spring context). The first problem that we have faced occurs when we simply add the module from the jsp project as a dependency in our api project. The spring boot initialization process provides no log feedback and hangs but does not exit (It runs fine when the dependency is not included). When crawling through with a debugger I have found that it is loading configuration files that are in the business module. My thinking is that one of the api's configuration files are getting overwritten which is breaking spring's logging and other behaviors.
The other more general problem is that I don't know if I can use the beans provided in the business logic module in my modern version of spring.
So the question is:
Is there a proper way to use beans defined in a dependent spring project?

Spring Boot support maven multi-module

I am trying to convert my existing multi-module maven Spring project to Spring Boot project. The reason is make it self contain and follow Martain Fowler's microservices concept.
However, the problem I have encounter is when try to clean build, seems the spring boot is trying to find the Main method from every module, which of course will failed.
Is this feature currently supported by Spring Boot 1.1.6.RELEASE or I did something wrong?
Thanks
It sounds like you've added Spring Boot's Maven plugin to every module in your build – it's what's looking for a main method. You should only add the Spring Boot plugin to a module if its a service that you want to run. If the module's just code that's shared between your services, the Spring Boot plugin isn't needed in that module.

Deploying Simple Spring Hibernate Utility Project via Maven to Tomcat

My goal is understanding the J2EE lifecycle at a high-level with Spring, Hibernate, and Maven. From much research, I understand that Spring provides dependency injection and Hibernate provides object-relation mapping with databases. Maven is a tool to improve the build/deployment process from my understanding. With that said, everywhere I search I get more and more lost on configuration files (i.e. pom.xml, server.xml, etc.), terminology, and alternatives such as Gradle. I just want to build and launch the application and be able to see via http://localhost:8080 in tomcat.
At first, I couldn't get the default project (picture attached) built, but after further research found that I needed to Maven clean and Maven install.
I also modified settings in pom.xml changing version numbers and the database to use MySQL.
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
The next issue I had was in OrderPersistenceTests.java it used org.hibernate.classic.Session which is no longer the correct class path and found that it needed to be org.hibernate.Session.
Finally, I was able to get it to build but cannot figure out how to deploy to Tomcat from Spring Tool Suite.
I have put together a simple example using Maven, Spring, Hibernate, and ExtJS for the front end at the following link:
https://github.com/drembert/java-webapp-sample
If you are using Spring Source Tool Suite which it looks like you are in the screen shot, you should be able to import everything using the "Import Maven Projects" option. The example uses Hypersonic as the in-memory DB to allow easier deployment. Keep in mind this example generates two different .war files (one is presentation-layer and the other is service-layer) to emulate a simple RESTful service so both will need to be deployed to the Spring tcServer (STS's version of Tomcat), but once they are you should be able to view the GUI at http://localhost:8080/presentation-layer. Another thing to make note of is that this example currently doesn't have a security layer which would normally be implemented using Spring security, but I am working on adding that in the near future.

Resources