1

I am getting this "URI is not hierarchical" error when I run a JavaFX application. The exception is thrown in line 4 of the code below. Do you know how could I fix this?

001          { // HISTORY
002              try {
003                  URI uri = MainClass.class.getResource("history.txt").toURI();
004                  File f = new File(uri);
005                  String text = FileUtils.readFileToString(f);
006                  historyTextArea.setText(text);
007              } catch (URISyntaxException | IOException ex) {
008                  Constants.LOGGER.log(Level.SEVERE, ex.toString());
009              }
010          }

.

URI is not hierarchical
file:/C:/Users/Carlos/Desktop/projetos_java/CodePaster/dist/CodePaster.jar!/com/googlecode/codepaster/gui/about.fxml
  at java.io.File.<init>(File.java:392)
  at com.googlecode.codepaster.gui.AboutWindowController.initialize(AboutWindowController.java:142)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2152)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2742)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2694)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2683)
  at com.googlecode.codepaster.MainClass.start(MainClass.java:97)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
  at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
  at java.lang.Thread.run(Thread.java:722)

2 Answers 2

2

The problem is that you have a URI that refers to a resource inside a JAR file. The File(URI) constructor cannot cope with a URI like that. It only understands URIs for files.

What you need to do is something like this:

    String text;
    try (InputStream is = MainClass.class.getResourceAsStream("history.txt")) (
        text = IOUtils.toString(is);
    } catch (...
Sign up to request clarification or add additional context in comments.

2 Comments

Ah yes... you are right. It was working before, but I think I had that file outside the jar. Thanks!
Wonderful! This is my code now: <code> { // HISTORY try (InputStream inputStream = MainClass.class.getResourceAsStream("history.txt");) { String text = IOUtils.toString(inputStream); historyTextArea.setText(text); } catch (IOException ex) { Constants.LOGGER.log(Level.SEVERE, ex.toString()); } } </code>
0

For those wanting to load an FXML document included as a resource....

Parent page = new FXMLLoader().load(getClass().getResourceAsStream("/fxml/Gui.fxml"));

Your fxml cannot include a Stylesheet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.