473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to Open PDF file in Java Swings

184 100+
Hi All,
I am very much new to java Swings I need to open the PDF file from Swings application..
I tried as follows...

Runtime r= Runtime.getRuntime();
Process p = r.exec("C:/Program Files/Adobe/Reader 8.0/Reader/AcroRd32.exe Test.Pdf");


it got work.. But is ter any other solution.. because am hard coding the path of the exe file in the above code....
pl help me out ASAP...
Mar 15 '10 #1
11 11661
jkmyoung
2,057 Expert 2GB
1. Use the registry class to determine what file is used to open a .pdf file.
HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer\FileExts\.pdf\OpenWithList
HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Explorer\FileExts\.pdf\OpenWithProgids

You could then handle the case where the user does not have .pdf reader installed, by directing them to FoxIt's website, or some other .pdf reader site.

2.You could pass the name of the exe file in as a String variable. eg get it from your String[] args.
Mar 15 '10 #2
gaya3
184 100+
Thanks. Could you please brief your first solution.. And any other solutions.
Mar 16 '10 #3
pbrockway2
151 Expert 100+
java.awt.Desktop has an open() method.
Mar 16 '10 #4
gaya3
184 100+
Thanks.. but Desktop api are supported only fron java 1.6 rt?.. i need to implement the above functionality in java 1.5 version
Mar 16 '10 #5
gaya3
184 100+
How to open the existing PDF file using iText
Mar 16 '10 #6
jkmyoung
2,057 Expert 2GB
Open up regedit and go to those registry entries. They should show what program you use to open .pdf files by default.

Once you get the program command line, you can simply send that to the Process to exec.
Mar 16 '10 #7
gaya3
184 100+
Hi ,
I tried as follows

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.google.com");

am able to open the google page.. where as when i tried to open a pdf file .. it is not able to do so..

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler C:/pdf/test.pdf");


Do any one faced this issue? Please help me out.
Mar 17 '10 #8
jkmyoung
2,057 Expert 2GB
You're using url.dll to open a non-url.
Mar 17 '10 #9
gaya3
184 100+
can you please suggest some solution for opening non-url?
Mar 18 '10 #10
jkmyoung
2,057 Expert 2GB
Registry method: There won't be a standard answer for all types, unless you use the registry method. Even then you still have the problem of 'What if there is no designated program to open files of this type?'

If you know for sure what type of files you will be dealing with, you may be able to find a particular dll that works with it.

You could also probably go
r.exec("C:/pdf/test.pdf");
and let your computer deal with it.
Mar 18 '10 #11
Expand|Select|Wrap|Line Numbers
  1. try {
  2.  
  3.                         if ((new File("c:\\OFFER_LETTER_1.pdf")).exists()) {
  4.  
  5.                             Process p = Runtime
  6.                                     .getRuntime()
  7.                                     .exec("rundll32 url.dll,FileProtocolHandler c:\\OFFER_LETTER_1.pdf");
  8.                             p.waitFor();
  9.  
  10.                         } else {
  11.  
  12.                             System.out.println("File is not exists");
  13.  
  14.                         }
  15.  
  16.                         System.out.println("Done");
  17.  
  18.                     } catch (Exception ex) {
  19.                         ex.printStackTrace();
  20.                     }
Sep 17 '13 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
2
by: nissiml | last post by:
hi, i'm trying to open a asp.net web page that list files from a Windows application like winword and select a file from it . what do i have to do to make it happen, is it simple ? Thanks in...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
1
by: manju1983 | last post by:
hello hi everyone, i am doing project using java swings now i have created a Jframe .that jframe contains 3 text fields and 2 jbuttons. now the problem is , when i press the submit button ,...
1
by: swatiawasthy | last post by:
hi i am developing a gui in java swings and i am getting my database displayed in jtable... i have a textfield labeled CUSTID and my table also has its sec column as CUSTID (which is not a primary...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.