Wednesday, 21 June 2017

[Java] How to find running java process in java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 *********************************************************
 * FindRunningJavaProcess.java
 *********************************************************
 * @version 1.0.00 2017. 6. 21. dorbae(dorbae.io@gmail.com) Initialize
 * @since 2017. 6. 21.
 * @author dorbae(dorbae.io@gmail.com) Initialize
 */
package pe.dorbae.test;

import java.util.Iterator;
import java.util.Set;

import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;

/**
 * @author Administrator
 *
 */
public class FindRunningJavaProcess {

 /**
  *********************************************************
  * @param args
  *********************************************************
  * @version 1.0.00 2017. 6. 21. dorbae(dorbae.io@gmail.com) Initialize
  * @since 2017. 6. 21.
  * @author dorbae(dorbae.io@gmail.com) Initialize
  */
 public static void main(String[] args) {
  
     MonitoredVm vm = null;
     VmIdentifier id = null;
     int lvmId = -1;
     String className = null;
     String arg = null;
     String vmArg = null;
     String vmVersion = null;
     String commandLine = null;
         
  try {
   
   sun.jvmstat.monitor.HostIdentifier hostId = new sun.jvmstat.monitor.HostIdentifier( "localhost");
   sun.jvmstat.monitor.MonitoredHost monitoredHost = sun.jvmstat.monitor.MonitoredHost.getMonitoredHost( hostId);
   
    // Get Active VMs
   Set< Integer> jvms = monitoredHost.activeVms();
   if ( jvms == null)
    System.out.println( "ActiveVM is null.");
   else if ( jvms.size() < 1)
    System.out.println( "ActiveVM's size is 0.");
  
   
   for ( Iterator< Integer> j = jvms.iterator(); j.hasNext(); /* empty */ ) {
             lvmId = j.next().intValue();
         
             String vmidString = "//" + lvmId;
         
             try {
              id = new VmIdentifier( vmidString);
             
              try {
               vm = monitoredHost.getMonitoredVm( id, 0);
               
               if ( vm == null) {
                   System.out.println( "MonitoredVm is null).");
                   continue;
                  }
               
               // Get Java class name. true : full path including package, false : only class name
               className = MonitoredVmUtil.mainClass( vm, true);
               // Program arguments
               arg = MonitoredVmUtil.mainArgs( vm);
               // VM options
               vmArg = MonitoredVmUtil.jvmArgs( vm);
               // JVM version
               vmVersion = MonitoredVmUtil.vmVersion( vm);
               // Command line
               commandLine = MonitoredVmUtil.commandLine( vm);

               System.out.printf( "CLASS=[%s], ARG=[%s], VMARG=[%s], VMVERSION=[%s], COMMANDLINE=[%s]\n"
                 , className
                 , arg
                 , vmArg
                 , vmVersion
                 , commandLine
                 );
               
              } catch ( Exception e) {
               System.out.println( "Failed to get MonitoredVm.");
               e.printStackTrace();
               
              }
              
             } catch ( Exception e) {
              e.printStackTrace();
              
             } finally {
              if ( vm != null)
               try {
                monitoredHost.detach( vm);
               } catch ( Exception e) {
                e.printStackTrace();
                
               }
             }
             
         } // End of for
   
  } catch ( Exception e) {
   e.printStackTrace();
   
        
  }
 }
        
}

No comments:

Post a Comment