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();
}
}
}
Thank you Thank you I can’t thank you enough had this error for a week now
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…
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.
this worked for me:
https://stackoverflow.com/questions/23176624/javafx-freeze-on-desktop-openfile-desktop-browseuri
How to read pdf file inside in jframe using jpanel, jtextarea, jlable, etc…
Hi sir, I have one dought how to read pdf file in jframe
hai i have the same dought?|
hey even i have the same doubt………..
Hey, do you known how to use Desktop.getDesktop().open(pdfFile); to open pdf file in full screen?
Dear sir
How can i find if the file is being opened in the runtime from any directory
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?
Hi ,
Is it possible to open the pdf in adobe reader pro via java ?
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..
Works well Thanks…!!!
its works
How to open a pdf file from menuconfig.xml in struts2 application.Can somebody please help me out.
Thanks my friend. Has solved my doubt!
how to open dynamically generated pdf file(stored in server) in browser????
IT’s Working very well. thanks
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……………
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
Good one mate! Very helpful…
I want to close the pdf opened using option-1.
I tried p.destroy() but it closes the process but not the pdf.
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
If you want to obtain much from this paragraph then you have to apply such strategies to your won webpage.
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!
How can i open pdf in solaris machine/server
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.
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.
Love reading ur articles.. very simple and fantastics.. Good work!
thanks a lot! this is the thing that I searched for my project! 😀
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?
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
Which class is not found? Please post your last error caused by.
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..
Validate your input fields
its really working … thanks a ton 🙂
lolx, of course it’s working 🙂
i am android developer so i want to make a application
how we open the word file and pdf file in my mobile devices
Very usefull, i want to to the same but using C++ any help?
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.
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
hey mkyong,
i want to open a an excel-file when i click on a JPopUpMenu
how to do?
How can I open a pdf file with parameters?
For example: “..\\test.pdf” “page=3”
Thanks,
Tim
You may need to try this iText jar library.
This is very useful for me.
Very useful… Thanks a millions…
ThanX, It was very useful
Thank You Very much Working very nice
Working Very Nice Thanks Alot
Thank you sir!!!
THANK U SIR, UR CODE HELPED US LOT……..
Sir wonderful examples are given.. It is so helpful keep on posting
Hi Mkyong,
How to open a excel file in Java?Thank You.
Above solution should open excel file no problem.
However, to manipulate the excel file, you may need third party library like Apache POI.
http://poi.apache.org/
https://mkyong.com/spring-mvc/spring-mvc-export-data-to-excel-file-via-abstractexcelview/
Tangible Sir
thanks a lot we use this codes you help us to much
Simply does not work. Pdf file never opens on Desktop.
It’s working fine in my pc and many others. Do you have any error message?
Greatest Apportunity For the Greatest Chance
Thank you!! this code helped me so much…
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() )
Thank you very much mate, you are a life saver!
You r Great
Maxxxxxx…………
Thanks for sharing this. I was looking for this. Very well explained.