12

Is it possible to start other application that are installed on the system with my java app and pass a file as a parameter to them? I have a client which receives videos from a server and I want my client program to start, lets say the VLC player with the file that I received. How do I manage to do that?

asked Mar 16, 2010 at 22:29
3
  • 3
    Accept something first! (you'll get a badge for it and more answers) Commented Mar 16, 2010 at 22:34
  • 1
    You can find a history of your questions in your profile page (the page which you will see when you click anywhere where your name appears as a link): stackoverflow.com/users/283494 Vote the answers you found useful by clicking up arrow and accept the answers which actually helped in solving the problem by clicking the checkmark. Keep the spirit of StackOverflow alive :) Commented Mar 16, 2010 at 22:38
  • oh sorry guys. I'm new to stackoverflow and didn't know that I have to do this. Commented Mar 17, 2010 at 15:28

4 Answers 4

19

Use Desktop#open(). It will launch the platform default associated application to open the given file.

File file = new File("/absolute/path/to/file.vlc");
Desktop.getDesktop().open(file);

No need to hassle with Runtime#exec() or ProcessBuilder for which you would have to add platform detection and to write platform specific logics for.

answered Mar 16, 2010 at 22:35
Sign up to request clarification or add additional context in comments.

1 Comment

quick question is there a way to make the started program do stuff with java, for example import a file? Like "go to file -> select file -> import?
4

You can run an external program pretty easily on Java 5+ with ProcessBuilder, including passing arguments and handling input/output streams.

eg.

ProcessBuilder movieProcess = new ProcessBuilder("/path/to/movieplayer", "/path/to.moviefile");
movieProcess.start();

Only used it myself executing non-UI stuff, I'll give it a quick go and see what happens with something like VLC.


Update - works a treat for flv on Ubuntu, UI is visible and accepts file arguments.

answered Mar 16, 2010 at 22:33

1 Comment

Sweet, I didn't know that! :-)
3

Quite simply:

Runtime.getRuntime().exec("vlc [arguments]"); //Write all arguments as you would in your shell.

Make sure you catch all relevant exceptions

answered Mar 16, 2010 at 22:33

1 Comment

Then provide the full file path + extension of VLC.
3

You can call the exec method on the Runtime object.

Runtime.getRuntime().exec("System specific command line text here");
answered Mar 16, 2010 at 22:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.