Monitor { @SuppressWarnings("checkstyle:regexp") public static void main(final String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { int sepPos = args.length; for (int i = 0; i < args.length; i++) { if ("--".equals(args[i])) { sepPos = i; break; } } String[] newArgs; String[] ownArgs; if (sepPos == args.length) { newArgs = new String[0]; ownArgs = args; } else { newArgs = new String[args.length - sepPos - 1]; ownArgs = new String[sepPos]; System.arraycopy(args, sepPos + 1, newArgs, 0, newArgs.length); System.arraycopy(args, 0, ownArgs, 0, sepPos); } Options options = new Options(); CmdLineParser parser = new CmdLineParser(options); try { parser.parseArgument(ownArgs); } catch (CmdLineException e) { System.err.println("Error: " + e.getMessage() + "\nUsage:"); parser.printUsage(System.err); System.exit(SysExits.EX_USAGE.exitCode()); } final Sampler sampler = new Sampler(options.sampleInterval, options.dumpInterval, (t) -> new FastStackCollector(false, true, new Thread[]{t}), options.dumpFolder, options.dumpFilePrefix); Runtime.getRuntime().addShutdownHook(new Thread(new AbstractRunnable() { @Override public void doRun() throws InterruptedException, IOException { sampler.stop(); sampler.dumpToFile(); sampler.dispose(); } }, "Sampling report")); sampler.registerJmx(); if (options.startSampler) { sampler.start(); } Class.forName(options.mainClass).getMethod("main", String[].class).invoke(null, (Object) newArgs); System.exit(SysExits.OK.exitCode()); } private  Monitor(); @SuppressWarnings("checkstyle:regexp") static void main(final String[] args);  }
@Test(expected = ExitException.class) public void testError() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IOException { String report = File.createTempFile("stackSample", ".html").getPath(); System.setSecurityManager(new NoExitSecurityManager()); Monitor.main(new String[]{"-ASDF", "-f", report, "-ss", "-si", "10", "-w", "600", "-main", MonitorTest.class.getName()}); }
@Test public void testJmx() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { try { Monitor.main(new String[]{"-ss", "-si", "10", "-main", MonitorTest.class.getName()}); } catch (ExitException ex) { Assert.assertEquals(0, ex.getExitCode()); } }