简体   繁体   中英

Opening PDF only works in netbeans

I have written a program that creates PDF documents. After creating, the PDF's must be opened so it can be printen.

i have the following code, but it only works if i launch from netbeans. Could anyone give me some pointers?

 public void openPDF()
    {
        try {
            System.out.println("Opening PDF");
            File file = new File(pdfPath+pdfName);
            String absolutePDFpath = file.getAbsolutePath().replace(""+(char)92,""+(char)92+(char)9);
            System.out.println("Path = "+absolutePDFpath);
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + absolutePDFpath);
        } 
        catch (IOException ex) 
        {
            JOptionPane.showMessageDialog(null, "Er is een fout opgetreden tijdens het openen van het PDF"
                    + " document\nFoutcode: 0xFF05");
            Logger.getLogger(PrintJob.class.getName()).log(Level.SEVERE, null, ex);
        }

See Desktop.open(File) for a way to open a PDF across different platforms. Something like this..

File file = new File(pdfPath+pdfName);
Desktop.getDesktop().open(file);

If the app. needs to support Java 1.5 or earlier (before Desktop was available), stick with exec() , but implement all the recommendations of When Runtime.exec() won't .

There are a number of things that using a Process requires the programmer to do, for reliable running. That code does none of them.

Probably your code doesn't work because you don't specify environment variables. It should be something like this:

Runtime.getRuntime().exec("command to execute", env[]);

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