使用JAVA代码运行程序

来源:百度知道 编辑:UC知道 时间:2024/06/10 15:28:02
正如题目所说的 使用JAVA代码实现运行程序~比如说 使用JAVA代码运行 浏览器,用JAVA代码 打开QQ~

1 用Java调用windows系统的exe文件,比如notepad,calc之类:
public class Demo{
public static void main(String args[]){
Runtime rn=Runtime.getRuntime();
Process p=null;

try{
p=rn.exec("notepad");
}catch(Exception e){
System.out.println("Error exec notepad");
}
}
}

2 调用其他的可执行文件,例如:自己制作的exe,或是下载安装的软件
public class Demo{
public static void main(String args[]){
Runtime rn=Runtime.getRuntime();
Process p=null;

try{
p=rn.exec("D:\\Program Files\\Tencent\\QQ\\QQ.exe");
}catch(Exception e){
System.out.println("Error exec AnyQ");
}
}
}

其实,1和2的区别在于你的程序是否在windows的环境变量里。如果你把QQ加入环境变量,也可以直接调用

Process pro = Runtime.getRuntime().exec("C:\\Program Files\\Thunder Network\\Thunder\\Thunder.exe");
我运行的程序是迅雷,你指定要运行程序的执行文件路径就可以了。