Search code examples
macosmavenintellij-ideajavafxrabbitmq

Why JavaFX and RabbitMQ do not work together?


On some reason JavaFX and RabbitMQ do not want to work together in project! When I have my JavaFX lib added the classes cannot import RabbitMQ methods even tho the library is connected too. When I am deleting the JavaFX libraries RabbitMQ works perfectly fine. Are these simply incompatible or am I missing some crucial setting?

You can see my module-info.java file below:

 module org.example.demo {
    requires javafx.controls;
    requires javafx.fxml;

    opens org.example.demo to javafx.fxml;
    exports org.example.demo;
}

And my pom.xml 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.10.0</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>19.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>19.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>19</source>
                    <target>19</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>org.example.demo/org.example.demo.MessengerApplication</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Solution

  • Finding the Rabbit MQ client module name

    Rabbit amqp-client is an automatic module with an automatic module name defined in META-INF/MANIFEST.MF in the amqp-client-5.20.0.jar file.

    It has the value:

    Automatic-Module-Name: com.rabbitmq.client
    

    Requiring the Rabbit MQ client module in your module-info.java

    To use amqp-client as a module, you need to require this module in your module-info.java:

    requires com.rabbitmq.client;
    

    So, your module-info.java will become:

    module org.example.demo {
        requires javafx.controls;
        requires javafx.fxml;
        requires com.rabbitmq.client;
    
        opens org.example.demo to javafx.fxml;
        exports org.example.demo;
    }
    

    Info on module path settings

    You also need to have amqp-client.jar (and it's transitive dependencies) on your module path (this is done using the -p or --module-path VM argument for the java process).

    As you are using a recent version of Maven for builds, Maven will understand that this is a modular project, and automatically place required modules on the module path. Some modern IDEs (such as IntelliJ Idea) integrate with Maven to define projects within the IDE and handle the module path definition for such projects automatically.

    Usually, you don't need to worry about manually setting the module path for the javac or java processes.