How to open a PDF file in Java

In this article, we show you two ways to open a PDF file with Java.

1. rundll32 – Windows Platform Solution

In Windows, you can use “rundll32” command to launch a PDF file, see example :


package com.mkyong.jdbc;

import java.io.File;

//Windows solution to view a PDF file
public class WindowsPlatformAppPDF {

	public static void main(String[] args) {

	  try {

		if ((new File("c:\\Java-Interview.pdf")).exists()) {

			Process p = Runtime
			   .getRuntime()
			   .exec("rundll32 url.dll,FileProtocolHandler c:\\Java-Interview.pdf");
			p.waitFor();
				
		} else {

			System.out.println("File is not exists");

		}

		System.out.println("Done");

  	  } catch (Exception ex) {
		ex.printStackTrace();
	  }

	}
}

2. Awt Desktop – Cross Platform Solution

This Awt Desktop cross platform solution is always recommended, as it works in *nix, Windows and Mac platforms.


package com.mkyong.io;

import java.awt.Desktop;
import java.io.File;

//Cross platform solution to view a PDF file
public class AnyPlatformAppPDF {

	public static void main(String[] args) {

	  try {

		File pdfFile = new File("c:\\Java-Interview.pdf");
		if (pdfFile.exists()) {

			if (Desktop.isDesktopSupported()) {
				Desktop.getDesktop().open(pdfFile);
			} else {
				System.out.println("Awt Desktop is not supported!");
			}

		} else {
			System.out.println("File is not exists!");
		}

		System.out.println("Done");

	  } catch (Exception ex) {
		ex.printStackTrace();
	  }

	}
}

Reference

  1. http://download.oracle.com/javase/6/docs/api/java/awt/Desktop.html

66 comments on “How to open a PDF file in Java

  1. Thank you Thank you I can’t thank you enough had this error for a week now

    Reply
  2. this program is useful if the default pdf viewer is chrome…. but in my case i want to open pdf viewer as chrome even though chrome it’s not a default pdf viewer…. how can i get that…

    Reply
  3. When I use this in a JavaFX application to open a .pdf file on Ubuntu Linux the application hangs. Any idea what the problem might be, or if there is a work-around?

    Thanks.

    Reply
  4. How to read pdf file inside in jframe using jpanel, jtextarea, jlable, etc…

    Reply
  5. Hi sir, I have one dought how to read pdf file in jframe

    Reply
  6. Hey, do you known how to use Desktop.getDesktop().open(pdfFile); to open pdf file in full screen?

    Reply
  7. Dear sir

    How can i find if the file is being opened in the runtime from any directory

    Reply
  8. My question is little different from the context . I need advice from the respected mentors.

    How to open a pdf or any external program in a new process using your cross platform solution?

    Reply
  9. Hi ,
    Is it possible to open the pdf in adobe reader pro via java ?

    Reply
  10. hi mkyong, actually i want to display pdf file in browser or in Jtable on button click but my pdf file is stored in db. so ho to do this..

    Reply
  11. How to open a pdf file from menuconfig.xml in struts2 application.Can somebody please help me out.

    Reply
  12. how to open dynamically generated pdf file(stored in server) in browser????

    Reply
  13. sir

    Runtime.getRuntime().exec(C:\\Documents and Settings\\car\\samples)
    In The Above Command Space Between The Documents,and,Settings Are showing The Error
    What Can I Do For It……………

    Reply
  14. I want to close the pdf opened using option-1.
    I tried p.destroy() but it closes the process but not the pdf.

    Reply
  15. dear mykoung

    can you pls post a generic way of opening any file in windows and unix system

    for example i will have c://test/test/pdf

    but in unix i will under dir test/tmp/files

    Pleae help

    Reply
  16. If you want to obtain much from this paragraph then you have to apply such strategies to your won webpage.

    Reply
  17. I have work example fine but when i have use this example with tomcat than not working.
    Steps:
    1) Prepare one web project
    2) Write your sample code and put pdf file on webapps folder.

    This example working fine when i test in local but when i use this in network that time this is working but some issue like i have upload project in one machine and run from other machine that time file is open in uploaded machine not client.

    I want to know how can i open pdf(Which is located at server or any URL) file at client side.

    Reply
  18. I am now not certain the place you’re getting your information, however good topic. I needs to spend some time studying more or working out more. Thank you for excellent info I was in search of this information for my mission.

    Reply
  19. Love reading ur articles.. very simple and fantastics.. Good work!

    Reply
  20. thanks a lot! this is the thing that I searched for my project! 😀

    Reply
  21. Hello again, I have got it working. I want to run a .bat file to open Apache Tomcat server. On Windows PC the command prompt must stay open after the file is run for the server to stay running.For the code you have given the command prompt closes after running the batch file.Any suggestions on how I can modify the code?

    Reply
  22. Hey,
    I am new to java. I tried using both codes and got the error “java.lang.NoClassDefFoundError”. Where could I have gone wrong?

    Thanks

    Reply
      1. hi sir how to open pdf file via java button if my pdf is in project folder ? i have tried desktop and other method
        in my computer the pdf opens but if i want to open the pdf in other computer the jbutton don’t do anything if i run my project in other computer please help..

        Reply
  23. i am android developer so i want to make a application
    how we open the word file and pdf file in my mobile devices

    Reply
  24. Very usefull, i want to to the same but using C++ any help?

    Reply
  25. Hi All,

    I want to read the byte stream from the web service and display the same as PDF to the user. Can someone please help me on that.
    I have tried various examples in google, it is working fine in IE, but somehow in Mozilla and chrome, for PDF’s greater than 15 kb, it is printing the byte response directly on the browser window.

    Any help will be really appreciated.

    Reply
  26. Hello,

    It appears that the first method allows you to wait for the process to complete whereas the second creates a child process and leaves it alone. In the second method, you lose some control. Is there an easy way for the program to wait for desktop.open in a in a thread in the second case ?

    thanks

    Reply
  27. hey mkyong,
    i want to open a an excel-file when i click on a JPopUpMenu
    how to do?

    Reply
  28. How can I open a pdf file with parameters?
    For example: “..\\test.pdf” “page=3”

    Thanks,

    Tim

    Reply
  29. Sir wonderful examples are given.. It is so helpful keep on posting

    Reply
  30. Hi Mkyong,

    How to open a excel file in Java?Thank You.

    Reply
  31. thanks a lot we use this codes you help us to much

    Reply
    1. Simply does not work. Pdf file never opens on Desktop.

      Reply
      1. It’s working fine in my pc and many others. Do you have any error message?

        Reply
    2. Greatest Apportunity For the Greatest Chance

      Reply
  32. Nice, an addition could be to check if the file exists. This can be done with the following statement:

    if( (new File(“c:/Java-Interview.pdf”)).exists() )

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *