第1个回答 推荐于2017-09-28
使用Runtime.getRuntime().exec()方法可以在java程序里运行外部程序。
1. exec(String command)
2. exec(String command, String envp[], File dir)
3. exec(String cmd, String envp[])
4. exec(String cmdarray[])
5. exec(String cmdarray[], String envp[])
6. exec(String cmdarray[], String envp[], File dir)
一般的应用程序可以直接使用第一版本,当有环境变量传递的时候使用后面的版本。其中2和6版本可以传递一个目录,标识当前目录,因为有些程序是使用相对目录的,所以就要使用这个版本。
实例:
Process process = Runtime.getRuntime().exec(".\\p.exe");
process.waitfor();
在上面的程序中,第一行的“.\\p.exe”是要执行的程序名,Runtime.getRuntime()返回当前应用程序的Runtime对象,该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。通过Process可以控制该子进程的执行或获取该子进程的信息。第二条语句的目的等待子进程完成再往下执行。
第2个回答 推荐于2017-10-04
用word主程序打开这个word文档就OK了。。。
命令如下:
"C:\office11\winword.exe a.doc"
________
import java.lang.*;
import java.io.*;
public class process {
public static void main(String args[]){
try
{
String cmd= "C:\office11\winword.exe a.doc";
Runtime.getRuntime().exec(cmd);
}
catch(IOException e)
{
System.out.println("err!");
}
}
}本回答被提问者采纳