使用桌面的默认应用程序打开存储在jar文件中的PDF文件

使用桌面的默认应用程序打开存储在jar文件中的PDF文件

本文介绍了使用桌面的默认应用程序打开存储在jar文件中的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我即将结束计算机科学课程的程序开发。但是,其中一个要求是在应用程序中包含用户手册。我在Eclipse的工作区内将用户手册保存为PDF。
它存储在/ Documents / PDF Manual.pdf下。我最初使用此代码:

I'm nearing the end of a program development for my computer science course. However, one of the requirements is to have a user manual within the app. I saved the user manual as a PDF inside Eclipse's workspace.It is stored under "/Documents/PDF Manual.pdf". I originally used this code:

URL url = getClass().getResource( fileSeparator + "Documents" + fileSeparator + "PDF Manual.pdf");
           //fileSeparator = '/' on mac, & '\\' on windows
        File userManual = new File (url.toURI());
        if (userManual.exists())
        {
            Desktop.getDesktop().open(userManual);
        }

从eclipse运行项目时工作正常,但URI返回非分层程序导出到jar文件时的异常(按预期)。我想到了使用url.toString(),使用substring和replaceAll()去除不需要的字符(空间的%20),但是这给出了奇怪的结果,当然代码在它没有时也无法正常工作是一个jar文件。

This works fine while running the project from eclipse, however URI returns a non hierarchical exception (as expected) when the program is exported to a jar file. I thought of playing around with url.toString(), using substring and replaceAll() to get rid of unwanted characters (%20 for the space), however this gave weird results and of course the code wouldn't work properly when it wasn't a jar file.

我查看了InputStream,但这仅用于从文件中读取,我无法使用桌面应用程序打开该文件。

I looked at InputStream, however this is only used to read from a file and I cannot open the file using a desktop app.

由于提交过程,pdf HAS要保存在项目文件夹中。

Due to the process of submission, the pdf HAS to be saved inside the project folders.

此外,我的代码必须是独立于平台的(或者至少在windows和mac上工作),因此操作文件名变得复杂得多。有什么建议?

Also, my code has to be platform independent (or at the very least, work on windows and mac) and thus manipulating file names becomes a lot more complicated. Any suggestions?

在@SubOptimal的帮助下,这是我现在使用的代码:

After @SubOptimal 's help, this is the code I am now using:

String inputPdf = "Documents" + fileSeparator + "PDF Manual.pdf";
        InputStream manualAsStream = getClass().getClassLoader().getResourceAsStream(inputPdf);

        Path tempOutput = Files.createTempFile("TempManual", ".pdf");
        tempOutput.toFile().deleteOnExit();

        Files.copy(manualAsStream, tempOutput, StandardCopyOption.REPLACE_EXISTING);

        File userManual = new File (tempOutput.toFile().getPath());
        if (userManual.exists())
        {
            Desktop.getDesktop().open(userManual);
        }

这适用于mac。但是,在Windows上,由于某些未知原因,AsStream为null。

This works on mac. However, on windows manualAsStream is null for some unknown reason.

推荐答案

完整的工作示例。在Windows环境中测试。

Full working example. Tested in Windows environment.

文件结构

.\REPL.java
.\doc\manual.pdf
.\manifest.mf

REPL.java

package sub.optimal;

import java.io.InputStream;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.awt.Desktop;

public class REPL {

    public static void main(String[] args) throws Exception {
        String inputPdf = "doc/manual.pdf";
        Path tempOutput = Files.createTempFile("TempManual", ".pdf");
        tempOutput.toFile().deleteOnExit();
        System.out.println("tempOutput: " + tempOutput);
        try (InputStream is = REPL.class.getClassLoader().getResourceAsStream(inputPdf)) {
            Files.copy(is, tempOutput, StandardCopyOption.REPLACE_EXISTING);
        }
        Desktop.getDesktop().open(tempOutput.toFile());
    }
}

manifest.mf

Main-Class: sub.optimal.REPL

编译

javac -d . REPL.java

创建JAR

mkdir dist\
jar cvfm dist/REPL.jar MANIFEST.MF sub/optimal/REPL.class doc/manual.pdf

执行JAR

cd dist\
java -jar REPL.jar

这篇关于使用桌面的默认应用程序打开存储在jar文件中的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:26