• Post Reply Bookmark Topic Watch Topic
  • New Topic

Launching a Windows Program  RSS feed

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an app that launches the default windows program for the appropriate file type. I am currently achieving this by using Runtime.exec() passing it "cmd /c C:\image.jpg" .

I have recently run into some issues with this and was wondering if there is a better way to launch the default windows program for the associated file type. Any suggestions?
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jennifer Sohl:
I have recently run into some issues with this and was wondering if there is a better way to (...)


Could you elaborate? In general, "If it ain't broke, don't fix it!" is a good maxim.

- Anand
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... you got me. I haven't actually run into any problems..... yet. I am however anticipating one. I was going to try and run this code on a Mac, but didn't think it would work since I am issuing a windows command to launch the program. What do you think?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Java 6, then you could do this with the class java.awt.Desktop. The API documentation says:
The Desktop class allows a Java application to launch associated applications registered on the native desktop to handle a URI or a file.

Supported operations include:
  • launching the user-default browser to show a specified URI;
  • launching the user-default mail client with an optional mailto URI;
  • launching a registered application to open, edit or print a specified file.

  • One caveat: As far as I know, there is still no Java 6 available for the Mac...
     
    Anand Hariharan
    Rancher
    Posts: 272
    C++ Debian VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Jennifer Sohl:
    Okay... you got me.


    Just so you know, that wasn't my agenda (or is it agendum?).


    Originally posted by Jennifer Sohl:
    I haven't actually run into any problems..... yet. I am however anticipating one. I was going to try and run this code on a Mac, but didn't think it would work since I am issuing a windows command to launch the program. What do you think?


    Of course it isn't going to work. Anytime you use Runtime.exec, your code is no longer portable. Of course, you could have multiple if-else statements, and have different execution strings for different platforms, but as Dewhurst would say "This code is not platform-independent. It's multiplatform dependent."

    I think I now understand your question. You want to know a *general purpose* solution for "How to get your Java app to get its host platform to open a particular file with its associated application?". Am I in the ball-park?

    Am afraid, I don't know of a good answer. You could look at some of the Open Source Bit Torrent Java clients that do this already, and see what they do. If you learn something, would appreciate if you come back and share it with the rest of the forum.

    - Anand
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!