Java command to show the processus -


hi want show jboss 7 process information , database sessions information in java code... try code:

process p = runtime.getruntime().exec                     (system.getenv("windir") +"\\system32\\"+"tasklist.exe"); 

and code:

process p = runtime.getruntime().exec                     ("c:\\users\\user\\downloads\\pstools\\pslist.exe -s 2"); 

and works works on windows operating system . want java code work on each operating system, , not on windows or linux ... can u me please? everybody.

finding java processes on same host straight forward (some caveats listed later). connecting jmx interfaces achievable. far tracking database sessions, can conceptually acquire data want, long published through accessible mbean.

the process requires using java attach api. here's simple example list jvm's running on host, attempt attach them , list heap usage.

first off, here's imports:

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; 

the com.sun.tools.attach classes in jvm's tools.jar. here's code:

public class attachapiexample {      /**      * uses attach api locate jvms accessible on machine.      * @param args none      */     public static void main(string[] args) {         // pid         final string mypid = managementfactory.getruntimemxbean().getname().split("@")[0];         log("scanning jvms...");         // list virtual machine descriptors         list<virtualmachinedescriptor> descriptors = virtualmachine.list();          for(virtualmachinedescriptor vmd: descriptors) {             virtualmachine vm = null;             // in catch block in case run jvm not same "bit"             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 @ [%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 can find 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 @ [%s]", connectoraddress);                     } else {                         log("cannot find agent jar jvm [%s] @ [%s]", vmd.id(), javahome);                     }                 }                 // lets try , connect , read mbean values                 if(connectoraddress!=null) {                     log("attaching 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);                     } {                         if(connector!=null) {                             try { connector.close(); } catch (exception ex) {/* no op */}                         }                     }                 }             } catch (exception ex) {                 /* no op */             } {                 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));     }  } 

and here's sample output:

scanning jvms... jvm pid: 27928 display: sun.tools.jconsole.jconsole     connector found installed @ [service:jmx:rmi://127.0.0.1/stub/ro0abxnyac5qyxzhec5tyw5hz2vtzw50lnjlbw90zs5ybwkuuk1ju2vydmvysw1wbf9tdhviaaaaaaaaaaicaab4cgaaamf2ys5ybwkuc2vydmvylljlbw90zvn0dwlp/tzji+flggiaahhyabxqyxzhlnjtas5zzxj2zxiuumvtb3rlt2jqzwn002g0kqxhmx4daab4chc3aatvbmljyxn0umvmmgaaddewljeyljexnc4znwaa9bhcc21u1z9pmpllr/0aaafaxsf5moacahg=] attaching jvm [27928]... heap usage: init = 8388608(8192k) used = 40242696(39299k) committed = 44236800(43200k) max = 5726666752(5592448k) ====================================== jvm pid: 25028 display: org.jboss.main -c ecseu -b 0.0.0.0 think can find jvm's management agent here: [c:\java\jdk1.6.0_30\jre\lib\management-agent.jar]     connector installed @ [service:jmx:rmi://127.0.0.1/stub/ro0abxnyac5qyxzhec5tyw5hz2vtzw50lnjlbw90zs5ybwkuuk1ju2vydmvysw1wbf9tdhviaaaaaaaaaaicaab4cgaaamf2ys5ybwkuc2vydmvylljlbw90zvn0dwlp/tzji+flggiaahhyabxqyxzhlnjtas5zzxj2zxiuumvtb3rlt2jqzwn002g0kqxhmx4daab4chc4aatvbmljyxn0umvmmgaadvbqlvdllu5xsektmdeaapjjtzybxtjrukazapteaaabqmukx+6abab4] attaching jvm [25028]... heap usage: init = 1073741824(1048576k) used = 173876432(169801k) committed = 982581248(959552k) max = 982581248(959552k) ====================================== 

caveats

  1. jmvs can attach bit jvms (i.e. 32bit 32bit, 64bit 64bit)
  2. the os user launching attach must have os authorization access other processed. same user, no problem. other user... should root.
  3. the attach api has mixed results when used across different jvm vendor implementations. ie. if it's sun/oracle/openjdk, you're good. if it's those, , you're trying connect ibm jvm (or vice-versa) have no idea happen, although jrockit seems friendly in regard.
  4. i don't know all caveats.

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -