Java命令来显示processus
您好,我想在java代码中将JBoss7显示为进程信息和数据库会话信息。。。 我尝试以下代码:Java命令来显示processus,java,command,jmx,Java,Command,Jmx,您好,我想在java代码中将JBoss7显示为进程信息和数据库会话信息。。。 我尝试以下代码: Process p = Runtime.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe"); Process p = Runtime.getRuntime().exec ("C:\\Users\\user\\Download
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
Process p = Runtime.getRuntime().exec
("C:\\Users\\user\\Downloads\\PSTools\\pslist.exe -s 2");
该代码:
Process p = Runtime.getRuntime().exec
(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
Process p = Runtime.getRuntime().exec
("C:\\Users\\user\\Downloads\\PSTools\\pslist.exe -s 2");
它运行良好,但只在windows操作系统上运行。
所以我想要一个java代码,它可以在每个操作系统上工作,而不仅仅是在windows或linux上。。。
你能帮我吗?
谢谢大家。在同一台主机上查找java进程非常简单,后面列出了一些注意事项。连接到他们的JMX接口也是可以实现的。就跟踪数据库会话而言,您可以从概念上获取所需的任何数据,只要它是通过可访问的MBean发布的 该过程需要使用。下面是一个简单的示例,我将列出主机上运行的所有JVM,尝试连接到它们并列出它们的堆使用情况 首先,以下是导入内容:
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
import java.util.List;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
com.sun.tools.attach类位于JVM的tools.jar中。代码如下:
public class AttachAPIExample {
/**
* Uses the attach API to locate all JVMs accessible on this machine.
* @param args None
*/
public static void main(String[] args) {
// Get my PID
final String MYPID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
log("Scanning for JVMs...");
// List all the Virtual Machine Descriptors
List<VirtualMachineDescriptor> descriptors = VirtualMachine.list();
for(VirtualMachineDescriptor vmd: descriptors) {
VirtualMachine vm = null;
// Do this in a catch block in case we run into a JVM that is not the same "bit" as we are
try {
vm = vmd.provider().attachVirtualMachine(vmd.id());
String display = vmd.displayName().trim().isEmpty() ? "Unknown" : vmd.displayName();
log("JVM%sPID: %s Display: %s", vmd.id().equals(MYPID) ? " (Me) " : " ", vmd.id(), display);
String connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
if(connectorAddress!=null) {
log("\tConnector Found Installed at [%s]", connectorAddress);
} else {
String javaHome = vm.getSystemProperties().getProperty("java.home");
File agentJarFile = new File(javaHome + File.separator + "lib" + File.separator + "management-agent.jar");
if(agentJarFile.exists()) {
log("I think we can find this JVM's management agent here: [%s]", agentJarFile.toString());
vm.loadAgent(agentJarFile.getAbsolutePath());
connectorAddress = vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress", null);
log("\tConnector Installed at [%s]", connectorAddress);
} else {
log("Cannot find the agent jar for JVM [%s] at [%s]", vmd.id(), javaHome);
}
}
// Now lets try and connect and read some MBean values
if(connectorAddress!=null) {
log("Attaching to JVM [%s]...", vmd.id());
JMXServiceURL jmxUrl = new JMXServiceURL(connectorAddress);
JMXConnector connector = null;
try {
connector = JMXConnectorFactory.connect(jmxUrl);
MBeanServerConnection conn = connector.getMBeanServerConnection();
MemoryUsage heap = MemoryUsage.from((CompositeData)conn.getAttribute(new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), "HeapMemoryUsage"));
log("Heap Usage: %s", heap);
} finally {
if(connector!=null) {
try { connector.close(); } catch (Exception ex) {/* No Op */}
}
}
}
} catch (Exception ex) {
/* No Op */
} finally {
if(vm!=null) try { vm.detach(); } catch (Exception ex) {/* No Op */}
log("======================================");
}
}
}
public static void log(String fmt, Object...args) {
System.out.println(String.format(fmt, args));
}
}
警告
JMV只能连接到类似位的JVM,即32位到32位、64位到64位
启动附加的操作系统用户必须具有访问其他已处理文件的操作系统授权。所以同一个用户,没问题。其他用户。。。你应该是根。
当在不同的JVM供应商实现中使用attach api时,会产生不同的结果。如果是Sun/Oracle/OpenJDK,你可能会很好。如果是这些,并且您正试图连接到IBM JVM,或者反之亦然,那么我不知道会发生什么,尽管JRockit在这方面似乎相当友好。
我不知道所有的警告。
.exe在linux中不起作用。我知道,但我想知道一种方法或一个命令,允许我了解这些信息并在所有操作系统上工作。您是否尝试使用linux命令promptno,但项目经理告诉我,我们希望使用java命令而不是osRuntime命令。getRuntime.exec仅支持您需要查找的Windows操作系统在linux中使用此命令的替代方法,或者尝试在参数中添加linux路径,它可能会起作用。非常感谢。这段代码正是我所需要的。但是为什么它给我描述符列表大小等于0?有一些可能性。一个是您可能没有任何其他java进程在同一台主机上运行。另一个原因是,您可能正在使用32位JVM运行扫描仪,但其他JVM是64位的,反之亦然。最好的方法是使用[jps][1]进行测试。[1]: