- Make sure to have homebrew installed.
Then install JDK FX Package
and maven
:
brew install maven # used for managing our whole project, and the packages
Be sure to specify the correct JDK version, when downloading the package, you can check your version like this:
java --version
# openjdk 23.0.1 2024-10-15
# OpenJDK Runtime Environment Homebrew (build 23.0.1)
# OpenJDK 64-Bit Server VM Homebrew (build 23.0.1, mixed mode, sharing)
Create a pom.xml
file at the root of your project folder:
<?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.example</groupId>
<artifactId>Lab_07</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<javafx.version>21.0.1</javafx.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.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Be sure to have the maven
standard directory structure:
mkdir -p src/main/java
and move your old entrypoint main.java
to src/main/java
:
mv src/main.java src/main/java/ # or from wherever it is
This is a simple window to check if it works (src/main/java/Main.java
):
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Hello, JavaFX!");
StackPane root = new StackPane();
root.getChildren().add(label);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("JavaFX Application");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
To test if everything is running, use this:
mvn clean javafx:run
IntelliJ
- Convert to Maven Project
- Click
Add Framework Support...
- Check Maven and ok, this will craete apom.xml
file - Copy the Maven Dependencies
- Use the config we used above, and
load Maven changes
- Do the same file-project restructuring as above
- (Optional) if the project was using
Java Modules
(if it has amodule-info.java
), make sure it includes:requires javafx.controls; requires javafx.fxml;
- Update Run Configuration
- Click
Run -> Edit Configurations...
- Remove any old configurations - Add Maven, set command line toclean javafx:run
VSCode
- Do all of the above.
- To get rid of import errors, add this to the
.vscode/settings.json
file:
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"java.compile.nullAnalysis.mode": "automatic",
"java.maven.downloadSources": true
}
- Also create a '.classpath' file so that VSCode recognizes this as a Maven project:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
- Lastly, do a `Cmd+Shift+P` -> select `Java: Clean Java Language Server Workspace` -> `Reload and delete`