src/share/classes/sun/tools/jinfo/JInfo.java

Print this page
rev 8717 : 8027765: Make exit codes and stdout/stderr printing from jmap/jinfo/jstack/jps consistent
Reviewed-by:

@@ -42,11 +42,11 @@
  */
 public class JInfo {
 
     public static void main(String[] args) throws Exception {
         if (args.length == 0) {
-            usage(); // no arguments
+            usage(1); // no arguments
         }
 
         boolean useSA = true;
         String arg1 = args[0];
         if (arg1.startsWith("-")) {

@@ -54,18 +54,24 @@
                 arg1.equals("-sysprops")) {
                 // SA JInfo needs <pid> or <server> or
                 // (<executable> and <code file>). So, total
                 // argument count including option has to 2 or 3.
                 if (args.length != 2 && args.length != 3) {
-                    usage();
+                    usage(1);
                 }
             } else if (arg1.equals("-flag")) {
                 // do not use SA, use attach-on-demand
                 useSA = false;
             } else {
                 // unknown option or -h or -help, print help
-                usage();
+                int exit;
+                if (arg1.equals("-help") || arg1.equals("-h")) {
+                    exit = 0;
+                } else {
+                    exit = 1;
+                }
+                usage(exit);
             }
         }
 
         if (useSA) {
             runTool(args);

@@ -73,22 +79,23 @@
             if (args.length == 3) {
                 String pid = args[2];
                 String option = args[1];
                 flag(pid, option);
             } else {
-                usage();
+                int exit = arg1.equals("-help") || arg1.equals("-h") ? 0 : 1;
+                usage(exit);
             }
         }
     }
 
     // Invoke SA tool  with the given arguments
     private static void runTool(String args[]) throws Exception {
         String tool = "sun.jvm.hotspot.tools.JInfo";
         // Tool not available on this  platform.
         Class<?> c = loadClass(tool);
         if (c == null) {
-            usage();
+            usage(1);
         }
 
         // invoke the main method with the arguments
         Class[] argTypes = { String[].class } ;
         Method m = c.getDeclaredMethod("main", argTypes);

@@ -174,41 +181,41 @@
         vm.detach();
     }
 
 
     // print usage message
-    private static void usage() {
+    private static void usage(int exit) {
 
         Class<?> c = loadClass("sun.jvm.hotspot.tools.JInfo");
         boolean usageSA = (c != null);
 
-        System.out.println("Usage:");
+        System.err.println("Usage:");
         if (usageSA) {
-            System.out.println("    jinfo [option] <pid>");
-            System.out.println("        (to connect to running process)");
-            System.out.println("    jinfo [option] <executable <core>");
-            System.out.println("        (to connect to a core file)");
-            System.out.println("    jinfo [option] [server_id@]<remote server IP or hostname>");
-            System.out.println("        (to connect to remote debug server)");
-            System.out.println("");
-            System.out.println("where <option> is one of:");
-            System.out.println("    -flag <name>         to print the value of the named VM flag");
-            System.out.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
-            System.out.println("    -flag <name>=<value> to set the named VM flag to the given value");
-            System.out.println("    -flags               to print VM flags");
-            System.out.println("    -sysprops            to print Java system properties");
-            System.out.println("    <no option>          to print both of the above");
-            System.out.println("    -h | -help           to print this help message");
-        } else {
-            System.out.println("    jinfo <option> <pid>");
-            System.out.println("       (to connect to a running process)");
-            System.out.println("");
-            System.out.println("where <option> is one of:");
-            System.out.println("    -flag <name>         to print the value of the named VM flag");
-            System.out.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
-            System.out.println("    -flag <name>=<value> to set the named VM flag to the given value");
-            System.out.println("    -h | -help           to print this help message");
+            System.err.println("    jinfo [option] <pid>");
+            System.err.println("        (to connect to running process)");
+            System.err.println("    jinfo [option] <executable <core>");
+            System.err.println("        (to connect to a core file)");
+            System.err.println("    jinfo [option] [server_id@]<remote server IP or hostname>");
+            System.err.println("        (to connect to remote debug server)");
+            System.err.println("");
+            System.err.println("where <option> is one of:");
+            System.err.println("    -flag <name>         to print the value of the named VM flag");
+            System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
+            System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
+            System.err.println("    -flags               to print VM flags");
+            System.err.println("    -sysprops            to print Java system properties");
+            System.err.println("    <no option>          to print both of the above");
+            System.err.println("    -h | -help           to print this help message");
+        } else {
+            System.err.println("    jinfo <option> <pid>");
+            System.err.println("       (to connect to a running process)");
+            System.err.println("");
+            System.err.println("where <option> is one of:");
+            System.err.println("    -flag <name>         to print the value of the named VM flag");
+            System.err.println("    -flag [+|-]<name>    to enable or disable the named VM flag");
+            System.err.println("    -flag <name>=<value> to set the named VM flag to the given value");
+            System.err.println("    -h | -help           to print this help message");
         }
 
-        System.exit(1);
+        System.exit(exit);
     }
 }