Java IO Tutorial

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

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
66 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
ilGibbo
12 years ago

Thank you!! this code helped me so much…

Robert Veringa
13 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
11 months ago

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

Sathiya
4 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
6 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
6 years ago

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

furin
6 years ago

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

Anil k
8 years ago

Dear sir

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

Ali Akhtar
8 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
8 years ago

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

Vikash
8 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
9 years ago

Works well Thanks…!!!

tama
9 years ago

its works

Ramana
9 years ago

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

Alaor Resende
10 years ago

Thanks my friend. Has solved my doubt!

gopal
10 years ago

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

Sai Hemanth
10 years ago

IT’s Working very well. thanks

raja
10 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
10 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
10 years ago

Good one mate! Very helpful…

mano
10 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
10 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
10 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
10 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
11 years ago

How can i open pdf in solaris machine/server

Jatin
11 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
11 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
11 years ago

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

elio
11 years ago

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

Learner
11 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?