Gets the current user to achieve all the java process and jps command

We often encounter such problems in development, all need to find a Java application running locally out in order to perform an operation. Therefore, the common idea is that different implementations on different platforms:
use netstat under windows, using ps -ef under linux. Linux under relatively easier, since ps -ef can jvm detailed parameters and file path are also listed, the program operation is more convenient. Of course, if you have used the jps java tool, it will feel more convenient.
jps of usage:


usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]

Definitions:
<hostid>: <hostname>[:<port>]

Just enter jps on the command line, a list of all current users of java process.
Of course, the standard output only the name of pid and Main Class. Plus specific parameters -l lists the full class name Main Class, plus -v, lists details of jvm parameters:
For example:

6544 sun.tools.jps.Jps -Dapplication.home=C:\Program Files (x86)\Java\jdk1.6.0_4
5 -Xms8m
3920 sun.tools.jconsole.JConsole -Dapplication.home=D:\Java\jdk1.6.0_10 -Djconso
le.showOutputViewer


According to the full class name, pid can be used to solve common problems.
But as a programmer has pursued: oops:, we can not help but want to, jps is how to achieve it, is to use different commands depending on the platform you?
With this kind of attitude, we can see the OpenJDK source code under the jps. After reading suddenly realized: the original is this. 8)

***

each launch a java application, it creates a temporary file in the current user's temporary directory, pid to the application name.


public static final String dirNamePrefix = "hsperfdata_";
public static String getTempDirectory(String user) {
return tmpDirName + dirNamePrefix + user + File.separator;
}
在windows中,如果没有设置java.io.tmpdir,则会在C:\Users\UserName\AppData\Local\Temp\hsperfdata_UserName\目录下创建xxx(pid)

jps according to these documents, access to the local java process, and the specific Main CLass names.
Get the code:
Jps

try {
HostIdentifier hostId = arguments.hostId();
MonitoredHost monitoredHost =
MonitoredHost.getMonitoredHost(hostId);

// get the set active JVMs on the specified host.
Set jvms = monitoredHost.activeVms();

for (Iterator j = jvms.iterator(); j.hasNext(); /* empty */ ) {
StringBuilder output = new StringBuilder();
Throwable lastError = null;

int lvmid = ((Integer)j.next()).intValue();

output.append(String.valueOf(lvmid));

if (arguments.isQuiet()) {
System.out.println(output);
continue;
}

MonitoredVm vm = null;
String vmidString = "//" + lvmid + "?mode=r";

try {
VmIdentifier id = new VmIdentifier(vmidString);
vm = monitoredHost.getMonitoredVm(id, 0);
} catch (URISyntaxException e) {
// unexpected as vmidString is based on a validated hostid
lastError = e;
assert false;
} catch (Exception e) {
lastError = e;
} finally {
if (vm == null) {
/*
* we ignore most exceptions, as there are race
* conditions where a JVM in 'jvms' may terminate
* before we get a chance to list its information.
* Other errors, such as access and I/O exceptions
* should stop us from iterating over the complete set.
*/
output.append(" -- process information unavailable");
if (arguments.isDebug()) {
if ((lastError != null)
&& (lastError.getMessage() != null)) {
output.append("\n\t");
output.append(lastError.getMessage());
}
}
System.out.println(output);
if (arguments.printStackTrace()) {
lastError.printStackTrace();
}
continue;
}
}

output.append(" ");
output.append(MonitoredVmUtil.mainClass(vm,
arguments.showLongPaths()));
以下是activeVMs方法的代码:
/**
* Return the current set of monitorable Java Virtual Machines.
* <p>
* The set returned by this method depends on the user name passed
* to the constructor. If no user name was specified, then this
* method will return all candidate JVMs on the system. Otherwise,
* only the JVMs for the given user will be returned. This assumes
* that principal associated with this JVM has the appropriate
* permissions to access the target set of JVMs.
*
* @return Set - the Set of monitorable Java Virtual Machines
*/
public synchronized Set<Integer> activeVms() {
/*
* This method is synchronized because the Matcher object used by
* fileFilter is not safe for concurrent use, and this method is
* called by multiple threads. Before this method was synchronized,
* we'd see strange file names being matched by the matcher.
*/
Set<Integer> jvmSet = new HashSet<Integer>();

if (! tmpdir.isDirectory()) {
return jvmSet;
}

if (userName == null) {
/*
* get a list of all of the user temporary directories and
* iterate over the list to find any files within those directories.
*/
File[] dirs = tmpdir.listFiles(userFilter);

for (int i = 0 ; i < dirs.length; i ++) {
if (!dirs[i].isDirectory()) {
continue;
}

// get a list of files from the directory
File[] files = dirs[i].listFiles(fileFilter);

if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {
jvmSet.add(new Integer(
PerfDataFile.getLocalVmId(files[j])));
}
}
}
}
} else {
/*
* Check if the user directory can be accessed. Any of these
* conditions may have asynchronously changed between subsequent
* calls to this method.
*/

// get the list of files from the specified user directory
File[] files = tmpdir.listFiles(fileFilter);

if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {
jvmSet.add(new Integer(
PerfDataFile.getLocalVmId(files[j])));
}
}
}
}

// look for any 1.4.1 files
File[] files = tmpdir.listFiles(tmpFileFilter);
if (files != null) {
for (int j = 0; j < files.length; j++) {
if (files[j].isFile() && files[j].canRead()) {
jvmSet.add(new Integer(
PerfDataFile.getLocalVmId(files[j])));
}
}
}

return jvmSet;
}
}

Through these, the implementation jps understand, the original is not execute different commands in different systems, ha ha
Published 56 original articles · won praise 0 · Views 7785

Guess you like

Origin blog.csdn.net/chainhou/article/details/84433779