0

sorry if this is obvious, but I've been looking for days and I can't seem to find the answer.

Here is the situation. I am trying to read from an rtf file located within a jar file and then display it in a JEditorPane. This is working from the Netbeans environment, but not when executing the jar file in the dist folder. A version of this question has been asked before, but for my application I think I don't think the previous answers apply. (The reason is that JEditorPane.read requires a FileInputStream, and all of the solutions to other problems I have seen have involved non-File InputStreams. Here is the code in question:

descPane = new JEditorPane("application/rtf", "");
   try {
       File test = new File(this.getClass().getResource("files/general.rtf").toURI());
       descPane.read(new FileInputStream(test), null);
    } catch (Exception ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

Any help is very appreciated! Thank you!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

2

Use the URL obtained from getResource() to load the document. If it is in a Jar, it will not be accessible as a File.

Something like this:

URL urlToRtf = this.getClass().getResource("files/general.rtf");
descPane.setPage(urlToRtf);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433