0

I am trying to create a cross-platform JAR which contains:

  • a command line application ("Tool")
  • a JavaFX application ("Viewer")

I followed the solution of: Maven Shade JavaFX runtime components are missing and I created on Windows a JAR from which I can run both the Tool and the Viewer. I copied it to a Linux machine, and as I can still run the Tool in this way, the Viewer hangs, and does not even prints to the std out.

Here is my 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>com.test</groupId>
    <artifactId>crossjavafx</artifactId>
    <version>0.1.0-SNAPSHOT</version>

    <name>${project.artifactId}</name>
    <description>${project.artifactId}</description>

    <properties>
        <java.version>1.11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>11</version>
            <classifier>win</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics </artifactId>
            <version>11</version>
            <classifier>linux</classifier>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.11</source>
                    <target>1.11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

simplified Tool.java:

package crossjavafx;

public class Tool {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}

simplified Viewer.java

package crossjavafx;

public class Viewer {
    public static void main(String[] args) {
        MainViewer.main(args);
    }
}

MainViewer.java

package crossjavafx;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainViewer extends Application {

    @Override
    public void start(Stage stage) {
        try {
            System.out.println("Hello from Viewer");
            Group root = new Group();
            Scene scene = new Scene(root, 400, 300);
            stage.setTitle("Hello");
            stage.setScene(scene);
            stage.show();
        } catch (final Exception e) {
            System.exit(1);
        }
    }

    public static void main(String args[]){
        launch(args);
    }
}

I am executing both applications:

java -cp crossjavafx-0.1.0-SNAPSHOT.jar crossjavafx.Tool // works on win and linux
java -cp crossjavafx-0.1.0-SNAPSHOT.jar crossjavafx.Viewer // works on win, hangs on linux 

Edit I have changed 1.8 to 1.11 below.

           <plugin>
                <configuration>
                    <source>1.11</source>
                    <target>1.11</target>
                </configuration>
           </plugin>

Eventually, I got the following error message when running Viewer on Linux:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fddc02dcf55, pid=20666, tid=20685
#
# JRE version: OpenJDK Runtime Environment (11.0.1+13) (build 11.0.1+13-Ubuntu-3ubuntu116.04ppa1)
# Java VM: OpenJDK 64-Bit Server VM (11.0.1+13-Ubuntu-3ubuntu116.04ppa1, mixed mode, sharing, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [libGL.so.1+0x89f55]

On Windows I am using: OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.4+11) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.4+11, mixed mode)

Piotr G
  • 959
  • 1
  • 7
  • 25
  • Why would anybody ever want to create such a monster jar which contains all run-time libs of all supported operating systems. The way to go is to build a platform-specific installer for each platform you are interested in. – mipa Nov 09 '19 at 10:11
  • @mipa: I need to support just windows and linux, and if I can handle 2 single platform fat jars, I can also handle 1 double-platform fat jar. Have a look at the accepted answer in the link I provided, its author himself suggested the cross-platform solution. One of the advantages of java is that its binary runs on different platforms, so why not use it? Also, the jar I got is 26 MB, is it a monster to you? – Piotr G Nov 09 '19 at 13:45
  • Just to clarify... I didn't suggested a fat jar for cross-platform solutions... I gave an answer on how to do it. As @mipa suggests, the proper way is using an installer. Nonetheless, the old fat jar should work, so I'll have a look – José Pereda Nov 09 '19 at 13:54
  • Why do you set source/target to 1.8? That should be 11. Why don't you add `transformers` with the main class to the shade plugin? What "hangs on linux" means? Does the app start? – José Pereda Nov 09 '19 at 14:04
  • @JoséPereda, Hi Jose, sorry for implying what you did not intend, but since the author did not ask for a cross platform solution, and you told him how to do this, that was my impression. – Piotr G Nov 09 '19 at 14:05
  • ```Why do you set source/target to 1.8``` - this is the intended version. Probably java version in pom should not be 11, but 8. Why would I need transformers for? I have two classes that I would like to call from this jar: Tool and Viewer, so not a single Main class. "Hangs" means, that there is a system process created, but nothing happens, even the "Hello from Viewer" is not printed. – Piotr G Nov 09 '19 at 14:11
  • Intended version should be 11? You say you are using JDK 1.8 on linux to run the jar? Can you tray JDK 11? – José Pereda Nov 09 '19 at 14:29
  • I meant everything should be 1.8, not 1.11. – Piotr G Nov 09 '19 at 15:41
  • So you have the JavaFX 11 classes compiled to 1.8, and you are running on 1.8? For Windows what is your JDK? Does it contain JavaFX? And for Linux? Again, can you try JDK 11? – José Pereda Nov 09 '19 at 19:05
  • @JoséPereda, I have updated everything to Java11 and checked again. I have edited my question providing new information. – Piotr G Nov 09 '19 at 20:48
  • Use `11` instead `1.11`. If you still have issues, try a HelloFX simple sample on Linux so you are sure your system can run JavaFX (there could be some libraries missing). – José Pereda Nov 09 '19 at 20:59

0 Answers0