- 承接《Runtime详解一》进行编写
方法详解
exec(String command, String[] envp)
- public Process exec(String command, String[] envp) throws IOException
- 在指定环境的单独进程中执行指定的字符串命令。
- 对于 exec(command, envp) 形式的调用而言,其行为与调用 exec(command, envp, null) 完全相同。
- 参数:command - 一条指定的系统命令,其中可以设置变量,包括windows中的环境变量;
- 参数:envp - 字符串数组,其中每个元素的环境变量的设置格式为 name=value,name值为command中的变量名称;如果子进程应该继承当前进程的环境,或该参数为 null。
- 返回:一个新的 Process 对象,用于管理子进程
- 通俗的将就是在exec(String command)的基础上,加了可以为指令(command)动态设置变量值了
public static void main(String[] args) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c dir %targetDir%", new String[]{"targetDir=E:\\gxg"}); InputStream inputStream = process.getInputStream(); InputStreamReader streamReader = new InputStreamReader(inputStream,"gbk"); BufferedReader bufferedReader = new BufferedReader(streamReader); String readLine ; while ((readLine = bufferedReader.readLine())!=null){ System.out.println(readLine); } } catch (IOException e) { e.printStackTrace(); } }
- runtime.exec("cmd /c dir %targetDir%", new String[]{"targetDir=E:\\gxg"})
- 没有加"start"参数,所以并不会弹出cmd框
- %tartgetDir%类似windows环境变量的写法,变量必须用"%%"扣起来,后面字符串数组中用"key=value"的形式,key名称必须与前面的变量名称相同
- new InputStreamReader(inputStream,"gbk"):将字节流转字符流并采用"gbk"编码,否则中文乱码,接着使用缓冲流读取数据
- 输出结果类似如下:
驱动器 E 中的卷是 document
卷的序列号是 0001-7658E:\gxg 的目录
2018/04/21 周六 16:24 <DIR> .
2018/04/21 周六 16:24 <DIR> ..
2018/04/23 周一 11:12 <DIR> resources
2018/03/27 周二 18:50 <DIR> wmx
2018/04/21 周六 16:24 <DIR> wmx1
0 个文件 0 字节
5 个目录 1,247,900,999,680 可用字节
Process finished with exit code 0
public static void main(String[] args) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c dir %JAVA_HOME%",null); InputStream inputStream = process.getInputStream(); InputStreamReader streamReader = new InputStreamReader(inputStream,"gbk"); BufferedReader bufferedReader = new BufferedReader(streamReader); String readLine ; while ((readLine = bufferedReader.readLine())!=null){ System.out.println(readLine); } } catch (IOException e) { e.printStackTrace(); } }
- runtime.exec("cmd /c dir %JAVA_HOME%",null)
- 可以直接获取windows系统的环境变量值
- 输出结果如下:
卷的序列号是 000C-81F5
D:\Java\jdk1.8.0_101 的目录
2017/07/29 周六 10:42 <DIR> .
2017/07/29 周六 10:42 <DIR> ..
2017/07/29 周六 10:40 <DIR> bin
2016/06/22 周三 01:42 3,244 COPYRIGHT
2017/07/29 周六 10:40 <DIR> db
2017/07/29 周六 10:40 <DIR> include
2017/07/29 周六 10:40 5,090,294 javafx-src.zip
2017/07/29 周六 10:40 <DIR> jre
2017/07/29 周六 10:40 <DIR> lib
2017/07/29 周六 10:40 40 LICENSE
2017/07/29 周六 10:40 159 README.html
2017/07/29 周六 10:40 528 release
2016/06/22 周三 01:42 21,251,669 src.zip
2017/07/29 周六 10:40 110,114 THIRDPARTYLICENSEREADME-JAVAFX.txt
2017/07/29 周六 10:40 177,094 THIRDPARTYLICENSEREADME.txt
8 个文件 26,633,142 字节
7 个目录 296,898,805,760 可用字节
Process finished with exit code 0
exec(String command, String[] envp, File dir)
public Process exec(String command, String[] envp, File dir) throws IOException
- 参数:command - 一条指定的系统命令,可以带"%变量%"
- 参数:envp - 字符串数组,其中每个元素的环境变量的设置格式为 name=value;如果子进程应该继承当前进程的环境,或该参数为 null。可为前面的command中的变量赋值
- 参数:dir - 子进程的工作目录;如果子进程应该继承当前进程的工作目录,则该参数为 null。
- 返回:一个新的 Process 对象,用于管理子进程
- 通俗的讲就是在exec(String command,String[] envp)的基础上,加上了可以在指定目录执行子进程了
- 如下是没指定dir时,会以当前进程的工作目录执行
public static void main(String[] args) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c dir",null,null); InputStream inputStream = process.getInputStream(); InputStreamReader streamReader = new InputStreamReader(inputStream,"gbk"); BufferedReader bufferedReader = new BufferedReader(streamReader); String readLine ; while ((readLine = bufferedReader.readLine())!=null){ System.out.println(readLine); } } catch (IOException e) { e.printStackTrace(); } }
- runtime.exec("cmd /c dir",null,null)
- envp为null,因为command不需要传变量值,所以为null
- dir为null,表明子进程直接在当前进程的工作目录下运行
- 输出结果如下, E:\IdeaProjects\BaoAnCCC目录就是IDEA工作空间下当前项目目录,BaoAnCCC就是项目名:
卷的序列号是 0001-7658
E:\IdeaProjects\BaoAnCCC 的目录
2018/05/11 周五 17:06 <DIR> .
2018/05/11 周五 17:06 <DIR> ..
2018/05/11 周五 17:06 <DIR> %SystemDrive%
2018/03/27 周二 19:01 <DIR> .idea
2018/03/27 周二 14:23 564 BaoAnCCC.iml
2018/04/23 周一 11:07 <DIR> classes
2018/03/27 周二 17:18 <DIR> lib
2018/04/21 周六 16:24 <DIR> resources
2018/03/28 周三 08:49 <DIR> src
1 个文件 564 字节
8 个目录 1,247,900,999,680 可用字节
Process finished with exit code 0
- 当将工作目录dir改成"E:\\gxg"时,效果如下
public static void main(String[] args) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c dir",null,new File("E:\\gxg")); InputStream inputStream = process.getInputStream(); InputStreamReader streamReader = new InputStreamReader(inputStream,"gbk"); BufferedReader bufferedReader = new BufferedReader(streamReader); String readLine ; while ((readLine = bufferedReader.readLine())!=null){ System.out.println(readLine); } } catch (IOException e) { e.printStackTrace(); } }
- 当dir为“E:\\gxg”目录时,输出如下:
驱动器 E 中的卷是 document
卷的序列号是 0001-7658
E:\gxg 的目录
2018/04/21 周六 16:24 <DIR> .
2018/04/21 周六 16:24 <DIR> ..
2018/04/23 周一 11:12 <DIR> resources
2018/03/27 周二 18:50 <DIR> wmx
2018/04/21 周六 16:24 <DIR> wmx1
0 个文件 0 字节
5 个目录 1,247,900,999,680 可用字节
Process finished with exit code 0
- 当使用命令行程序"WolCmd"做主机的网络唤醒时,可以用如下的写法
public static void main(String[] args) { try { Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("cmd /c WolCmd.exe 1CB72CEFBA3D 192.168.1.20 255.255.255.0 100", null,new File("D:\\program")); InputStream inputStream = process.getInputStream(); InputStreamReader streamReader = new InputStreamReader(inputStream,"gbk"); BufferedReader bufferedReader = new BufferedReader(streamReader); String readLine ; while ((readLine = bufferedReader.readLine())!=null){ System.out.println(readLine); } } catch (IOException e) { e.printStackTrace(); } }
程序结果输出如下:
Wake On Lan signal sent to Mac Address 1CB72CEFBA3D
via Broadcast Address 192.168.1.255 on port 100
Process finished with exit code 0
扫描二维码关注公众号,回复:
1477708 查看本文章