简体   繁体   中英

error while opening pdf files from jar folder

I am making a Software in whihch I have to display the pdf files. I have stored the pdf files in my project folder. 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.

After some experiments I included my pdf files in SRC folder and then clean and build the project. 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 . But it didnt work.

Then After more experiments I included all the files in the Dist folder.

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, . This is the code i used to open a file named "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). 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. 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.

You might also consider using a JFileChooser to select the file if you want the user to choose the PDF during runtime. Check out this example of how to open a dialog box from Java to pick a PDF file and retrieve its path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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