Open java PDF on another machine

Asked

Viewed 525 times

6

I did a function that I can open a PDF, but this function is only working on my local machine. When I run . jar generated on another machine, an error is issued that the Pdfnão file exists.

The section that performs such a function:

 try { 

   java.awt.Desktop desktop = java.awt.Desktop.getDesktop();           
   desktop.open(new File("/manual.pdf")); 

} catch (IOException ex) {
    Logger.getLogger(JFrameInicio.class.getName()).log(Level.SEVERE, null, ex);
    System.out.println(ex);
} 

The PDF file is in the following location: \Documents Netbeansprojects Rectory src rectory

To create the JAR, I use the method package for store that in my mind, creates a jar with all the necessary files.

I think the PDF is being included in the final jar because without putting the PDF in the src folder, the jar is with a final size of 3 MB, my PDF also has 3MB. When I create the jar with the PDF file in the src folder, the final . jar is 6 MB in size (I believe it is the jar + PDF file)

  • 1

    Place the stack error please. It may be a directory issue.

  • Java.lang.illegalArgumentException: The file manual.pdf doesn’t exist But if the error was in the directory, wouldn’t it not work even on my local machine? (both running by the IDE itself and by the jar

  • 2

    You have to create a temporary PDF file on disk, and read the file to this file. The ide runs its uncompressed problem, so it works, already running via jar, pdf is inside the package and does not exist external to it. See this here, it can help. http://stackoverflow.com/a/28808187/5524514

  • I will look thank you very much

2 answers

1

Use a path that will always validate on any machine like "C:/" or use relative paths like the user folder System.getProperty("user.home") or the class/jar path with System.getProperty("user.dir").

Make sure the PDF is in the indicated path.

0

When creating the File object use as argument "./rectory/manual.pdf" whereas in Netbeans what would be the current command line directory is the src folder.

So your piece of code would be:

try { 

   java.awt.Desktop desktop = java.awt.Desktop.getDesktop();           
   desktop.open(new File("./reitoria/manual.pdf")); 

} catch (IOException ex) {
    Logger.getLogger(JFrameInicio.class.getName()).log(Level.SEVERE, null, ex);
    System.out.println(ex);
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.