龙空技术网

「编程」Java调用可执行程序的方法

自由杂货铺045 89

前言:

今天大家对“java 调用”大致比较重视,大家都想要知道一些“java 调用”的相关文章。那么小编同时在网上汇集了一些有关“java 调用””的相关知识,希望同学们能喜欢,同学们快快来了解一下吧!

public void runProcessDemo1 {    try {        Process process = Runtime.getRuntime().exec("ipconfig /all");        InputStream is = process.getInputStream();        InputStreamReader isr =new InputStreamReader(is, "gbk");        BufferedReader br = new BufferedReader(isr);        String line;    		while ((line = br.readLine()) != null) {      			System.out.println(line);    		}    		int exitCode = process.waitFor();    		System.out.println(exitCode);  	} catch (IOException | InterruptedException e) {    		e.printStackTrace();  	}}
public void runProcessDemo2 {    ProcessBuilder processBuilder = new ProcessBuilder();    processBuilder.command("ipconfig","/all");    //将标准输入流和错误输入流合并,通过标准输入流读取信息    processBuilder.redirectErrorStream(true);    try {        //启动进程        Process start = processBuilder.start();        //获取输入流        InputStream inputStream = start.getInputStream();        //转成字符输入流        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "gbk");        int len = -1;        char[] c = new char[1024];        StringBuffer outputString = new StringBuffer();        //读取进程输入流中的内容        while ((len = inputStreamReader.read(c)) != -1) {            String s = new String(c, 0, len);            outputString.append(s);            System.out.print(s);        }        inputStream.close();    } catch (IOException e) {        e.printStackTrace();    }}
public void runProcessDemo3 throws IOException {    ProcessBuilder processBuilder = new ProcessBuilder("cmd","/c","dir");    processBuilder.directory(new File("D:/"));    Process process = processBuilder.start();    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "gbk"));    String line;    while ((line = br.readLine()) != null) {        System.out.println(line);    }}

以上均是windows平台,如果是Linux,请使用相应命令。

标签: #java 调用 #java怎么调用