获取JAVA进程:
成都创新互联长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为柳北企业提供专业的成都做网站、网站建设,柳北网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
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 {
// 获取监控主机
MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
// 取得所有在活动的虚拟机集合
Set? vmlist = new HashSetObject(local.activeVms());
// 遍历集合,输出PID和进程名
for(Object process : vmlist) {
MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + process));
// 获取类名
String processname = MonitoredVmUtil.mainClass(vm, true);
System.out.println(process + " ------ " + processname);
}
}
}
可以直接根据类找到对应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;
}
// 获取监控主机
MonitoredHost local = MonitoredHost.getMonitoredHost("localhost");
// 取得所有在活动的虚拟机集合
Set? vmlist = new HashSetObject(local.activeVms());
// 遍历集合,输出PID和进程名
for(Object process : vmlist) {
MonitoredVm vm = local.getMonitoredVm(new VmIdentifier("//" + 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();
}
}
运行结果:2896@PC-20150603VRPL2896
当前进程ID为2896。
ManagementFactory是一个在运行时管理和监控Java VM的工厂类,它能提供很多管理VM的静态接口,比如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("包含关键字 "+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();
}
}
运行结果如下
包含关键字 QQ 的进程有:
****************************
进程名:QQ.exe
进程ID:3956
****************************