Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting other Applications with Java

Tags:

java

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?

like image 543
user283494 Avatar asked Mar 16 '10 22:03

user283494


People also ask

What famous applications use Java?

Mobile applications that are created using Java include some popular ones like Netflix, Twitter, Spotify, and many more. The reason why Java is used to build mobile apps are: Helps to write simple code.


2 Answers

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.

like image 106
BalusC Avatar answered Oct 01 '22 05:10

BalusC


Quite simply:

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

Make sure you catch all relevant exceptions

like image 24
Buhake Sindi Avatar answered Oct 01 '22 05:10

Buhake Sindi