java中Runtime.getRuntime().exec(String cmd)接口允许你执行一个windows命令,我想通过该命令,打来任意的windows文件,文件夹,或者程序。
直接写成Runtime.getRuntime.exec(test.txt); 运行时将报错,因为text.txt不可执行。
调查了一下,发现了这两个Windows命令挺好用:Start 和 call,
利用他们, 可以如下实现:
Runtime.getRuntime().exec("cmd /c start C:\\DocYouWant2Open.txt");
Runtime.getRuntime().exec("cmd /c cd C:\\a b & start 1.txt");//error
Runtime.getRuntime().exec("cmd /c call \"C:\\path contain space\\1.txt\"");
Runtime.getRuntime().exec("cmd /c start \"C:\\Program files\\Microsoft Office\\OFFICE11\\EXCEL.EXE\"");
Runtime.getRuntime().exec("cmd /c call explorer \"C:\\Program Files\\Microsoft Office\\OFFICE11\"");
Runtime.getRuntime().exec("cmd /c call \"C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE\"");
他们的区别在于,start命令路径参数中不支持空格,call通过引号括起来,路径参数可以支持空格。
顺路看了两篇关于bat的文章,windows的bat确实也挺强~ :)
http://www.yesky.com/20020228/218877.shtml
http://blog.youkuaiyun.com/able123521/archive/2007/12/15/1937918.aspx
==================================
为了日后搜索方便设置的关键字: 启动,起动