如何在 java 中使用 exec 方法打开文件?
how to open a file using exec method in java?
C:\Users\Admin\Downloads\VID_20160226_203631957.mp4
当我在命令提示符下执行以上行时,相应的视频将使用默认媒体播放器播放。
但是当我尝试使用 java 运行时 class 做同样的事情时,它不起作用。
我正在使用以下方法。
Runtime r= Runtime.getRuntime();
r.exec("C:\Users\Admin\Downloads\VID_20160226_203631957.mp4")
使用 Desktop.open(File)
启动关联的应用程序来打开文件。 类似于,
File f = new File("C:/Users/Admin/Downloads/VID_20160226_203631957.mp4");
try {
Desktop.getDesktop().open(f);
} catch (IOException e) {
e.printStackTrace();
}
您可能更愿意构建相对于 user's home directory 的路径;像
File downloads = new File(System.getProperty("user.home"), "Downloads");
File f = new File(downloads, "VID_20160226_203631957.mp4");
试试这个。
Runtime r= Runtime.getRuntime();
r.exec("cmd /c C:\Users\Admin\Downloads\VID_20160226_203631957.mp4");
C:\Users\Admin\Downloads\VID_20160226_203631957.mp4
当我在命令提示符下执行以上行时,相应的视频将使用默认媒体播放器播放。
但是当我尝试使用 java 运行时 class 做同样的事情时,它不起作用。 我正在使用以下方法。
Runtime r= Runtime.getRuntime();
r.exec("C:\Users\Admin\Downloads\VID_20160226_203631957.mp4")
使用 Desktop.open(File)
启动关联的应用程序来打开文件。 类似于,
File f = new File("C:/Users/Admin/Downloads/VID_20160226_203631957.mp4");
try {
Desktop.getDesktop().open(f);
} catch (IOException e) {
e.printStackTrace();
}
您可能更愿意构建相对于 user's home directory 的路径;像
File downloads = new File(System.getProperty("user.home"), "Downloads");
File f = new File(downloads, "VID_20160226_203631957.mp4");
试试这个。
Runtime r= Runtime.getRuntime();
r.exec("cmd /c C:\Users\Admin\Downloads\VID_20160226_203631957.mp4");