• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Launching a document programmatically

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic