文章目录
本次有用
执行命令,打卡cmd框,运行语句或者bat文件,目录结构如下
public class auto {
public static void main(String[] args) {
try {
ProcessBuilder processBuilder=new ProcessBuilder();
//场景一 只能执行一条命令
processBuilder.command("cmd.exe","/k","start","cd","./out");
//场景二 可以把多条命令放到bat里面,然后执行bat文件。这里需要注意的是bat文件中如果不是同目录执行,还需要增加切换符操作如e: d: f:
processBuilder.command("cmd.exe","/k","start","D:\\CDesktop\\example\\untitled3\\src\\tt.bat");
//场景三 打开文件夹
processBuilder.command("cmd.exe","/k","start","C:\\Intel");
//或者
Runtime.getRuntime().exec("explorer /select, "+nowChoosePath+"\\"+directoryName+"-scanResult.txt");
//场景四 打开某一个文件
processBuilder.command("cmd.exe","/k","start","C:\\Users\\xxx\\Desktop\\问题描述.docx");
processBuilder.start();
//场景五 压缩文件
String cmd2 = "C:\\Program Files\\WinRAR\\WinRAR.exe x -r -o+ -ibck -y "
+ zipFile + " *.* " + tmp;
Runtime rt = Runtime.getRuntime();
Process pre = rt.exec(cmd2);
//或者
String[] cmd = new String[5];
cmd[0] = "cmd";
cmd[1] = "/c";
cmd[2] = "start";
cmd[3] = " ";
cmd[4] = outputUrl;
Runtime.getRuntime().exec(cmd);
//场景六 打开浏览器
Desktop d = Desktop.getDesktop();
URI address = null;
try {
address = new URI("http://xxx/index.php/Question/detail?id="+hunNumber);
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
d.browse(address);
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
java关闭CMD窗口
Runtime.getRuntime().exec("Taskkill /im cmd.exe ");
参考
https://blog.csdn.net/weixin_43916074/article/details/123118256
http://www.manongjc.com/detail/27-hstjvrzclujrvym.html
http://www.manongjc.com/detail/27-hstjvrzclujrvym.html
https://blog.csdn.net/weixin_36268390/article/details/114202932
https://blog.csdn.net/weixin_43916074/article/details/123118256
http://t.zoukankan.com/jeryM-p-8459709.html
https://www.cnblogs.com/zhufu9426/p/7928570.html
https://blog.csdn.net/hongsaoru/article/details/121288619
https://blog.csdn.net/weixin_36431535/article/details/114566073
shell 命令
https://blog.51cto.com/u_15431495/5642758
/*cmd /c dir 是执行完dir命令后关闭命令窗口
cmd /k dir 是执行完dir命令后不关闭命令窗口
cmd /c start dir 会打开一个新窗口后执行dir命令,原窗口会关闭
cmd /k start dir 会打开一个新窗口后执行dir命令,原窗口不会关闭