18

I am developing JavaFx application in netbeans, in netbeans the project is building and running fine.

I made a build (mvn package) from my project its finished without error but when I launch the program its not loading all the scenes and the FXMLLoader return with null value in this cases.

All .fxml file in the same folder.

public class JavaFXApplication extends Application {

    public static final String TOOLBAR_MAIN = "toolbarMain";
    public static final String TOOLBAR_MAIN_FXML = "/fxml/ToolbarMain.fxml";
    public static final String TOOLBAR_SUB = "toolbarSub";
    public static final String TOOLBAR_SUB_FXML = "/fxml/ToolbarSub.fxml";

    public static final String NEW_SESSION_PANEL = "newSession";
    public static final String NEW_SESSION_PANEL_FXML = "/fxml/NewSessionPanel.fxml";
    public static final String OPEN_SESSION_PANEL = "openSession";
    public static final String OPEN_SESSION_PANEL_FXML = "/fxml/OpenSessionPanel.fxml";
    public static final String CONNECTIONS_PANEL = "connections";
    public static final String CONNECTIONS_PANEL_FXML = "/fxml/ConnectionsPanel.fxml";
    public static final String LOGS_PANEL = "logs";
    public static final String LOGS_PANEL_FXML = "/fxml/LogsPanel.fxml";
    public static final String EXCEPTIONS_PANEL = "exceptions";
    public static final String EXCEPTIONS_PANEL_FXML = "/fxml/ExceptionsPanel.fxml";
    public static final String MESSAGES_PANEL = "messages";
    public static final String MESSAGES_PANEL_FXML = "/fxml/MessagesPanel.fxml";

    public static ScreensController menuContainer = new ScreensController();
    public static ScreensController contentContainer = new ScreensController();

    public static ServerService server = new ServerService();

    public static Stage STAGE;

    @Override
    public void start(Stage primaryStage) throws Exception {

        STAGE = primaryStage;

        primaryStage.setOnCloseRequest((WindowEvent t) -> {
            if (server.isRunning()) {
                server.cancel();
            }
        });

        menuContainer.loadScreen(JavaFXApplication.TOOLBAR_MAIN,
                JavaFXApplication.TOOLBAR_MAIN_FXML);
        menuContainer.loadScreen(JavaFXApplication.TOOLBAR_SUB,
                JavaFXApplication.TOOLBAR_SUB_FXML);

        contentContainer.loadScreen(JavaFXApplication.NEW_SESSION_PANEL,
                JavaFXApplication.NEW_SESSION_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.OPEN_SESSION_PANEL,
                JavaFXApplication.OPEN_SESSION_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.NEW_SESSION_PANEL,
                JavaFXApplication.NEW_SESSION_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.CONNECTIONS_PANEL,
                JavaFXApplication.CONNECTIONS_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.LOGS_PANEL,
                JavaFXApplication.LOGS_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.EXCEPTIONS_PANEL,
                JavaFXApplication.EXCEPTIONS_PANEL_FXML);
        contentContainer.loadScreen(JavaFXApplication.MESSAGES_PANEL,
                JavaFXApplication.MESSAGES_PANEL_FXML);
        menuContainer.setScreen(JavaFXApplication.TOOLBAR_MAIN);
        contentContainer.setScreen(JavaFXApplication.NEW_SESSION_PANEL);

        SplitPane root = new SplitPane();

        root.getItems().addAll(menuContainer, contentContainer);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setResizable(false);
        primaryStage.setTitle("Exam Supervisor");

    }

Screen controller where the screens loaded:

public class ScreensController extends StackPane {

    private HashMap<String, Node> screens = new HashMap<>();

    public void addScreen(String name, Node screen) {
        screens.put(name, screen);
    }

    public boolean loadScreen(String name, String resource) {
        try {
            FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
            System.out.println("name:" + name + " ,resource" + resource + " ,loader:" + myLoader.getLocation());
            Parent loadScreen = myLoader.load();
            ControlledScreen myScreenControler
                    = myLoader.getController();
            myScreenControler.setScreenParent(this);
            addScreen(name, loadScreen);
            return true;
        } catch (Exception e) {
            System.out.println("name: " + name + ", resource" + resource + " ,exception: " + e.getMessage());
            return false;
        }
    }

    public boolean setScreen(final String name) {

        if (screens.get(name) != null) {
            final DoubleProperty opacity = opacityProperty();

            if (!getChildren().isEmpty()) {
                getChildren().remove(0);
                getChildren().add(0, screens.get(name));
            } else {
                getChildren().add(screens.get(name));
            }
            return true;
        } else {
            System.out.println(screens.get(name) + " ,screen hasn't been loaded!\n");
            return false;
        }

    }

    public boolean unloadScreen(String name) {
        if (screens.remove(name) == null) {
            System.out.println("Screen didn't exist");
            return false;
        } else {
            return true;
        }
    }
}

My github repo: https://github.com/eszikk/ExamSuperVisorServer

Jonny C
  • 1,943
  • 3
  • 20
  • 36
eszik.k
  • 1,729
  • 4
  • 17
  • 40
  • You have the fxml as a subfolder of resources: do you have the same structure in the jar file that is created? – James_D May 09 '15 at 20:00
  • Yes.The build created /target/classes/fxml structure and its contains all .fxml files. – eszik.k May 09 '15 at 20:10
  • That wasn't what I asked. In the *generated jar file*, is the fxml folder at the top level, or was it placed under a resources folder? – James_D May 09 '15 at 20:13
  • Yes it is in the top level. Here is the strucutre: `/com, /fxml, /imageRes, /META-INF, /shared, /styles, ` – eszik.k May 09 '15 at 20:17
  • Can you please add a structure of your project [like this](https://stackoverflow.com/questions/24948397/javafx-project-structure/24948550#24948550) or verify if it is in a similar manner? – ItachiUchiha May 13 '15 at 14:09

3 Answers3

4

I looked into your netbeans project files and the maven pom and found nothing strange. The fact that the javafx libs are not contained in the classpath of both configurations tells me that, you are using it from the Java Installation.

Perhaps you use a differrent Java installation from netbeans and when you start the maven target. Then, the fxml module might be missing or there is no java fx installed with the second installation or something else is not compatible.

Did you start the maven target from a console? e.g.

java -jar xyz.jar

Try

java -version

and compare the version with the java version used by netbeans (Help > About).

Netbeans build are done by an internal maven version. But there is also a chance that an external maven build uses a different version of the java compiler than the internal netbeans build.

I hope that helps!

Peter Paul Kiefer
  • 2,114
  • 1
  • 11
  • 16
1

Try to use javafx-maven-plugin Hope, it can help.

Community
  • 1
  • 1
mremne21
  • 46
  • 2
  • typically when someone puts a bounty on their question, they generally are looking for more detail than a simple link. Please explain to the OP why your suggestion will help and in what fashion and how to implement. As it stands this is a poor answer. – nomistic May 19 '15 at 23:16
  • Sorry, i'm new ). i had a similar problem, before i start packing with this plugin, maybe it can help in this case. It's just a suggestion. – mremne21 May 19 '15 at 23:33
0

I have run your application successfully from outside of Netbeans after making the following changes.

Firstly, I was wondering why you have the compiler plugin. So, I removed it and was getting "class file for sun.util.logging.PlatformLogger not found" error during the build.

You have come across the issue as explained in JavaFX8 - sun.util.logging.PlatformLogger not found Exception in NetBeans 8

You need to fix it.

Secondly, what you had was only the compile phase, not a runnable jar.

There might be way to do it using the compiler plugin(or by having other dependent jars in the classpath), but I found it easier to use the assembly plugin to create a jar with all dependencies..

Add the following to your pom.xml

         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.eszik.supervisorserver.main.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>

Next, run the two mvn commands separately.

mvn clean install
mvn assembly:single

And then, java -jar target/SuperVisorServer-1.0-SNAPSHOT-jar-with-dependencies.jar

Community
  • 1
  • 1
KayDK
  • 134
  • 10