1

I am running an JavaFX project in IntelliJ successfully but when I am trying to build the artifacts and run the jar outside it won't open or it's opening and closing right away.

I tried to move the .fxml files into java directory and change the load but I get an error.

My project sturcture is this:

enter image description here

And my Main class is here:

public class Main extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
        primaryStage.setTitle("xxxxxxxxxxxxx");
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

I have selected all jar libraries that I use and added them in artifacts. My guess is that it doesn't load the .fxml files outside IntelliJ.

If is needed my sample.fxml is here:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="650.0" prefWidth="550.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <TextField fx:id="username" alignment="CENTER" layoutX="151.0" layoutY="246.0" prefHeight="25.0" prefWidth="229.0" />
        <PasswordField fx:id="password" layoutX="152.0" layoutY="345.0" prefHeight="25.0" prefWidth="229.0" />
        <CheckBox fx:id="rememberMe" layoutX="207.0" layoutY="418.0" mnemonicParsing="false" text="Remember me ?">
            <font>
                <Font size="13.0" />
            </font>
        </CheckBox>
        <Button fx:id="login" layoutX="199.0" layoutY="542.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="153.0" text="OK" textAlignment="CENTER">
            <font>
                <Font size="14.0" />
            </font></Button>
        <Hyperlink layoutX="203.0" layoutY="446.0" text="I forgot my password !" />
        <Label layoutX="239.0" layoutY="219.0" text="Username:">
            <effect>
                <Lighting>
                    <bumpInput>
                        <Shadow />
                    </bumpInput>
                </Lighting>
            </effect>
        </Label>
        <Label alignment="CENTER" layoutX="240.0" layoutY="317.0" text="Password:" textAlignment="CENTER">
            <effect>
                <Lighting>
                    <bumpInput>
                        <Shadow />
                    </bumpInput>
                </Lighting>
            </effect>
        </Label>
    </children>
</Pane>

Error:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unk
nown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Sou
rce)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown So
urce)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(
Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
        at javafx.fxml.FXMLLoader.load(Unknown Source)
        at Main.start(Main.java:25)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162
(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown
 Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Sourc
e)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown S
ource)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
        ... 1 more
Exception running application Main
Marian Pavel
  • 2,726
  • 8
  • 29
  • 65
  • Try running the jar from the terminal using `java -jar `and check for any errors. – ItachiUchiha Nov 17 '15 at 13:50
  • I didin't know about that command, is my first java application using javafx on windows. I am android developer. I am editing the questions with the problem. Thank you. – Marian Pavel Nov 17 '15 at 13:53
  • My guess was right: `Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));` this line is the problem – Marian Pavel Nov 17 '15 at 13:56
  • The exception says that it cannot find the fxml file. Can you unzip the jar and check if the fxml file is present in it ? – ItachiUchiha Nov 17 '15 at 13:56
  • No it isn't, how can I add it ? – Marian Pavel Nov 17 '15 at 13:59
  • How are you creating the jar file? – ItachiUchiha Nov 17 '15 at 14:01
  • Project Structure -> Artifacts -> Adding artifact -> Adding all available elements from the right to the left ( all jars ) -> Adding a main class and I Build -> Build artifacts. – Marian Pavel Nov 17 '15 at 14:04
  • Ahh, I see. If you are using Gradle as a build tool, you need to use it to create the jar file. Have a look at [this answer](http://stackoverflow.com/a/21721305/1759128). You can also use Intellij to create a jar file, but it will need additional configurations. – ItachiUchiha Nov 17 '15 at 14:11
  • I managed to figure it out, I used the same way as creating the .jar, added just the resource directory. – Marian Pavel Nov 17 '15 at 14:34

0 Answers0