This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Launching a document programmatically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Launching a document programmatically" Watch "Launching a document programmatically" New topic
Author

Launching a document programmatically

J Arshad
Greenhorn

Joined: Sep 07, 2004
Posts: 19
Hi,

I need to launch a document (pdf, word, excel, etc) from within my Java program. I googled it & found that :
---------------
Runtime runtime = Runtime.getRuntime();
try {
Process pr = runtime.exec("start" + launchName);
}
catch (IOException e) {
System.out.println("Unable to launch: " + launchName);
}
---------------
should do the trick. But unfortunately, it doesn't work.

Does anyone know how to do this?

Jay
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are a couple ways to do this. The hard way is to find or know the location of the EXE that should open the file. You would specify the full path to WinWord.exe and the name of the word file.

The easier way is to invoke RUNDLL32 as the program and pass along the document name as the argument. This is exactly like typing the document name into a command window or double-clicking a file in Windows Explorer. It launches the program associated with the extension, which is probably (!) the program you want. For grins, open a command window and type the name of some existing Word doc as if it were a command.

If the latter sounds good, Google for "java exec rundll32" and you'll find some samples for sure.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
J Arshad
Greenhorn

Joined: Sep 07, 2004
Posts: 19
Thanks Stan. This seems to work. The full sytanx I find, is as follows:

Runtime.getCurrent().exec("cmd.exe /E:1900 /C myDocument.txt");

as detailed here: http://www.mindprod.com/jgloss/exec.html
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That's an interesting shortcut. I'd seen it done with rundll32 as the program and that seems unnecessary for a local file. Some other folks recently posted "start" as the program. Start would fire up another command instance so the one you exec would seem to end very quickly.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Youc can see at the JACAOB - Java COM Bridge Project. It is a open source project.

Using this you can launch any of the registered applications like Word, Excel, PPT, PDF etc from java program just by writing a few lines of code.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Launching a document programmatically
 
Similar Threads
open MSWord document through Javascript
does proprties file has the ability of hashmap
launching IExplorer with java
Use custom font and also make that font available for other applications
launch associated program of file type