简体   繁体   English

试图从Jar读取rtf文件到JEditorPane; 在Netbeans工作,而不是从Jar工作

[英]trying to read rtf file from Jar to JEditorPane; works in Netbeans, not from Jar

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. 我试图从位于jar文件中的rtf文件中读取,然后将其显示在JEditorPane中。 This is working from the Netbeans environment, but not when executing the jar file in the dist folder. 这是在Netbeans环境中工作,但不是在dist文件夹中执行jar文件时。 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: (原因是JEditorPane.read需要一个FileInputStream,而我遇到的其他问题的所有解决方案都涉及非File InputStream。以下是有问题的代码:

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! 谢谢!

Use the URL obtained from getResource() to load the document. 使用从getResource()获取的URL来加载文档。 If it is in a Jar, it will not be accessible as a File . 如果它在Jar中,它将无法作为File访问。

Something like this: 像这样的东西:

URL urlToRtf = this.getClass().getResource("files/general.rtf");
descPane.setPage(urlToRtf);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM