• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Launching a document programmatically

 
J Arshad
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
J Arshad
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic