0

this is my first post on Stack, I was looking for answer on my question, but I haven't found anything useful. I'm learning Java, so be gentle ;)

My problem is, that I don't want to make One file with everything in "jar". I must load FXML file, which is out of *.jar file. For example: I'm making FXML file which is in my JavaFXMLApplication folder, I run "JavaFXApplication.jar" file and this Application use FXML which I just made.

I hope I describe it clearly :)

EDIT: I have found another solution, which works better for me...

    File file = new File("absolutPathToFile\\FXMLDocument.fxml");
    InputStream is = new BufferedInputStream(new FileInputStream(file));

    Pane root = new Pane();

    FXMLLoader loader = new FXMLLoader();
    try
    {
        root = loader.load(is);

    }
    catch (IOException ex)
    {
        System.out.println(ex);
    }

I hope it will help someone

Cheers :)

Regemaster
  • 11
  • 4

1 Answers1

1

You can load FXML files from anywhere. One possibility is to use the load command which takes an URL as an argument. You can get that URL easily like this:

    try {
        File file = new File("../../FXMLDocument.fxml");
        if (file.canRead()) {
            URL url = file.toURI().toURL();
            Pane root = FXMLLoader.load(url);
        } else {
            System.err.println("File does not exist.");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
mipa
  • 10,369
  • 2
  • 16
  • 35
  • I think the question wants to fetch the fxml out of the jar rather than as a separate file (in which case [getResource()](http://stackoverflow.com/a/19603055/1155209) would be a more applicable example). – jewelsea Apr 01 '16 at 08:15
  • Hmm on re-reading the question, maybe I interpreted it wrong and the asker does actually want to load an fxml file which is not in the jar. Stackoverflow doesn't let me reverse the downvote :( – jewelsea Apr 01 '16 at 08:18
  • I'm Getting Exceptions when I used sth like this: File file = new File("../../FXMLDocument.fxml"); String url = file.toURI().toURL().toExternalForm(); Pane root = FXMLLoader.load(getClass().getResource(url)); What am I doing wrong? – Regemaster Apr 01 '16 at 08:41
  • Which exception? Stack trace? – mipa Apr 01 '16 at 12:48
  • Sorry for delay... I get this exception: http://pastebin.com/mCHMpqwf Thanks for your help @mipa – Regemaster Apr 04 '16 at 06:37
  • I updated the example in my answer. You must not use the getClass().getResource() stuff in your case. – mipa Apr 04 '16 at 08:43
  • Thanks @mipa, it works :) all I needed to change is to take "Pane root = new Pane();" outside if statement and in 'if' "root = FXMLLoader.load(url);" – Regemaster Apr 04 '16 at 12:56