Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

java - Running JavaFX 11 project from Intellij and from Maven gives different results

I just developed my first JavaFX application in the form of an IntelliJ Maven project in Java 11 and got to the point that it works as intended. Up to this point, I only used Maven to handle dependencies and IntelliJ to compile and run my project.
Then I moved on to try to package the project in the form of a JAR file and found out that, due to Java 11 not including JavaFX anymore, the JAR generation has become harder (yes I know, I should have checked this earlier). Reading JavaFX 11's docs I decided to try to generate the JAR file using Maven, instead of using IntelliJ's artifact build (which maybe doesn't even work anymore for JavaFX 11 projects).
So I added the required dependencies in my pom.xml file, most notably the JavaFX plugin and JavaFX libraries (up to this point IntelliJ was loading JavaFX from a directory that I specified in the project settings, as explained in ItelliJ's docs).
Before trying to actually build my JAR package I checked if I was able to compile and run the project using the javafx:run goal of JavaFX's plugin, and finally we arrive at my problem: my application looks different when compiled with Maven from the version compiled with IntelliJ:

Correct IntelliJ version

Correct IntelliJ version

Wrong Maven version

Wrong Maven version

Most notably, seems that it's not loading the image at the bottom of the menu, but I'm not getting any warning or error about loading that image. Also, I can get the buttons to fully show by moving the mouse cursor on the window, but the image never shows nor the image box moves to the right position.
The image in question is located, together with all the other resources, in src/main/resources (which should be the default resource path for Maven, and the other resources seem to be loaded correctly). The only difference that I could find, is that I'm loading all the other resources from my Java code, while that image is loaded inside an FXML file, which is in the same resources directory as the image.
To complete my question, I'll put it here

  • my Main.class file, where I'm creating the menu you see in the screenshots:
package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.File;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends Application {
    public static Stage stage;
    private final static Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private static String tempDir;
    private final static String projectName="ReportManager";


    @Override
    public void start(Stage primaryStage) throws Exception{ //https://stackoverflow.com/questions/26325403/how-to-implement-language-support-for-javafx-in-fxml-documents
        if(getClass().getResource("/sample.fxml")!=null){
            ResourceBundle languageResourceBundle = ResourceBundle.getBundle("languageResources"); //Loads bundle for the default JMV locale
            Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"), languageResourceBundle);
            primaryStage.setTitle("ReportManager");
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/bar-chart-2.png"))); //Sets the icon for the window
            primaryStage.setScene(new Scene(root)); //If I don't specify width and height it auto-resizes using values from fxml file
            this.stage=primaryStage;
            primaryStage.show();
        }
        else{
            System.err.println("Can't start GUI: got null instead of fxml file");
            logger.severe("Can't start GUI: got null instead of fxml file");
            return;
        }
    }

    public static String getTempDir() {
        return tempDir;
    }


    public static void main(String[] args) {
        logger.setLevel(Level.WARNING);
        tempDir=System.getProperty("java.io.tmpdir") + File.separator + projectName;
        if(!new File(tempDir).mkdir()){
            logger.info("Failed to create temporary directory, maybe it already exists?");
        }
        launch(args);
    }
}
  • The FXML file I'm using to generate that menu:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="354.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="creaReportButton" alignment="CENTER" mnemonicParsing="false" onAction="#avviaReportApplication" text="%generateReport" />
      <Region prefHeight="19.0" prefWidth="400.0" />
      <Button fx:id="registraReportButton" alignment="CENTER" mnemonicParsing="false" onAction="#registraSullaBlockchain" text="%registerDocument" />
      <Region prefHeight="19.0" prefWidth="400.0" />
      <Button mnemonicParsing="false" onAction="#verificaReportSullaBlockchain" text="%verifyDocument" />
      <Region layoutX="10.0" layoutY="36.0" prefHeight="19.0" prefWidth="400.0" />
      <Button mnemonicParsing="false" onAction="#settings" text="%settings" />
      <Region layoutX="10.0" layoutY="126.0" prefHeight="51.0" prefWidth="400.0" />
      <ImageView fitHeight="135.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@logo.jpg" />
         </image></ImageView>
   </children>
</VBox>
  • 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>Interface</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.5</version>
                <configuration>
                    <mainClass>sample.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
    <dependency>
    <groupId>com.eternitywall</groupId>
    <artifactId>java-opentimestamps</artifactId>
    <version>1.19</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
            </exclusion>
        </exclusions>
</dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.6.0</version>
        </dependency>


       <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12.0.2</version>
       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just copied mine:

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-base</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>13.0.2</version>
    </dependency>

Adapt the versions to 12. javafx-web is probably not needed when not using WebView. My guess: the graphics.

Trivial answer, hence: the VBox could do with a setFillWidth(true) and setPadding(new Insets(10))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...