java代码——返回当前计算机所有进程ID怎么做? 5
2个回答
展开全部
获取JAVA进程:
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 HashSet<Object>(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 HashSet<Object>(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;
}
}
展开全部
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*;public class CaculatorA { private JFrame jf; private JButton[] jbs; private JTextField jtf; private JButton clear; private double num1,num2,jieguo; private char c; /** * 构造方法实例化属性 * */ public CaculatorA(){ jf=new JFrame("我的计算器v1.0"); jtf=new JTextField(20); clear=new JButton("clear"); jbs=new JButton[16]; String str="123+456-789*0./="; for(int i=0; i<str.length(); i++){ jbs[i]=new JButton(str.charAt(i)+""); } init(); addEventHandler(); // setFont(); // setColor(); showMe(); } /**
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询