Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jps/Jps.java
41159 views
/*1* Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jps;2627import java.util.*;28import java.net.*;29import sun.jvmstat.monitor.*;3031/**32* Application to provide a listing of monitorable java processes.33*34* @author Brian Doherty35* @since 1.536*/37public class Jps {3839private static Arguments arguments;4041public static void main(String[] args) {42try {43arguments = new Arguments(args);44} catch (IllegalArgumentException e) {45System.err.println(e.getMessage());46Arguments.printUsage(System.err);47System.exit(1);48}4950if (arguments.isHelp()) {51Arguments.printUsage(System.err);52System.exit(0);53}5455try {56HostIdentifier hostId = arguments.hostId();57MonitoredHost monitoredHost =58MonitoredHost.getMonitoredHost(hostId);5960// get the set active JVMs on the specified host.61Set<Integer> jvms = monitoredHost.activeVms();6263for (Integer jvm: jvms) {64StringBuilder output = new StringBuilder();65Throwable lastError = null;6667int lvmid = jvm;6869output.append(String.valueOf(lvmid));7071if (arguments.isQuiet()) {72System.out.println(output);73continue;74}7576MonitoredVm vm = null;77String vmidString = "//" + lvmid + "?mode=r";7879String errorString = null;8081try {82// Note: The VM associated with the current VM id may83// no longer be running so these queries may fail. We84// already added the VM id to the output stream above.85// If one of the queries fails, then we try to add a86// reasonable message to indicate that the requested87// info is not available.8889errorString = " -- process information unavailable";90VmIdentifier id = new VmIdentifier(vmidString);91vm = monitoredHost.getMonitoredVm(id, 0);9293errorString = " -- main class information unavailable";94output.append(' ').append(MonitoredVmUtil.mainClass(vm,95arguments.showLongPaths()));9697if (arguments.showMainArgs()) {98errorString = " -- main args information unavailable";99String mainArgs = MonitoredVmUtil.mainArgs(vm);100if (mainArgs != null && mainArgs.length() > 0) {101output.append(' ').append(mainArgs);102}103}104if (arguments.showVmArgs()) {105errorString = " -- jvm args information unavailable";106String jvmArgs = MonitoredVmUtil.jvmArgs(vm);107if (jvmArgs != null && jvmArgs.length() > 0) {108output.append(' ')109.append(110// multi-line args are permitted111jvmArgs.replace("\n", "\\n").replace("\r", "\\r")112);113}114}115if (arguments.showVmFlags()) {116errorString = " -- jvm flags information unavailable";117String jvmFlags = MonitoredVmUtil.jvmFlags(vm);118if (jvmFlags != null && jvmFlags.length() > 0) {119output.append(' ').append(jvmFlags);120}121}122123errorString = " -- detach failed";124monitoredHost.detach(vm);125126System.out.println(output);127128errorString = null;129} catch (URISyntaxException e) {130// unexpected as vmidString is based on a validated hostid131lastError = e;132assert false;133} catch (Exception e) {134lastError = e;135} finally {136if (errorString != null) {137/*138* we ignore most exceptions, as there are race139* conditions where a JVM in 'jvms' may terminate140* before we get a chance to list its information.141* Other errors, such as access and I/O exceptions142* should stop us from iterating over the complete set.143*/144output.append(errorString);145if (arguments.isDebug()) {146if ((lastError != null)147&& (lastError.getMessage() != null)) {148output.append("\n\t");149output.append(lastError.getMessage());150}151}152System.out.println(output);153if (arguments.printStackTrace()) {154lastError.printStackTrace();155}156continue;157}158}159}160} catch (MonitorException e) {161if (e.getMessage() != null) {162System.err.println(e.getMessage());163} else {164Throwable cause = e.getCause();165if ((cause != null) && (cause.getMessage() != null)) {166System.err.println(cause.getMessage());167} else {168e.printStackTrace();169}170}171System.exit(1);172}173}174}175176177