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

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

66 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
ilGibbo
15 years ago

Thank you!! this code helped me so much…

Robert Veringa
15 years ago

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() )

pherwani
3 years ago

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

Sathiya
6 years ago

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…

Dean Schulze
8 years ago

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.

kailas
8 years ago

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

furin
8 years ago

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

Anil k
10 years ago

Dear sir

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

Ali Akhtar
10 years ago

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?

Nesh
11 years ago

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

Vikash
11 years ago

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..

Mukesh Storge
11 years ago

Works well Thanks…!!!

tama
12 years ago

its works

Ramana
12 years ago

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

Alaor Resende
12 years ago

Thanks my friend. Has solved my doubt!

gopal
12 years ago

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

Sai Hemanth
12 years ago

IT’s Working very well. thanks

raja
12 years ago

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……………

sehrish
12 years ago

I use Aspose.PDF for Java to read and create my pdf files and they also provide java code also for developers to use in their API. here is the link:

http://www.aspose.com/java/pdf-component.aspx

Alinda
12 years ago

Good one mate! Very helpful…

mano
13 years ago

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

jabbo
13 years ago

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

residencehabiba.net
13 years ago

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

stage perfectionnement golf marrakech
13 years ago

This is very interesting, You’re a very skilled blogger. I’ve joined your rss
feed and look forward to seeking more of your magnificent post.
Also, I’ve shared your website in my social networks!

kamal
13 years ago

How can i open pdf in solaris machine/server

Jatin
13 years ago

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.

Mable
13 years ago

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.

balesh
13 years ago

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

elio
13 years ago

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

Learner
13 years ago

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?