Build executable .jar by Maven with depending on JavaFX - java

everyone!
I've tried to create .jar file by Maven with dependence on JavaFX according to this post. Unfortunately, attempts was failed. In folder targets creating two files:
MAVEN_UI_INTRO-1.0-SNAPSHOT.jar
original-MAVEN_UI_INTRO-1.0-SNAPSHOT.jar
With first file, I get this error:
Error: JavaFX runtime components are missing, and are required to run this application
With second file, this error:
no main manifest attribute, in target/original-MAVEN_UI_INTRO-1.0-SNAPSHOT.jar
Build by command mvn clean package
Start by command java -jar target/"someJarFile.jar"
Main question: What do I wrong?
UPD: My pom.xml file:
<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>com.novalab</groupId>
<artifactId>MAVEN_UI_INTRO</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.novalab.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.1</version>
<configuration>
<mainClass>com.novalab.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

Related

Unable to add javafx.web to Maven project on VS Code [duplicate]

This question already has an answer here:
Cannot resolve symbol 'WebView'. JavaFX WebView with Maven in IntelliJ IDEA [duplicate]
(1 answer)
Closed last month.
I'm new to maven and java projects as a whole and I've been trying to add javafx.web to my Maven project, specifically for the HTMLEditor class, on VS Code and so far have been unable to.
I've added the dependency to the pom.xml file and put in VM Arguments that lead to my installation of JavaFX and nothing has worked.
Is there a way to do this that I'm missing or would it be better to just add a copy of the javafx.web.jar that is in the JavaFX\javafx-sdk\lib folder directly somehow?
My pom.xml file:
<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>dylan.gresham</groupId>
<artifactId>nottah</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>19</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>dylan.gresham.Main</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation=
"org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>dylan.gresham.Driver</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
My VM Arguments:
--module-path C:\Users\dylan\Downloads\JavaFX\javafx-sdk-18.0.1\lib --add-modules =ALL -MODULE-PATH
My module-info.java:
module dylan.gresham {
requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;
opens dylan.gresham to javafx.fxml;
exports dylan.gresham;
}
Edit: Added module-info file above.
As pointed out by #James_D I needed to add requires javafx.web; to my module-info.java file. After adding that line I was able to import what I needed to.

Make windows stand-alone executable file for spring boot project and angular project which can access offline in windows

I have one jar file which bundles with UI now I want to make one standalone window executable file. in the jar file, I have code with PostgreSQL database tomcat server
my pom file for the project like
I want only one exe which has run directly on double click
<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">
<parent>
<groupId>dev.marco</groupId>
<artifactId>java-angular-example</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
</properties>
<artifactId>backend</artifactId>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>frontend</artifactId>
<version>${project.version}</version>
<type>jar</type>
</dependency>
....
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<mainClass>dev.marco.ShlowinApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
</project>
The closest I can think of would be launch4j, adding an exe launcher for your application. This will add the exe, but will not magically make it a desktop-application, for that you may need to adjust code.
A configuration in the pom.xml would look similar to the following:
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>l4j-gui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>gui</headerType>
<outfile>target/${project.artifactId}.exe</outfile>
<jar>target/${project.artifactId}.jar</jar>
<dontWrapJar>false</dontWrapJar>
<addDependencies>true</addDependencies>
<errTitle>Error in launch4j plugin</errTitle>
<classPath>
<mainClass>${mainClass}</mainClass>
</classPath>
<icon>src/main/resources/Images/logo.ico</icon>
<jre>
<path>./java</path>
</jre>
<versionInfo>
<fileVersion>${project.version}.0</fileVersion>
<txtFileVersion>${project.version}.0</txtFileVersion>
<fileDescription>${description}</fileDescription>
<copyright>Copyright(c) ${distributor}</copyright>
<companyName>${distributor}</companyName>
<productVersion>${project.version}.0</productVersion>
<txtProductVersion>${project.version}.0</txtProductVersion>
<productName>${project.artifactId}</productName>
<internalName>${project.artifactId}</internalName>
<originalFilename>${project.artifactId}.exe</originalFilename>
<language>ENGLISH_UK</language>
</versionInfo>
</configuration>
</execution>
</executions>
</plugin>

"Error: Could not find or load main class" in a Module of Spring Boot

I have a Spring Boot app that is in a Module within a project.
The module has its own pom.xml and the outer project has its own pom.xml
The module's pom.xml has the <mainClass>...</mainClass> annotation pointing to the main class of the app.
It also has a reference to the parent pom.xml with the <parent>...</parent> property, pointing to the parent artifactId, groupId & version.
When running the app within Intellij, it runs OK.
The problem is when trying to run the app from the jar:
java -jar myApp.jar
Then we get and error of
Error: Could not find or load main class com.example.server.Application
What are we missing here?
EDIT - Adding the POM files:
The parent 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>com.example</groupId>
<artifactId>my-app</artifactId>
<packaging>pom</packaging>
<version>5.1-SNAPSHOT</version>
<modules>
<module>my-app-server</module>
</modules>
<parent>
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>LATEST</version>
</parent>
<repositories>
...
</repositories>
<name>...</name>
<description>...</description>
<properties>
...
</properties>
The module 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">
<parent>
<artifactId>my-app</artifactId>
<groupId>com.example</groupId>
<version>5.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.my-app</groupId>
<artifactId>my-app-server</artifactId>
<repositories>
...
</repositories>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<optimize>true</optimize>
<debug>true</debug>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.server.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<tarLongFileMode>posix</tarLongFileMode>
<descriptors>
<descriptor>package.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Open your jar file with winzip or winrar, if you are using linux use archive manager. Then go to the META-INF, open the MANIFEST.MF and make sure that the property "Main-Class: your.class.path" is correct.
I had the same error but in a simple java project with javafx, I had already built the project and for some reason I wanted to change the name of the package where it found the main class, when I ran the program I got that error. I solve it:
Modifying the file "module-info.java" where the main class is referred to
I also modified the pom.xml
<configuration><mainClass>pathToTheMainClass</mainClass></configuration>
There you have to refer to the package who contains main class

How to create a fat jar?

With SpringBoot, you have the #SpringBootApplication annotation, but what is the equivalent with the neat Java Spark framework?
IntelliJ creates a Maven project and I added the spark dependency, but running the install goal, I get a 5 KB jar with no manifest. Not an executable jar.
The pom.xml created thus far is as follows:
<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>sparkdemo</groupId>
<artifactId>sparkdemo</artifactId>
<version>1.0 </version>
<packaging>jar</packaging>
<name>sparkdemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<optimize>true</optimize>
<debug>true</debug>
</configuration>
</plugin>
</plugins>
</build>
</project>
Would appreciate any suggestions....Thanks!
What you need here is an executable jar that will not only contain your classes, but also classes from all your dependencies.
For that you can use the Maven Assembly Plugin.
See the sample code below.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.sample.App</mainClass> // specify your main class here
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
Then, run mvn package and the executable jar should be located in the folder target (with a name like ProjectName-1.0-SNAPSHOT-jar-with-dependencies.jar).
Looks like you are looking for maven assembly plugin
Add this in the plugin section and run mvn package
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Groovy & maven - compile sourcesfrom src/main/groovy

I have been searching for few hours how to build .jar with maven.
Finnaly i got it.
But now i have to have sources in src/main/java.
How to 'include' src/main/groovy to my project?
I read solution here but I really don't want to change my pom.xml. I had so many troubles with this.
Here 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.thornydev</groupId>
<artifactId>script</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>script</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.7.0-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>pl.jedro.Main</mainClass>
</transformer>
</transformers>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.0-beta-3</version>
</dependency>
</dependencies>
I tried with:
<sources>
<source>${basedir}/src/main/groovy</source>
</sources>
and
<build>
<resources>
<resource>
<directory>src/main/groovy</directory>
</resource>
</resources>
but still nothing.
I got :
Error: Could not find or load main class pl.jedro.Main
Finally got solution.
I added:
<sourceDirectory>src/main</sourceDirectory>
<resources>
<resource>
<directory>groovy</directory>
</resource>
</resources>
in <build> </build> section.

Categories