すべてのJavaプロセスとJPSコマンドを達成するために現在のユーザーを取得します。

私たちはしばしば、開発中の操作を行うために外にローカルで実行中のJavaアプリケーションを見つけるために、すべての必要性を、このような問題が発生しました。:そのため、一般的なアイデアは、異なるプラットフォーム上の異なる実装である
Linuxでのps -efを使用して、Windowsで使用するのnetstat。PS -efは、詳細なパラメータやファイルパスもリストされているJVMができるので、比較的簡単に下のLinuxは、プログラム動作がより便利です。あなたはJPSのJavaツールを使用している場合はもちろん、それはより便利に感じるでしょう。
用法のJPS:


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

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

ただ、コマンドラインでJavaプロセスの現在のすべてのユーザーのリストをJPSを入力してください。
もちろん、標準出力PIDとメインクラスの名前のみ。プラス特定のパラメータ-lリストの完全なクラス名のメインクラス、プラス-v、JVMパラメータのリストの詳細:
例えば:

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


完全なクラス名によると、pidは一般的な問題を解決するために使用することができます。
しかし、プログラマとして追求してきた:JPSはどのようにそれを達成するためにされ、おっと:,我々は助けることはできませんがしたい、プラットフォームあなたに応じて異なるコマンドを使用することですか?
態度のこの種では、我々はJPS下OpenJDKのソースコードを見ることができます。突然、読んだ後に実現:オリジナルはこれです。8)

***

各打ち上げjavaアプリケーションは、アプリケーション名にpidの現在のユーザーの一時ディレクトリに一時ファイルを作成します。


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)

これらの文書、ローカルのJavaプロセスへのアクセス、および特定のメインクラス名に応じJPS。
コードを取得する:
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;
}
}

これらを通じ、実装のJPSは理解して、元はハハ、異なるシステムで異なるコマンドを実行していません
公開された56元の記事 ウォンの賞賛0 ビュー7785

おすすめ

転載: blog.csdn.net/chainhou/article/details/84433779