2014年9月13日 星期六

JAVA Process簡單應用


*java如果要執行exe或bat可執行檔,可以利用 Runtime.getRuntime().exec("xxx.exe")來執行,如下就可以啟動jmeter。

Process process = Runtime.getRuntime().exec("D:/JMeter/apache-jmeter-2.9/bin/jmeter.bat");

*可利用下面語法看到目前作業系統
System.out.println(System.getProperty("os.name"));


練習使用JAVA 在目前正在執行 process 裡找到特定的process,並找到pid kill掉。


範例:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExcuteTest {

public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("tasklist");
int taskId = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(
process.getInputStream(), "MS950"));

String line = null;
while ((line = br.readLine()) != null) {
if (line.indexOf("Skype.exe") != -1) {
line = line.substring( "Skype.exe".length() );
System.out.println(line.trim());
       taskId = Integer.parseInt( (line.trim().split( " " ))[0] );

       System.out.println( "Task Id: " + taskId );
}
}
Runtime rt = Runtime.getRuntime();
if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) {
System.out.println("taskkill......." + taskId);
rt.exec("tskill " + taskId);
} else {
System.out.println("kill -9 ......." + taskId);
rt.exec("kill -9 " + taskId);
}
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}

1 則留言:

  1. Casinos Near Murphy, CT - Mapyro
    Find casinos near Murphy, CT in 목포 출장안마 real time, see activity, 속초 출장안마 read reviews, see photos and get 부천 출장샵 directions. Search by elevation, from 30 충청남도 출장샵 miles 김해 출장마사지 to 1.

    回覆刪除