使用 Java 启动其他应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2458622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting other Applications with Java
提问by user283494
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?
是否可以使用我的 java 应用程序启动系统上安装的其他应用程序并将文件作为参数传递给它们?我有一个从服务器接收视频的客户端,我希望我的客户端程序启动,比如说 VLC 播放器和我收到的文件。我该如何做到这一点?
回答by BalusC
Use Desktop#open(). It will launch the platform default associated application to open the given file.
使用Desktop#open(). 它将启动平台默认关联应用程序以打开给定文件。
File file = new File("/absolute/path/to/file.vlc");
Desktop.getDesktop().open(file);
No need to hassle with Runtime#exec()or ProcessBuilderfor which you would have to add platform detection and to write platform specific logics for.
无需麻烦Runtime#exec()或ProcessBuilder为此您必须添加平台检测并为其编写平台特定的逻辑。
回答by brabster
You can run an external program pretty easily on Java 5+ with ProcessBuilder, including passing arguments and handling input/output streams.
您可以使用ProcessBuilder在 Java 5+ 上非常轻松地运行外部程序,包括传递参数和处理输入/输出流。
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.
我自己只用它来执行非 UI 的东西,我会快速尝试一下,看看像 VLC 这样的东西会发生什么。
Update - works a treat for flv on Ubuntu, UI is visible and accepts file arguments.
更新 - 在 Ubuntu 上处理 flv,UI 可见并接受文件参数。
回答by Buhake Sindi
Quite simply:
很简单:
Runtime.getRuntime().exec("vlc [arguments]"); //Write all arguments as you would in your shell.
Make sure you catch all relevant exceptions
确保捕获所有相关异常
回答by csj
You can call the exec method on the Runtime object.
您可以在 Runtime 对象上调用 exec 方法。
Runtime.getRuntime().exec("System specific command line text here");

