< prev index next >

src/jdk.jcmd/share/classes/sun/tools/jcmd/Arguments.java

Print this page
rev 48243 : 8189102: All tools should support -?, -h and --help
Reviewed-by: kvn, jjg, weijun, alanb, rfield, ksrini
   1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  33     private boolean listProcesses = false;
  34     private boolean listCounters  = false;
  35     private boolean showUsage     = false;
  36     private String  command       = null;
  37     private String  processString = null;
  38 
  39     public boolean isListProcesses() { return listProcesses; }
  40     public boolean isListCounters() { return listCounters; }
  41     public boolean isShowUsage() { return showUsage; }
  42     public String getCommand() { return command; }
  43     public String getProcessString() { return processString; }
  44 
  45     public Arguments(String[] args) {
  46         if (args.length == 0 || args[0].equals("-l")) {
  47             listProcesses = true;
  48             /* list all processes */
  49             processString = "0";
  50             return;
  51         }
  52 
  53         if (args[0].equals("-h") || args[0].equals("-help") ) {




  54             showUsage = true;
  55             return;
  56         }
  57 
  58         processString = args[0];
  59 
  60         StringBuilder sb = new StringBuilder();
  61         for (int i = 1; i < args.length; i++) {
  62             if (args[i].equals("-f")) {
  63                 if (args.length == i + 1) {
  64                     throw new IllegalArgumentException(
  65                         "No file specified for parameter -f");
  66                 } else if (args.length == i + 2) {
  67                     try {
  68                         readCommandFile(args[i + 1]);
  69                     } catch(IOException e) {
  70                         throw new IllegalArgumentException(
  71                             "Could not read from file specified with -f option: "
  72                             + args[i + 1]);
  73                     }


  99                 }
 100                 command = sb.toString();
 101             }
 102     }
 103 
 104     public static void usage() {
 105         System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");
 106         System.out.println("   or: jcmd -l                                                    ");
 107         System.out.println("   or: jcmd -h                                                    ");
 108         System.out.println("                                                                  ");
 109         System.out.println("  command must be a valid jcmd command for the selected jvm.      ");
 110         System.out.println("  Use the command \"help\" to see which commands are available.   ");
 111         System.out.println("  If the pid is 0, commands will be sent to all Java processes.   ");
 112         System.out.println("  The main class argument will be used to match (either partially ");
 113         System.out.println("  or fully) the class used to start Java.                         ");
 114         System.out.println("  If no options are given, lists Java processes (same as -l).     ");
 115         System.out.println("                                                                  ");
 116         System.out.println("  PerfCounter.print display the counters exposed by this process  ");
 117         System.out.println("  -f  read and execute commands from the file                     ");
 118         System.out.println("  -l  list JVM processes on the local machine                     ");
 119         System.out.println("  -h  this help                                                   ");
 120     }
 121 }
   1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  33     private boolean listProcesses = false;
  34     private boolean listCounters  = false;
  35     private boolean showUsage     = false;
  36     private String  command       = null;
  37     private String  processString = null;
  38 
  39     public boolean isListProcesses() { return listProcesses; }
  40     public boolean isListCounters() { return listCounters; }
  41     public boolean isShowUsage() { return showUsage; }
  42     public String getCommand() { return command; }
  43     public String getProcessString() { return processString; }
  44 
  45     public Arguments(String[] args) {
  46         if (args.length == 0 || args[0].equals("-l")) {
  47             listProcesses = true;
  48             /* list all processes */
  49             processString = "0";
  50             return;
  51         }
  52 
  53         if (args[0].equals("-?") ||
  54             args[0].equals("-h") ||
  55             args[0].equals("--help") ||
  56             // -help: legacy.
  57             args[0].equals("-help")) {
  58             showUsage = true;
  59             return;
  60         }
  61 
  62         processString = args[0];
  63 
  64         StringBuilder sb = new StringBuilder();
  65         for (int i = 1; i < args.length; i++) {
  66             if (args[i].equals("-f")) {
  67                 if (args.length == i + 1) {
  68                     throw new IllegalArgumentException(
  69                         "No file specified for parameter -f");
  70                 } else if (args.length == i + 2) {
  71                     try {
  72                         readCommandFile(args[i + 1]);
  73                     } catch(IOException e) {
  74                         throw new IllegalArgumentException(
  75                             "Could not read from file specified with -f option: "
  76                             + args[i + 1]);
  77                     }


 103                 }
 104                 command = sb.toString();
 105             }
 106     }
 107 
 108     public static void usage() {
 109         System.out.println("Usage: jcmd <pid | main class> <command ...|PerfCounter.print|-f file>");
 110         System.out.println("   or: jcmd -l                                                    ");
 111         System.out.println("   or: jcmd -h                                                    ");
 112         System.out.println("                                                                  ");
 113         System.out.println("  command must be a valid jcmd command for the selected jvm.      ");
 114         System.out.println("  Use the command \"help\" to see which commands are available.   ");
 115         System.out.println("  If the pid is 0, commands will be sent to all Java processes.   ");
 116         System.out.println("  The main class argument will be used to match (either partially ");
 117         System.out.println("  or fully) the class used to start Java.                         ");
 118         System.out.println("  If no options are given, lists Java processes (same as -l).     ");
 119         System.out.println("                                                                  ");
 120         System.out.println("  PerfCounter.print display the counters exposed by this process  ");
 121         System.out.println("  -f  read and execute commands from the file                     ");
 122         System.out.println("  -l  list JVM processes on the local machine                     ");
 123         System.out.println("  -? -h --help print this help message                            ");
 124     }
 125 }
< prev index next >