如何使一个java程序,一执行,就打开本地某目录下的html文件?比如abc.html, 就像双击打开那样!

如题所述

如何使一个java程序,一执行,就打开本地某目录下的html文件?比如abc.html, 就像双击打开那样! 15

public static void main(String args[]) throws Exception
{
File file = new File("abc.html");
Runtime ce=Runtime.getRuntime();
System.out.println(file.getAbsolutePath());
ce.exec(file.getAbsolutePath());
}

像这样,想打开同目录下的abc.html,怎么不行呢?



恩,你的命令不正确,怎么会打开呢?
试试我给你的代码吧
import java.io.File;
public class TestRuntime {

/**
* @param args
*/
public static void main(String args[]) throws Exception
{
File file = new File("abc.html");
Runtime ce=Runtime.getRuntime();
System.out.println(file.getAbsolutePath());
ce.exec("cmd   /c   start  "+file.getAbsolutePath());
}
}

只要在你的同目录下有abc.html,就可以打开了

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-06-15
你用这个打开试试,意思就是打开cmd执行个打开文件命令
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "D:/my local/测试用例.xls"});
第2个回答  2017-06-15
用cmd 命令打开就可以了
第3个回答  2017-06-15
恩,你的命令不正确,怎么会打开呢? 试试我给你的代码吧 import java.io.File; public class TestRuntime { /** * @param args */ public static void main(String args[]) throws Exception { File file = new File("abc.html"); Runtime ce=R.