1

我在运行 JavaFX 应用程序时收到此“URI 不是分层”错误。异常在下面代码的第 4 行中引发。你知道我该如何解决这个问题吗?

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)
4

2 回答 2

2

问题是您有一个引用JAR 文件内资源的 URI。File(URI)构造函数无法处理这样的 URI 。它只理解文件的 URI。

你需要做的是这样的:

    String text;
    try (InputStream is = MainClass.class.getResourceAsStream("history.txt")) (
        text = IOUtils.toString(is);
    } catch (...
于 2013-01-08T03:16:50.103 回答
0

对于那些想要加载作为资源包含的 FXML 文档的人......

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

您的 fxml 不能包含样式表。

于 2014-10-21T03:52:16.337 回答