獲取JAVA進程:
成都創(chuàng)新互聯(lián)長期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為柳北企業(yè)提供專業(yè)的成都做網(wǎng)站、網(wǎng)站建設(shè),柳北網(wǎng)站改版等技術(shù)服務(wù)。擁有十載豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
package?com.test;
import?java.lang.management.ManagementFactory;
import?java.lang.management.RuntimeMXBean;
public?class?Target?{
public?static?void?main(String[]?args)?throws?InterruptedException?{
System.out.println(getProcessID());
while(true)?{
Thread.sleep(10000);
}
}
public?static?final?int?getProcessID()?{??
RuntimeMXBean?runtimeMXBean?=?ManagementFactory.getRuntimeMXBean();
System.out.println(runtimeMXBean.getName());
return?Integer.valueOf(runtimeMXBean.getName().split("@")[0])??
.intValue();??
}?
}
獲取所有正在運行著的Java進程
package?com.test;
import?java.util.HashSet;
import?java.util.Set;
import?sun.jvmstat.monitor.MonitoredHost;
import?sun.jvmstat.monitor.MonitoredVm;
import?sun.jvmstat.monitor.MonitoredVmUtil;
import?sun.jvmstat.monitor.VmIdentifier;
public?class?ProcessID?{
public?static?void?main(String[]?args)?throws?Exception?{
//?獲取監(jiān)控主機
MonitoredHost?local?=?MonitoredHost.getMonitoredHost("localhost");
//?取得所有在活動的虛擬機集合
Set??vmlist?=?new?HashSetObject(local.activeVms());
//?遍歷集合,輸出PID和進程名
for(Object?process?:?vmlist)?{
MonitoredVm?vm?=?local.getMonitoredVm(new?VmIdentifier("http://"?+?process));
//?獲取類名
String?processname?=?MonitoredVmUtil.mainClass(vm,?true);
System.out.println(process?+?"?------?"?+?processname);
}
}
}
可以直接根據(jù)類找到對應(yīng)Java進程ID的方法
package?com.test;
import?java.net.URISyntaxException;
import?java.util.HashSet;
import?java.util.Set;
import?sun.jvmstat.monitor.MonitorException;
import?sun.jvmstat.monitor.MonitoredHost;
import?sun.jvmstat.monitor.MonitoredVm;
import?sun.jvmstat.monitor.MonitoredVmUtil;
import?sun.jvmstat.monitor.VmIdentifier;
public?class?ProcessID?{
public?static?void?main(String[]?args)?throws?Exception?{
int?pid?=?getProcess(Target.class);
System.out.println("PID:?"+pid);
}
public?static?int?getProcess(Class??cls)?throws?MonitorException,?URISyntaxException?{
if(cls?==?null)?{
return?-1;
}
//?獲取監(jiān)控主機
MonitoredHost?local?=?MonitoredHost.getMonitoredHost("localhost");
//?取得所有在活動的虛擬機集合
Set??vmlist?=?new?HashSetObject(local.activeVms());
//?遍歷集合,輸出PID和進程名
for(Object?process?:?vmlist)?{
MonitoredVm?vm?=?local.getMonitoredVm(new?VmIdentifier("http://"?+?process));
//?獲取類名
String?processname?=?MonitoredVmUtil.mainClass(vm,?true);
if(cls.getName().equals(processname))?{
return?((Integer)process).intValue();
}
}
return?-1;
}
}
代碼:
package com.test;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
public class Target {
public static void main(String[] args) throws InterruptedException { System.out.println(getProcessID());
while(true) {
Thread.sleep(10000);
}
}
public static final int getProcessID() {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
System.out.println(runtimeMXBean.getName());
return Integer.valueOf(runtimeMXBean.getName().split("@")[0]) .intValue();
}
}
運行結(jié)果:2896@PC-20150603VRPL2896
當(dāng)前進程ID為2896。
ManagementFactory是一個在運行時管理和監(jiān)控Java VM的工廠類,它能提供很多管理VM的靜態(tài)接口,比如RuntimeMXBean;
RuntimeMXBean是Java虛擬機的運行時管理接口.
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class GetPid {
private static final String PNAME = "QQ";
public static void main(String[] args)
throws IOException {
System.out.println("包含關(guān)鍵字 "+PNAME+" 的進程有:");
System.out.println();
String path = System.getProperty("user.dir");
final File createFileName = new File(path + "\\mytempscript.vbe");
if(createFileName.exists()?createFileName.delete():createFileName.createNewFile()){}
final PrintWriter pw = new PrintWriter(new FileWriter(createFileName,
true), true);
pw.println("for each ps in getobject(\"winmgmts:\\\\.\\root\\cimv2:win32_process\").instances_");
pw.println("wscript.echo ps.handlevbtabps.name");
pw.println("next");
pw.close();
final InputStream ii = Runtime.getRuntime().exec(
"cscript " + path + "\\mytempscript.vbe").getInputStream();
final InputStreamReader ir = new InputStreamReader(ii);
final BufferedReader br = new BufferedReader(ir);
String str = null;
String[] ss = null;
while ((str = br.readLine()) != null) {
if (str.indexOf(PNAME)0str.endsWith(".exe")) {
ss = str.split("\\s");
for(int i=0;iss.length;i+=2){
System.out.println("****************************");
System.out.println("進程名:"+ss[i+1]+"\n進程ID:"+ss[i]);
}
}
}
System.out.println("****************************");
ir.close();
ii.close();
br.close();
}
}
運行結(jié)果如下
包含關(guān)鍵字 QQ 的進程有:
****************************
進程名:QQ.exe
進程ID:3956
****************************