java - pdf 文件无法从 ubuntu 16.04 的 jar 中打开

标签 java pdf jar ubuntu-16.04

我正在使用以下代码从 java 打开 pdf 文件。当我从 IDE 运行应用程序时,该代码可以运行。但是,当生成 jar 并执行它时,代码停止工作。我不知道我做错了什么。我已经尝试更改文件夹的 jar 但它仍然不起作用。问题似乎是 ubuntu 16.04 如何处理路由,因为在 Windows 中它可以正常工作。应用程序不会抛出异常

我获取 pdf 的方式对另一个应用程序执行相同的操作,但在其中我获取了一个图像,并且它在 jar 中和在 ide 中执行时都有效。

 jbTree.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/tree.png")));

Structure of the project

Application

按钮代码

   if (Desktop.isDesktopSupported()) {
        try { 
            File myFile = new File (getClass().getResource("/help/help.pdf").toURI());
            Desktop.getDesktop().open(myFile);
        } catch (IOException | URISyntaxException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

解决方案是通过控制台运行应用程序。尝试以其他方式运行它不起作用。

最佳答案

当您从 IDE 运行项目时,项目的根目录是 System.getProperty("user.dir") 例如,如果您的项目根文件夹是 PDFJar 它将在 PDFJar/src/project/help/ 文件夹中查找 help.pdf

将项目构建为 jar 文件后,将从 dist 或 bin 文件夹构建并执行可执行 jar,该文件夹现在是 System.getProperty("user.dir")帮助.pdf 将在 dist/src/project/help/ 文件夹中查找。

您可以在 dist 或 bin 目录中创建包含 help.pdf 的文件夹 /src/project/help/ ,或者将 Jar 文件放入项目根目录中

EDITED

除了作为输入流之外,您无法访问作为文件打包到 JAR 存档中的资源文件,它在您的 IDE 中工作的原因是因为该文件存在于 src 文件夹作为执行目录是您的项目文件夹。您需要在 JAR 归档之外创建文件,然后将流读入其中,以便您可以调用 Desktop 打开它。

package stackoverflow;

import java.awt.Desktop;
import java.awt.HeadlessException;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author thecarisma
 */
public class StackOverflow {

    public void openHelpFile() {
        OutputStream outputStream = null;
        try {
            File outFile = new File("./help.pdf"); 
            outFile.createNewFile(); //create the file with zero byte in same folder as your jar file or a folder that exists
            InputStream in = getClass().getResourceAsStream("/help/help.pdf");
            outputStream = new FileOutputStream(outFile);
            int read = 0;
            //now we write the stream into our created help file
            byte[] bytes = new byte[1024];
            while ((read = in.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, read);
            }
            if (outFile.exists()) {
                String path= outFile.getAbsolutePath(); 
                JOptionPane.showMessageDialog(null, path);
                if (Desktop.isDesktopSupported()) {
                    JOptionPane.showMessageDialog(null, "Enter");
                    try { 
                        File myFile = new File (path);
                        Desktop.getDesktop().open(myFile);
                    } catch (IOException ex) {
                        JOptionPane.showMessageDialog(null, "Exception");
                    }
                }
            } else {
                JOptionPane.showMessageDialog(null, "Error Occur while reading file");
            }
        } catch (HeadlessException | IOException ex) {
            Logger.getLogger(StackOverflow.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                outputStream.close();
            } catch (IOException ex) {
                Logger.getLogger(StackOverflow.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        StackOverflow stackOverFlow = new StackOverflow();
        stackOverFlow.openHelpFile();
        //the bellow example works for file outside the JAR archive
        /**
        String path= new File("help.pdf").getAbsolutePath(); 
        JOptionPane.showMessageDialog(null, path);
        if (Desktop.isDesktopSupported()) {
            JOptionPane.showMessageDialog(null, "Enter");
            try { 
                File myFile = new File (path);
                Desktop.getDesktop().open(myFile);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Exception");
            }
        } **/
    }

}

DETAIL

当您的资源文件打包到 JAR 存档中时,除了作为文件流之外,它无法作为文件进行访问。该文件在 JAR 存档中的位置将是绝对的,例如 /help/help.file。 如果您只想读取conf、xml、文本文件等资源的内容,您可以将其读入 BufferReader ,即

InputStream in = getClass().getResourceAsStream("/conf/conf.txt"); 
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

否则,如果它是二进制文件,您将需要在 jar 文件外部使用 0 字节 创建一个文件,然后将资源流从 JAR 存档读取到创建的文件中。

注意:在从 JAR 存档中读取之前,您应该检查是否已存在相同大小的帮助文件,以防止多次读取并跳过该过程以增加运行时间。创建文件时请注意,无法在 JAVA 中不存在的文件夹中创建文件。

您可以使用存档管理器打开您的 .jar 文件,以查看其结构

关于java - pdf 文件无法从 ubuntu 16.04 的 jar 中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50194110/

相关文章:

javascript - 无法使用 inAppBrowser cordova 在 android 中打开 pdf url

java - Proguard 问题与 jar 文件,如何找到丢失的 jar?

java - 如何在 Eclipse 中将带有图像库的 Java 项目导出为 jar 文件

java - JGraphT:寻找最短路径而不考虑边缘方向

pdf - 无法再为某些用户从 Google Sheets 电子表格生成 PDF

javascript - 谁能告诉我是否可以使用 javascript 生成 PDF 文件?

java - 无法解析导入 org.eclipse

java - STS 中没有 Maven 的 Spring Boot 项目

java - 如何将 Java 应用程序安装为服务

java - AES 解密 : javax. crypto.IllegalBlockSizeException:解密中的最后一个 block 不完整