1/* 2 * Copyright (c) 2000, 2013, 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 23 * questions. 24 */ 25 26 27import java.io.FileNotFoundException; 28import java.io.IOException; 29import java.lang.reflect.Method; 30import java.net.URL; 31import java.net.URLClassLoader; 32import java.util.jar.Attributes; 33import java.util.jar.JarFile; 34 35/** Run an application (from a jar file) and terminate it after a given 36 * number of seconds. 37 */ 38 39public class MaxTime { 40 41 private static boolean debugFlag = false; 42 43 44 private static void debug(String s) { 45 if (debugFlag) { 46 System.err.println(s); 47 } 48 } 49 50 51 private static void usage() { 52 System.err.println( 53 "Usage:\n" + 54 " java MaxTime <jarfile> <timeout>\n" + 55 "\n" + 56 " <jarfile> is a jar file specifying an application to run\n" + 57 " <timeout> is an integer number of seconds to allow the app to run." ); 58 System.exit(1); 59 } 60 61 62 public static void main(String[] args) { 63 int timeoutPeriod = 0; 64 String mainClass = null; 65 66 if (args.length != 2) 67 usage(); 68 69 // Identify the timeout value. 70 try { 71 timeoutPeriod = Integer.parseInt(args[1]); 72 } catch (Exception e) { 73 usage(); 74 } 75 76 // Identify the application's main class. 77 try { 78 mainClass = new JarFile(args[0]).getManifest() 79 .getMainAttributes().getValue(Attributes.Name.MAIN_CLASS); 80 } catch (FileNotFoundException e) { 81 System.err.println("Can't find " + args[0]); 82 System.exit(1); 83 } catch (IOException e) { 84 e.printStackTrace(); 85 System.exit(2); 86 } 87 88 // Set the exit timer. 89 final int timeout = timeoutPeriod * 1000; 90 new Thread() { 91 public void run() { 92 try { 93 debug("Before timeout!"); 94 Thread.sleep(timeout); 95 debug("After timeout!"); 96 } catch (InterruptedException e) { 97 } 98 debug("Exit caused by timeout!"); 99 System.exit(0); 100 } 101 }.start(); 102 103 104 // Start up the app. 105 try { 106 ClassLoader cl = new URLClassLoader( 107 new URL[] {new URL("file:"+args[0])}); 108 Class clazz = cl.loadClass(mainClass); 109 String[] objs = new String[0]; 110 Method mainMethod = clazz.getMethod("main", new Class[]{objs.getClass()}); 111 mainMethod.invoke(null, new Object[]{objs}); 112 } catch (Exception e) { 113 e.printStackTrace(); 114 System.exit(3); 115 } 116 } 117} 118