2

I have a JavaFx project running on Java 10 that runs perfectly in IntelliJ, which, to me, means that I am packaging the multi-module project incorrectly. I have attempted to search for fixes but I haven't found any.

Problem

I "successfully" create a .jar, as in I get no errors from the maven package, but when I right click on it and run, my program crashes because it doesn't find the services and thus can't continue with normal operation.

Example

The code below will output IMPLEMENTATIONS when run via IntelliJ, but NO IMPLEMENTATIONS when run via my .jar

ServiceLoader<Db> loader = ServiceLoader.load(Db.class);
if(!loader.iterator().hasNext()){
    log.error("NO IMPLEMENTATIONS");
}else {
    for(Db db : loader){
        log.error("IMPLEMENTATIONS");
    }
}

top pom.xml build section

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>10</source>
                <target>10</target>
                <showDeprecation>true</showDeprecation>
                <showWarnings>true</showWarnings>
                <compilerArgs>
                    <arg>--add-opens=java.base/java.lang=gson</arg>
                    <arg>--add-exports=javafx.graphics/com.sun.javafx.application=ALL-UNNAMED,program</arg>
                    <arg>--add-exports=javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED</arg>
                    <arg>--add-exports=javafx.controls/com.sun.javafx.scene.control.skin.resources=ALL-UNNAMED</arg>
                    <arg>--add-exports=javafx.base/com.sun.javafx.event=controlsfx</arg>
                </compilerArgs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                    <version>6.1.1</version> <!-- Use newer version of ASM -->
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
                <argLine>-Dlog4j.debug</argLine>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

Db module pom.xml build section

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>10</source>
                    <target>10</target>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.1.1</version> <!-- Use newer version of ASM -->
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Main Project pom.xml build section

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <configuration>
                <mainClass>startup.App</mainClass>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>startup.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>program</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <attach>false</attach>
            </configuration>
        </plugin>
    </plugins>
</build>

I am really unsure what I am doing wrong. Can anyone point me in the right direction?

Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32

2 Answers2

0

The problem might be that entries in META-INF/services/ are lost - that needs a handler for the assembly plugin, see this question with this answer. Perhaps the assembly plugin is too old?

Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
0

Is your code modular or not? If it is, then you also have to declare your services and their usage in your module-info.java file. If your packaged jars actually contain the required files and declarations can be easily checked by unpacking and expecting them.

mipa
  • 10,369
  • 2
  • 16
  • 35