Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法
更新時(shí)間:2016年03月12日 08:57:58 作者:qiaolevip
這篇文章主要介紹了Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法,主要用到了IOException類,需要的朋友可以參考下
Java調(diào)用cmd命令,并輸出顯示信息:
package com.anxin.cmd.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Command {
public static void main(String[] args) {
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir"); // cmd /c calc
// Process pr = rt.exec("D:\\xunlei\\project.aspx");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
Java啟動(dòng)本機(jī)應(yīng)用程序EXE的三種方式:
第一種方式:利用cmd方式
/**
* 執(zhí)行cmd命令
*
* @param command
* @throws IOException
*/
public static String executeCmd(String command) throws IOException {
log.info("Execute command : " + command);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("cmd /c " + command);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));
String line = null;
StringBuilder build = new StringBuilder();
while ((line = br.readLine()) != null) {
log.info(line);
build.append(line);
}
return build.toString();
}
executeCmd(start "AXAdWebBrowser" "D:\AXAdsBrowser\AXAdWebBrowser.exe");
第二種方式:利用ProcessBuilder調(diào)用cmd方式
/**
* 啟動(dòng)應(yīng)用程序
*
* @param programName
* @return
* @throws IOException
*/
public static void startProgram(String programPath) throws IOException {
log.info("啟動(dòng)應(yīng)用程序:" + programPath);
if (StringUtils.isNotBlank(programPath)) {
try {
String programName = programPath.substring(programPath.lastIndexOf("/") + 1, programPath.lastIndexOf("."));
List<String> list = new ArrayList<String>();
list.add("cmd.exe");
list.add("/c");
list.add("start");
list.add("\"" + programName + "\"");
list.add("\"" + programPath + "\"");
ProcessBuilder pBuilder = new ProcessBuilder(list);
pBuilder.start();
} catch (Exception e) {
e.printStackTrace();
log.error("應(yīng)用程序:" + programPath + "不存在!");
}
}
}
第三種方式:使用Desktop啟動(dòng)應(yīng)用程序
/**
* 啟動(dòng)應(yīng)用程序
*
* @param programName
* @return
* @throws IOException
*/
public static void startProgram(String programPath) throws IOException {
log.info("啟動(dòng)應(yīng)用程序:" + programPath);
if (StringUtils.isNotBlank(programPath)) {
try {
Desktop.getDesktop().open(new File(programPath));
} catch (Exception e) {
e.printStackTrace();
log.error("應(yīng)用程序:" + programPath + "不存在!");
}
}
}
相關(guān)文章
SpringBoot?快速實(shí)現(xiàn)?api?接口加解密功能
在項(xiàng)目中,為了保證數(shù)據(jù)的安全,我們常常會(huì)對(duì)傳遞的數(shù)據(jù)進(jìn)行加密,Spring?Boot接口加密,可以對(duì)返回值、參數(shù)值通過(guò)注解的方式自動(dòng)加解密,這篇文章主要介紹了SpringBoot?快速實(shí)現(xiàn)?api?接口加解密功能,感興趣的朋友一起看看吧2023-10-10
java 字符串相減(很簡(jiǎn)單的一個(gè)方法)
本篇文章是對(duì)java中關(guān)于字符串相減的一個(gè)簡(jiǎn)單的方法進(jìn)行了介紹,需要的朋友參考下2013-07-07
java多線程編程之使用runnable接口創(chuàng)建線程
實(shí)現(xiàn)Runnable接口的類必須使用Thread類的實(shí)例才能創(chuàng)建線程,通過(guò)Runnable接口創(chuàng)建線程分為以下兩步2014-01-01
spring中@Autowired自動(dòng)注入依賴項(xiàng)的使用
當(dāng)使用@Autowired注解時(shí),它可以自動(dòng)注入依賴項(xiàng),例如其他類的實(shí)例,本文就來(lái)詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
java數(shù)據(jù)庫(kù)連接池和數(shù)據(jù)庫(kù)連接示例
這篇文章主要介紹了java數(shù)據(jù)庫(kù)連接池和數(shù)據(jù)庫(kù)連接示例,需要的朋友可以參考下2014-05-05

