Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/non-build-utils/reorder/tools/MaxTime.java
32285 views
/*1* Copyright (c) 2000, 2013, 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*/242526import java.io.FileNotFoundException;27import java.io.IOException;28import java.lang.reflect.Method;29import java.net.URL;30import java.net.URLClassLoader;31import java.util.jar.Attributes;32import java.util.jar.JarFile;3334/** Run an application (from a jar file) and terminate it after a given35* number of seconds.36*/3738public class MaxTime {3940private static boolean debugFlag = false;414243private static void debug(String s) {44if (debugFlag) {45System.err.println(s);46}47}484950private static void usage() {51System.err.println(52"Usage:\n" +53" java MaxTime <jarfile> <timeout>\n" +54"\n" +55" <jarfile> is a jar file specifying an application to run\n" +56" <timeout> is an integer number of seconds to allow the app to run." );57System.exit(1);58}596061public static void main(String[] args) {62int timeoutPeriod = 0;63String mainClass = null;6465if (args.length != 2)66usage();6768// Identify the timeout value.69try {70timeoutPeriod = Integer.parseInt(args[1]);71} catch (Exception e) {72usage();73}7475// Identify the application's main class.76try {77mainClass = new JarFile(args[0]).getManifest()78.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);79} catch (FileNotFoundException e) {80System.err.println("Can't find " + args[0]);81System.exit(1);82} catch (IOException e) {83e.printStackTrace();84System.exit(2);85}8687// Set the exit timer.88final int timeout = timeoutPeriod * 1000;89new Thread() {90public void run() {91try {92debug("Before timeout!");93Thread.sleep(timeout);94debug("After timeout!");95} catch (InterruptedException e) {96}97debug("Exit caused by timeout!");98System.exit(0);99}100}.start();101102103// Start up the app.104try {105ClassLoader cl = new URLClassLoader(106new URL[] {new URL("file:"+args[0])});107Class clazz = cl.loadClass(mainClass);108String[] objs = new String[0];109Method mainMethod = clazz.getMethod("main", new Class[]{objs.getClass()});110mainMethod.invoke(null, new Object[]{objs});111} catch (Exception e) {112e.printStackTrace();113System.exit(3);114}115}116}117118119