I have been trying to set up a JavaFX project to prepare for a school project and I wanted to use Maven to make it easier to share it as it removes the need for configuration as long as the pom.xml file is correctly written.
I can compile the project, run it with javafx:run but when I use the maven jar plugin to generate a JAR I get the following error trying to run it :
Error: Could not find or load main class org.openjfx.App
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
I don't understand why it's not finding the JavaFX classes as I do have the JavaFX Maven plugin as well as the javafx-controls and javafx-fxml dependencies added in the pom.xml
Here's the full file for reference
<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>org.openjfx</groupId>
<artifactId>hellofx</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>11</maven.compiler.release>
<javafx.version>19</javafx.version>
<javafx.maven.plugin.version>0.0.8</javafx.maven.plugin.version>
</properties>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>${javafx.maven.plugin.version}</version>
<configuration>
<mainClass>org.openjfx.App</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>org.openjfx.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Does anyone know what I'm missing for the JAR to include the JavaFX classes ? I thought adding the maven plugin was enough.
Thank you
Related
I get two separate error windows when trying to run the created jar:
"Error: A JNI error has occurred, please check your installation and try again."
Followed by:
(Java Virtual Machine Launcher)
"A Java Exception has occurred."
My project itself runs just fine, and the jdk, jre are both of version "17". The jar command in maven is use is the default: jar:jar -f pom.xml. I tried running jar -t ThesisMVN-1.0-SNAPSHOT.jar which hang and didn't produce a result after 10s, as well as jar --validate ThesisMVN-1.0-SNAPSHOT.jar, which gave me the following errors:
java.nio.file.FileAlreadyExistsException: C:\Users\user\AppData\Local\Temp\tmpJar404095564019557515.jar
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:87)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:236)
at java.base/java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:484)
at java.base/java.nio.file.Files.newOutputStream(Files.java:228)
at java.base/java.nio.file.Files.copy(Files.java:3160)
at jdk.jartool/sun.tools.jar.Main.run(Main.java:411)
at jdk.jartool/sun.tools.jar.Main.main(Main.java:1665)
Finally, this is what my pom.xml looks like:
<?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>hu.elte.inf</groupId>
<artifactId>ThesisMVN</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>main/Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>
(No clue what the final tag and its contents are for but removing it didn't affect the result of my problem).
What am I missing here?
After I'd searched the net and similar StackOverflow questions and tried to apply the proposed sollutions I didn't come up with an idea how to fix this error, which gives me pom.xml:
Multiple annotations found at this line:
- Missing artifact jakarta.json:jakarta.json-
api:jar:2.0.0-RC3
- Missing artifact org.glassfish.hk2:hk2-
locator:jar:3.0.0-M2
- Missing artifact org.glassfish:jakarta.json:jar:2.0.0-
RC3
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 //error appears here
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gson</groupId>
<artifactId>advanced-jaxrs-01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>advanced-jaxrs-01 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-
media-moxy -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>3.0.0-M6</version>
</dependency>
<!--
https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -
->
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>3.0.0-M6</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>advanced-jaxrs-01</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven
defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-
bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
I've had a look at .m2/repository and I've noticed, that there are files maven can't find. What should I do? What's the matter?
If there are files that maven can't find, you can always try to install the JAR file and going into
Project folder properties --> Java Build Path --> Add external JARs (Click on Modulepath if you cant immediately press Add External JARs)
I have a very simple project written in Java 11 with just one class. I want to package it using Maven. During compilation step Maven prints warning:
"Required filename-based automodules detected. Please don't publish
this project to a public artifact repository!"
Here I found information that I should add Automatic-Module-Name entry to the manifest file but unfortunately, it doesn't help. I still get that warning.
Do you an idea what else might be wrong? What should I do to make Maven happy?
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.padamski</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.pgs-soft</groupId>
<artifactId>HttpClientMock</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifestEntries>
<Automatic-Module-Name>com.padamski.test</Automatic-Module-Name>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
and module-info.java
module com.padamski.test {
requires java.net.http;
}
I am trying to create a runnable jar from a maven project created in Intellij Idea.
I tried building the Artifact through Intellij, but that did not work out. It could not find the main file.
After that I tried it through maven with:
- mvn compile
- mvn package
This creates a runnable jar which executes, but later when parsing a csv it throws an Exception:
Exception in thread "JavaFX Application Thread"
java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVParser
But it is added in my pom.xml... I downloaded everything even the docs. I can see the org.apache.commons:commons-csv package in the external libraries, but it seems to be missing when creating the jar.
<?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>
<packaging>jar</packaging>
<groupId>sceosa</groupId>
<artifactId>CEO_SA_ReportingLine</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<finalName>CEOSA</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>de.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Can anybody see what is wrong with the pom file or intellij?
By default maven does not include dependencies when building jars. The jar will only work if you have the dependency in some other way.
You can use the maven-assembly-plugin to build a jar-with-dependencies.
I have used the maven-shade-plugin and it works great if you want to bundle all your dependencies into one jar. All you need is the plugin and provide the Main class from your project and it should have a main method. It will look something like this:
<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>com.test.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
And just build your project using mvn clean install and you should find the the jar in your target directory and it will be suffixed with -shaded.jar. Hope this helps
I need help on running maven project. I can run the project on Eclipse, and I tried to deploy / run it on Jenkins CI but without success.
I made a maven project with custom folder structure (not following the default).
Below is the structure:
I can run the project successfully through Eclipse by cleaning the project first where I believe Eclipse build all java files for me. Below is the 'target' folder looks like after I clean the project through Eclipse (not maven clean)
However, when I delete the content of 'target' folder, and run maven -clean and maven -install I received an error like this:
I believe it's because I did not set anything on the pom.xml about building all java classes. However I do not know how to do it ? Can you help me? Changing structure is avoided right now. Thanks
Below is the 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>bukalapak</groupId>
<artifactId>bukalapak</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.46.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- <resources>
<resource>
<directory>src/bukalapak</directory>
</resource>
</resources> -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<forkMode>never</forkMode>
<suiteXmlFiles>
<suiteXmlFile>testsuite/TestSuiteBukalapak.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
You can use maven-compiler-plugin. Example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
If you don't use the default directory structure, you have to configure the source directory appropriately. In your case, the correct configuration would be:
<build>
<sourceDirectory>src</sourceDirectory>
</build>
Otherwise Maven would look in the default folder structure.