I'm setting up a maven project, that is going to use JavaFX. Since I've heard JavaFX does not come with all versions of Java, I downloaded and put the jfxrt.jar file in a lib directory in my project.
1) How do I specify that the dependency (ie., JavaFX) should not be downloaded, but which is located in lib?
2) Does this then mean the project can be built on any machine with JDK (and not necessary JDK 1.7 - update 9+) ?
2019 Update for Recent JavaFX Versions
As of Java 11, JavaFX has been decoupled from the JDK, so JavaFX libraries are no longer automatically bundled together with a JDK install (as they were for instance in the Oracle JDK 8 distribution). Instead, the JavaFX system is now designed as a set of separate libraries independent from the JDK, which can be downloaded from a repository by a tool such as Maven for use by your application.
openjfx.io have a very short tutorial on using JavaFX 11+ with Maven:
https://openjfx.io/openjfx-docs/#maven
This tutorial recommends use of the OpenJFX JavaFX Maven Plugin and provides the following sample maven pom.xml project file for a "hello, world" style application.
<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>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Note that the org.openjfx JavaFX maven plugin used for Java 11+ is different from the com.zenjava JavaFX maven plugin used for Java 8 and 9, so use the appropriate one for your Java version.
Suggested Approach
For building JavaFX apps with Maven:
Use the com.zenjava maven JavaFX plugin with Java 8 and 9 AND/OR
Use Java 8 or 9 and don't specify any kind of Maven dependency for JavaFX OR
Use the org.openjfx maven JavaFX plugin with Java 11+.
Unlike Java 7, with Java 8 it is unnecessary to set JavaFX as a maven dependency, because JavaFX is on the standard Java runtime classpath (similar to the way Swing is today).
Opinion
I think the approach in your question of placing the jfxrt.jar file in a project lib directory is flawed.
The reasons it is flawed are:
JavaFX also includes native libraries, so you would need to include them as well (in a location where jfxrt.jar would be able to find them).
A given version of JavaFX will only be certified to operate against the version of the JDK it is shipped with, so the JavaFX runtime you place in your project's lib directory may not work against a later JDK version, such as JDK 8.
Future versions of the JDK such as JDK 8 will include JavaFX on the default classpath of the JDK, so you may get conflicts with the version you place in your lib directory.
In summary, JavaFX should be treated as part of the Java runtime system, not as a project library.
JavaFX System Dependency Sample
If you must use Java 7 and you don't want to use the JavaFX maven plugin, then you can take the less recommended approach:
Reference the JavaFX library from the jdk (not your project lib directory) as a maven system dependency.
This sample is provided for Java 7 only and is not required (and will not work as is) for Java 8. Java 8, places the jfxrt.jar in ${java.home}/lib/ext/jfxrt.jar, which is on the default Java runtime classpath, making a system dependency for Java 8 irrelevant.
<dependency>
<groupId>javafx</groupId>
<artifactId>jfxrt</artifactId>
<version>${java.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxrt.jar</systemPath>
</dependency>
If you package your application using a system dependency like this, then you need will need to write some execution instructions for your users so that they can add jfxrt.jar to the runtime classpath when running your application on Java 7. This is a very good reason not to use this approach.
If you choose to use the maven system dependency approach, you may also wish to use the maven antrun plugin to embed calls to the JavaFX ant tasks as in this sample. You may also want to use the maven enforcer plugin to ensure that your application is built against at least the minimum required version of Java containing a JavaFX version required by your application to work.
Related
I am trying to get a specific kind of file structure in eclipse for my work.
This is the structure I want:
The problem I am having is that everytime I use mvn eclipse:eclipse the structure of the files gets broken like this:
I have tried most of the answers that I've found on Stack Overflow and elsewhere. I've tried making the fileStructure2 to a maven Archetype and it sort of worked, but as soon as I ran mvn eclipse:eclipse it broke again:
I learned the basics of the archetype creation just today so I am not a master at that either and there might be something that is missing from the creation of the archetypes. I used mvn archetype:create-from-project in the FileStructure2 directory and then used mvn install archetype:update-local-catalog in the archetype directory which resulted the archetype to be added in the list of archetypes and I just created a new project with cmd and then ran mvn eclipse:eclipse which resulted in the above picture. I have not changed anything in the archetype folders that were created, I just used the basic ones that were created with running the archetype:create.
Most of the other posts i've found are really old and none of them have the structure I want, or it is only for src/main/java which I get working as intended but it is just the src/main/groovy file that switches the location to a normal folder instead of a source folder.
I am thinking it is my pom.xml that needs some modifications. at the moment it looks like this:
<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>FileStructure</groupId>
<artifactId>FileStructure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.0</version>
<type>pom</type> <!-- required JUST since Groovy 2.5.0 -->
</dependency>
</dependencies>
</project>
I have not added any maven compiler plugins since I don't 100% know what they do and still break the structure no matter what I've tried.
I am using apache-maven-3.8.5 which is downloaded with chocolatey. My eclipse jre and jdk versions are 1.8.0_333 but my java home is jdk11 since sonarQube can't use that old versions. My groovy is downloaded from the eclipse marketplace and it's compiler version is 2.5. My eclipse is a SAP netweaver. Eclipse Java EE IDE for Web Developers. Version: Oxygen.3a Release (4.7.3a)
I have converted the projects to maven and groovy via Eclipse's own methods.
Eclipse Groovy Development Tools will do this. There is a guide here with steps to achieve your desired result:
https://github.com/groovy/groovy-eclipse/wiki/Groovy-Eclipse-Maven-plugin
There is an archetype as well but it is not pre-built: https://github.com/groovy/groovy-eclipse/tree/master/extras/groovy-eclipse-quickstart
New to Java here. I have a java project on netbeans using maven. I am trying to add javafx to my project.
I added the following to my pom.xml file :
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx</artifactId>
<version>15</version>
<type>pom</type>
</dependency>
But when i try and build my project in netbeans I get this error (tsInfluence is the class which contains the main method that is trying to call jfx):
Error: LinkageError occurred while loading main class com.ts.tsInfluence.tsInfluence
java.lang.ClassFormatError: Invalid superclass index 0 in class file com/ts/tsInfluence/tsInfluence
Command execution failed.
Go to project properties -> Libraries -> Add a new classpath (Library) -> Give name etc -> than add the path to the scr zip of javafx
Edit:
Doesnt a window like this appear to you? right mouse button on project name
Edit 2:
Oh right... in maven you need to follow this
http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
A similiar question was answered here... you should follow what they say and just replace the file path for yours
Question close to this
The easiest way to build applications with JavaFX is to use a build of OpenJDK that includes the JavaFX modules. BellSoft and Azul both have variants of OpenJDK that bundle the JavaFX modules. Bellsoft's Liberica JDK calls this the "Full JDK", Zulu just calls it the "JDK FX" package. Beware that Bellsoft's version may not include the full media support - read the notes.
https://www.azul.com/downloads/zulu-community/?package=jdk-fx
Otherwise, follow the instructions at https://openjfx.io/openjfx-docs/#maven
Your setup does not work because "javafx" is not a valid artifact id. You have to provide (possibly more than one) dependencies like this.
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>15.0.1</version>
</dependency>
For more details read the documentation: https://openjfx.io/openjfx-docs/#maven
I am building a spring boot application and using Maven for the dependencies. I need to run some Matlab code from Java and I found out that MathWorks made an official Java API available to use since 2017. But the official documentation for it only points to a .jar file inside installed Matlab folder and does not have any Maven dependency. So how do I add it to my project in a smart way then? Right now I just do it in my pom.xml like this:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
But it seems to be a deprecated feature in Spring-Boot.
I am using Intellij Idea to compile a project that uses Maven dependencies and Intellij keeps telling me that my project has 50 something errors because JavaFX does not exist.
Intellij is not highlighting all the javafx dependencies in my code as errors, it is just that once I press the run and compile the program says that everything in JavaFX does not exists.
I tried to redownload the latest JDK (Java 9.0.1) and that did not fix it. I went into the Default Project Structure and Project Structure to make sure it was using the correct jdk and that did not fix the issue. All the jdks I am using seem to list the javafx packages as included in the project.
This is also only an issue for a particular project that I am working on with a friend. We may have to move over all our code into a new project, however I am not sure if that will fix anything.
Any suggestions?
Try to set project language level to "9" in "Project Structure | Project"
Okay I see what my problem was.
Besides Try to set project language level to "9" in "Project Structure | Project" mentioned above, I had a maven setting in some pom.xml looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
and java.version was defined as 1.8 somewhere above. I just had to change it to 9
I had this problem after upgrading a JavaFX project from Java 8 to Java 9.
After checking the usual language level settings for the project and module in IntelliJ and the Maven pom, I found the problem was that the module was explicitly set to generate Java 8 bytecode in the Java Compiler preferences.
Look in Preferences -> Build, Execution, Deployment -> Compiler -> Java Compiler. Check that Project bytecode version is unset (or set correctly) and that your module is not listed in Per-module bytecode version with an incorrect value.
File --> Project Structure-->Module
The language level in here as set to 5 for me. Upped it to 9 to allow classes etc and the same error as described above resolved for me.
I missed the modules in my gradle.build. Had to update
javafx {
version = "11"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}
In Java9+ some modules are not included in JRE/JDK. The solution is to add thore libraries explicitly.
For maven pom file for Java9 I used
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-fxml -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
Than I can use desired libray for including of TextField.
import javafx.scene.control.TextField;
I have a simple JavaFX application and I want to create an installer for Windows machines. The javafx-maven-plugin works and create an executable file of the application along with a Windows installer, but the problem is that, it creates a Windows installer with the JavaFX application inside and the complete JRE too.
So, how can I make to build the native files for Windows with the javafx-maven-plugin without carrying the complete Java framework in it. Perhaps it should create only a depedency to the Java Framework. This bloats the installer from 1.5MB to 200MB of disk space.
With Maven I use the command mvn clean compile jfx:build-jar jfx:native to get the native files in Windows and here is the POM file I am using:
<?xml version="1.0"?>
<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.demo</groupId>
<artifactId>hello-javafx-maven-example</artifactId>
<name>JavaFX Example Maven Project</name>
<organization>
<name>Jaa Demo</name>
</organization>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.1.3</version>
<configuration>
<mainClass>com.demo.helloWorld</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
I'm the maintainer of that javafx-maven-plugin.
When creating your installer, it is "by design" that the JRE is bundled within your application. I myself stumbled upon this too, but in despite of the current documentation of oracle, I found it not possible to run my generated files (using JDK 1.8u40 and above, Windows) without having that JRE aside of my native launcher. The packager.dll seems to require it to be there. But to make it clear: this may be just a problem for me, not verified it nor did I investigate this.
To answer your question: you can remove the JRE by passing some bundleArguments:
<bundleArguments>
<runtime />
</bundleArguments>
I'm doing this myself, mostly to speed up test-projects, like here:
https://github.com/javafx-maven-plugin/javafx-maven-plugin/blob/master/src/it/03-cli-jfx-native/pom.xml#L31
EDIT: please consider of upgrading to plugin-version 8.1.5 since there are some bugs fixed including a (IMHO) serious workaround for native launchers on linux-systems. Also there is support for creating bundles while normal maven-lifecycle: https://github.com/javafx-maven-plugin/javafx-maven-plugin/blob/master/src/it/07-lifecycle-build-jfx-native/pom.xml