Java 访问存储在.jar中的PDF文件

Java 访问存储在.jar中的PDF文件,java,file,pdf,file-io,jar,Java,File,Pdf,File Io,Jar,我正在创建一个GUI,我打算添加一个简单的pdf文件作为帮助文件。 我将其包含在参考资料包中,并在这块板上搜索,得到了以下代码: private void GuideActionPerformed(java.awt.event.ActionEvent evt) { if (Desktop.isDesktopSupported()) { try { File myFile = new Fil

我正在创建一个GUI,我打算添加一个简单的pdf文件作为帮助文件。 我将其包含在参考资料包中,并在这块板上搜索,得到了以下代码:

private void GuideActionPerformed(java.awt.event.ActionEvent evt) {                                      
    if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File(("Help.pdf"));
        if (!myFile.exists()) {
            // In JAR
            InputStream inputStream = ClassLoader.getSystemClassLoader()
                                .getResourceAsStream("resources/manual.pdf");
            // Copy file
            OutputStream outputStream = new FileOutputStream(myFile);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
            outputStream.close();
            inputStream.close();
        }
        Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
        // no application registered for PDFs
    }
}
问题是,现在pdf阅读器无法访问该文件,因为java应用程序已经在使用该文件。
有办法解决吗?否则,我只需将文件添加到dist并计算出资源路径。

我尝试了您的代码,效果很好,请检查您是否在源代码中使用文件manual.pdf创建了文件夹资源。另外,当您创建jar时,请检查这些文件是否已导出。

您尝试过吗?InputStream input=getClass.getResourceAsStreamresources/manual.pdf;至少复印了吗?