[英]Let user choose the application to open the file
I need my java code to open a file based upon the default application. 我需要我的Java代码才能根据默认应用程序打开文件。 Thanks to How to open user system preferred editor for given file?
感谢如何打开给定文件的用户系统首选编辑器? which suggests a quality method to do it by
这表明通过
Runtime.getRuntime().exec("RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL "+file);
But the issue is that, once I select the application to open it up, it doesn't open the file. 但是问题是,一旦我选择了要打开的应用程序,它就不会打开文件。 I do not know the reason for it.
我不知道原因。
Thanks 谢谢
EDIT: 编辑:
Desktop.getDesktop().open(file);
This opens in default application. 这将在默认应用程序中打开。 I want the user to choose the application to open it up
我希望用户选择要打开的应用程序
Use : 采用 :
Desktop.getDesktop().open(file);
http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File ) http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File )
EDIT: 编辑:
Here is the code to make your command work: 这是使您的命令起作用的代码:
import java.io.File;
import java.io.IOException;
public class TestExec {
public static void main(String[] args) throws IOException, InterruptedException {
File file = new File("d:/Clipboard1.png");
ProcessBuilder builder = new ProcessBuilder("RUNDLL32.EXE", "SHELL32.DLL,OpenAs_RunDLL", file.getAbsolutePath());
builder.redirectErrorStream();
builder.redirectOutput();
Process process = builder.start();
process.waitFor();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.