简体   繁体   English

从jar文件夹打开pdf文件时出错

[英]error while opening pdf files from jar folder

I am making a Software in whihch I have to display the pdf files. 我在制作软件时必须显示pdf文件。 I have stored the pdf files in my project folder. 我已经将pdf文件存储在我的项目文件夹中。 The software runs perfectly fine. 该软件运行良好。

But when I cleand and build the project and then i run my jar exe file the pdf files doesnt open. 但是,当我清理并构建项目后,再运行我的jar exe文件时,pdf文件不会打开。

After some experiments I included my pdf files in SRC folder and then clean and build the project. 经过一些实验,我将pdf文件包含在SRC文件夹中,然后清理并构建项目。 The difference i found is that now the jar file is bigger in size ( it equals to sum of all the pdf files) , I thought that this time it would work . 我发现的区别是,现在jar文件的大小更大(它等于所有pdf文件的总和),我认为这次可以正常工作。 But it didnt work. 但是它没有用。

Then After more experiments I included all the files in the Dist folder. 然后经过更多实验,我将所有文件都包含在Dist文件夹中。

Then The jar files can open the pdf files :) , I was happy but not satisfied, Since I only have to create a jar file and seeing all the pdf files in the project folder with one jar files looks really awkard and senseless, is thier anyway i can open the pdf files using only the jar file without copying the pdf files in the folder where my jar file is stored, . 然后jar文件可以打开pdf文件:),我很高兴但不满意,因为我只需要创建一个jar文件,然后看到一个文件夹中的项目文件夹中的所有pdf文件看起来真的很笨拙,毫无意义,无论如何,我可以仅使用jar文件打开pdf文件,而无需将pdf文件复制到存储jar文件的文件夹中。 This is the code i used to open a file named "aleemullah resume". 这是我用来打开名为“ aleemullah resume”的文件的代码。

try{
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "aleemullah resume.pdf");
}
catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error");
}

It looks as if you are reading the PDF file from your current working directory (that is, the folder that your program is launched from). 好像您正在从当前工作目录(即程序从其启动的文件夹)中读取PDF文件一样。 By that logic, you should be able to open a PDF stored anywhere by entering it's full path, rather than just the file name. 按照这种逻辑,您应该能够通过输入完整路径而不是文件名来打开存储在任何地方的PDF。 For instance: 例如:

String filePath = "C:\Users\you\Desktop\aleemullah resume.pdf"
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + filePath);

Change the filePath variable to wherever you are storing the PDF file. filePath变量更改为您存储PDF文件的位置。

You might also consider using a JFileChooser to select the file if you want the user to choose the PDF during runtime. 如果希望用户在运行时选择PDF,也可以考虑使用JFileChooser选择文件。 Check out this example of how to open a dialog box from Java to pick a PDF file and retrieve its path. 请查看此示例 ,了解如何从Java打开对话框以选择PDF文件并检索其路径。

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

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