Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/classes/apple/launcher/JavaAppLauncher.java
38829 views
/*1* Copyright (c) 2011, 2012, 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*/2425package apple.launcher;2627import java.io.*;28import java.lang.reflect.*;29import java.text.MessageFormat;30import java.util.*;31import java.util.jar.*;3233import javax.swing.*;3435class JavaAppLauncher implements Runnable {36static {37java.security.AccessController.doPrivileged(38new java.security.PrivilegedAction<Void>() {39public Void run() {40System.loadLibrary("osx");41return null;42}43});44}4546private static native <T> T nativeConvertAndRelease(final long ptr);47private static native void nativeInvokeNonPublic(Class<? extends Method> cls, Method m, String[] args);4849// entry point from native50static void launch(final long javaDictionaryPtr, final boolean verbose) {51final Map<String, ?> javaDictionary = nativeConvertAndRelease(javaDictionaryPtr);52(new JavaAppLauncher(javaDictionary, verbose)).run();53}5455// these are the values for the enumeration JavaFailureMode56static final String kJavaFailureMainClassNotSpecified = "MainClassNotSpecified";57static final String kJavaFailureMainClassNotFound = "CannotLoadMainClass";58static final String kJavaFailureMainClassHasNoMain = "NoMainMethod";59static final String kJavaFailureMainClassMainNotStatic = "MainNotStatic";60static final String kJavaFailureMainThrewException = "MainThrewException";61static final String kJavaFailureMainInitializerException = "MainInitializerException";6263final boolean verbose; // Normally set by environment variable JAVA_LAUNCHER_VERBOSE.64final Map<String, ?> javaDictionary;6566JavaAppLauncher(final Map<String, ?> javaDictionary, final boolean verbose) {67this.verbose = verbose;68this.javaDictionary = javaDictionary;69}7071@Override72public void run() {73final Method m = loadMainMethod(getMainMethod());74final String methodName = m.getDeclaringClass().getName() + ".main(String[])";75try {76log("Calling " + methodName + " method");77m.invoke(null, new Object[] { getArguments() });78log(methodName + " has returned");79} catch (final IllegalAccessException x) {80try {81nativeInvokeNonPublic(m.getClass(), m, getArguments());82} catch (final Throwable excpt) {83logError(methodName + " threw an exception:");84if ((excpt instanceof UnsatisfiedLinkError) && excpt.getMessage().equals("nativeInvokeNonPublic")) {85showFailureAlertAndKill(kJavaFailureMainThrewException, "nativeInvokeNonPublic not registered");86} else {87excpt.printStackTrace();88showFailureAlertAndKill(kJavaFailureMainThrewException, excpt.toString());89}90}91} catch (final InvocationTargetException invokeExcpt) {92logError(methodName + " threw an exception:");93invokeExcpt.getTargetException().printStackTrace();94showFailureAlertAndKill(kJavaFailureMainThrewException, invokeExcpt.getTargetException().toString());95}96}9798Method loadMainMethod(final String mainClassName) {99try {100final Class<?> mainClass = Class.forName(mainClassName, true, sun.misc.Launcher.getLauncher().getClassLoader());101final Method mainMethod = mainClass.getDeclaredMethod("main", new Class[] { String[].class });102if ((mainMethod.getModifiers() & Modifier.STATIC) == 0) {103logError("The main(String[]) method of class " + mainClassName + " is not static!");104showFailureAlertAndKill(kJavaFailureMainClassMainNotStatic, mainClassName);105}106return mainMethod;107} catch (final ExceptionInInitializerError x) {108logError("The main class \"" + mainClassName + "\" had a static initializer throw an exception.");109x.getException().printStackTrace();110showFailureAlertAndKill(kJavaFailureMainInitializerException, x.getException().toString());111} catch (final ClassNotFoundException x) {112logError("The main class \"" + mainClassName + "\" could not be found.");113showFailureAlertAndKill(kJavaFailureMainClassNotFound, mainClassName);114} catch (final NoSuchMethodException x) {115logError("The main class \"" + mainClassName + "\" has no static main(String[]) method.");116showFailureAlertAndKill(kJavaFailureMainClassHasNoMain, mainClassName);117} catch (final NullPointerException x) {118logError("No main class specified");119showFailureAlertAndKill(kJavaFailureMainClassNotSpecified, null);120}121122return null;123}124125// get main class name from 'Jar' key, or 'MainClass' key126String getMainMethod() {127final Object javaJar = javaDictionary.get("Jar");128if (javaJar != null) {129if (!(javaJar instanceof String)) {130logError("'Jar' key in 'Java' sub-dictionary of Info.plist requires a string value");131return null;132}133134final String jarPath = (String)javaJar;135if (jarPath.length() == 0) {136log("'Jar' key of sub-dictionary 'Java' of Info.plist key is empty");137} else {138// extract main class from manifest of this jar139final String main = getMainFromManifest(jarPath);140if (main == null) {141logError("jar file '" + jarPath + "' does not have Main-Class: attribute in its manifest");142return null;143}144145log("Main class " + main + " found in jar manifest");146return main;147}148}149150final Object javaMain = javaDictionary.get("MainClass");151if (!(javaMain instanceof String)) {152logError("'MainClass' key in 'Java' sub-dictionary of Info.plist requires a string value");153return null;154}155156final String main = (String)javaMain;157if (main.length() == 0) {158log("'MainClass' key of sub-dictionary 'Java' of Info.plist key is empty");159return null;160}161162log("Main class " + (String)javaMain + " found via 'MainClass' key of sub-dictionary 'Java' of Info.plist key");163return (String)javaMain;164}165166// get arguments for main(String[]) out of Info.plist and command line167String[] getArguments() {168// check for 'Arguments' key, which contains the main() args if not defined in Info.plist169final Object javaArguments = javaDictionary.get("Arguments");170if (javaArguments == null) {171// no arguments172log("No arguments for main(String[]) specified");173return new String[0];174}175176if (javaArguments instanceof List) {177final List<?> args = (List<?>)javaArguments;178final int count = args.size();179log("Arguments to main(String[" + count + "]):");180181final String[] result = new String[count];182for (int i = 0; i < count; ++i) {183final Object element = args.get(i);184if (element instanceof String) {185result[i] = (String)element;186} else {187logError("Found non-string in array");188}189log(" arg[" + i + "]=" + result[i]);190}191return result;192}193194logError("'Arguments' key in 'Java' sub-dictionary of Info.plist requires a string value or an array of strings");195return new String[0];196}197198// returns name of main class, or null199String getMainFromManifest(final String jarpath) {200JarFile jar = null;201try {202jar = new JarFile(jarpath);203final Manifest man = jar.getManifest();204final Attributes attr = man.getMainAttributes();205return attr.getValue("Main-Class");206} catch (final IOException x) {207// shrug208} finally {209if (jar != null) {210try {211jar.close();212} catch (final IOException x) { }213}214}215return null;216}217218void log(final String s) {219if (!verbose) return;220System.out.println("[LaunchRunner] " + s);221}222223static void logError(final String s) {224System.err.println("[LaunchRunner Error] " + s);225}226227// This kills the app and does not return!228static void showFailureAlertAndKill(final String msg, String arg) {229if (arg == null) arg = "<<null>>";230JOptionPane.showMessageDialog(null, getMessage(msg, arg), "", JOptionPane.ERROR_MESSAGE);231System.exit(-1);232}233234static String getMessage(final String msgKey, final Object ... args) {235final String msg = ResourceBundle.getBundle("appLauncherErrors").getString(msgKey);236return MessageFormat.format(msg, args);237}238}239240241