1 /*
   2  * Copyright (c) 2007, 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 package sun.launcher;
  27 
  28 /*
  29  *
  30  *  <p><b>This is NOT part of any API supported by Sun Microsystems.
  31  *  If you write code that depends on this, you do so at your own
  32  *  risk.  This code and its internal interfaces are subject to change
  33  *  or deletion without notice.</b>
  34  *
  35  */
  36 
  37 /**
  38  * A utility package for the java(1), javaw(1) launchers.
  39  * The following are helper methods that the native launcher uses
  40  * to perform checks etc. using JNI, see src/share/bin/java.c
  41  */
  42 import java.io.File;
  43 import java.io.IOException;
  44 import java.io.PrintStream;
  45 import java.io.UnsupportedEncodingException;
  46 import java.lang.reflect.Method;
  47 import java.lang.reflect.Modifier;
  48 import java.math.BigDecimal;
  49 import java.math.RoundingMode;
  50 import java.nio.charset.Charset;
  51 import java.nio.file.DirectoryStream;
  52 import java.nio.file.Files;
  53 import java.nio.file.Path;
  54 import java.util.ResourceBundle;
  55 import java.text.MessageFormat;
  56 import java.util.ArrayList;
  57 import java.util.Collections;
  58 import java.util.Iterator;
  59 import java.util.List;
  60 import java.util.Locale;
  61 import java.util.Locale.Category;
  62 import java.util.Properties;
  63 import java.util.Set;
  64 import java.util.TreeSet;
  65 import java.util.jar.Attributes;
  66 import java.util.jar.JarFile;
  67 import java.util.jar.Manifest;
  68 
  69 public enum LauncherHelper {
  70     INSTANCE;
  71     private static final String MAIN_CLASS = "Main-Class";
  72     private static StringBuilder outBuf = new StringBuilder();
  73 
  74     private static final String INDENT = "    ";
  75     private static final String VM_SETTINGS     = "VM settings:";
  76     private static final String PROP_SETTINGS   = "Property settings:";
  77     private static final String LOCALE_SETTINGS = "Locale settings:";
  78 
  79     // sync with java.c and sun.misc.VM
  80     private static final String diagprop = "sun.java.launcher.diag";
  81     final static boolean trace = sun.misc.VM.getSavedProperty(diagprop) != null;
  82 
  83     private static final String defaultBundleName =
  84             "sun.launcher.resources.launcher";
  85     private static class ResourceBundleHolder {
  86         private static final ResourceBundle RB =
  87                 ResourceBundle.getBundle(defaultBundleName);
  88     }
  89     private static PrintStream ostream;
  90     private static final ClassLoader scloader = ClassLoader.getSystemClassLoader();
  91     private static Class<?> appClass; // application class, for GUI/reporting purposes
  92 
  93     /*
  94      * A method called by the launcher to print out the standard settings,
  95      * by default -XshowSettings is equivalent to -XshowSettings:all,
  96      * Specific information may be gotten by using suboptions with possible
  97      * values vm, properties and locale.
  98      *
  99      * printToStderr: choose between stdout and stderr
 100      *
 101      * optionFlag: specifies which options to print default is all other
 102      *    possible values are vm, properties, locale.
 103      *
 104      * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates
 105      *    this code should determine this value, using a suitable method or
 106      *    the line could be omitted.
 107      *
 108      * maxHeapSize: in bytes, as set by the launcher, a zero-value indicates
 109      *    this code should determine this value, using a suitable method.
 110      *
 111      * stackSize: in bytes, as set by the launcher, a zero-value indicates
 112      *    this code determine this value, using a suitable method or omit the
 113      *    line entirely.
 114      */
 115     static void showSettings(boolean printToStderr, String optionFlag,
 116             long initialHeapSize, long maxHeapSize, long stackSize,
 117             boolean isServer) {
 118 
 119         initOutput(printToStderr);
 120         String opts[] = optionFlag.split(":");
 121         String optStr = (opts.length > 1 && opts[1] != null)
 122                 ? opts[1].trim()
 123                 : "all";
 124         switch (optStr) {
 125             case "vm":
 126                 printVmSettings(initialHeapSize, maxHeapSize,
 127                                 stackSize, isServer);
 128                 break;
 129             case "properties":
 130                 printProperties();
 131                 break;
 132             case "locale":
 133                 printLocale();
 134                 break;
 135             default:
 136                 printVmSettings(initialHeapSize, maxHeapSize, stackSize,
 137                                 isServer);
 138                 printProperties();
 139                 printLocale();
 140                 break;
 141         }
 142     }
 143 
 144     /*
 145      * prints the main vm settings subopt/section
 146      */
 147     private static void printVmSettings(
 148             long initialHeapSize, long maxHeapSize,
 149             long stackSize, boolean isServer) {
 150 
 151         ostream.println(VM_SETTINGS);
 152         if (stackSize != 0L) {
 153             ostream.println(INDENT + "Stack Size: " +
 154                     SizePrefix.scaleValue(stackSize));
 155         }
 156         if (initialHeapSize != 0L) {
 157              ostream.println(INDENT + "Min. Heap Size: " +
 158                     SizePrefix.scaleValue(initialHeapSize));
 159         }
 160         if (maxHeapSize != 0L) {
 161             ostream.println(INDENT + "Max. Heap Size: " +
 162                     SizePrefix.scaleValue(maxHeapSize));
 163         } else {
 164             ostream.println(INDENT + "Max. Heap Size (Estimated): "
 165                     + SizePrefix.scaleValue(Runtime.getRuntime().maxMemory()));
 166         }
 167         ostream.println(INDENT + "Ergonomics Machine Class: "
 168                 + ((isServer) ? "server" : "client"));
 169         ostream.println(INDENT + "Using VM: "
 170                 + System.getProperty("java.vm.name"));
 171         ostream.println();
 172     }
 173 
 174     /*
 175      * prints the properties subopt/section
 176      */
 177     private static void printProperties() {
 178         Properties p = System.getProperties();
 179         ostream.println(PROP_SETTINGS);
 180         List<String> sortedPropertyKeys = new ArrayList<>();
 181         sortedPropertyKeys.addAll(p.stringPropertyNames());
 182         Collections.sort(sortedPropertyKeys);
 183         for (String x : sortedPropertyKeys) {
 184             printPropertyValue(x, p.getProperty(x));
 185         }
 186         ostream.println();
 187     }
 188 
 189     private static boolean isPath(String key) {
 190         return key.endsWith(".dirs") || key.endsWith(".path");
 191     }
 192 
 193     private static void printPropertyValue(String key, String value) {
 194         ostream.print(INDENT + key + " = ");
 195         if (key.equals("line.separator")) {
 196             for (byte b : value.getBytes()) {
 197                 switch (b) {
 198                     case 0xd:
 199                         ostream.print("\\r ");
 200                         break;
 201                     case 0xa:
 202                         ostream.print("\\n ");
 203                         break;
 204                     default:
 205                         // print any bizzare line separators in hex, but really
 206                         // shouldn't happen.
 207                         ostream.printf("0x%02X", b & 0xff);
 208                         break;
 209                 }
 210             }
 211             ostream.println();
 212             return;
 213         }
 214         if (!isPath(key)) {
 215             ostream.println(value);
 216             return;
 217         }
 218         String[] values = value.split(System.getProperty("path.separator"));
 219         boolean first = true;
 220         for (String s : values) {
 221             if (first) { // first line treated specially
 222                 ostream.println(s);
 223                 first = false;
 224             } else { // following lines prefix with indents
 225                 ostream.println(INDENT + INDENT + s);
 226             }
 227         }
 228     }
 229 
 230     /*
 231      * prints the locale subopt/section
 232      */
 233     private static void printLocale() {
 234         Locale locale = Locale.getDefault();
 235         ostream.println(LOCALE_SETTINGS);
 236         ostream.println(INDENT + "default locale = " +
 237                 locale.getDisplayLanguage());
 238         ostream.println(INDENT + "default display locale = " +
 239                 Locale.getDefault(Category.DISPLAY).getDisplayName());
 240         ostream.println(INDENT + "default format locale = " +
 241                 Locale.getDefault(Category.FORMAT).getDisplayName());
 242         printLocales();
 243         ostream.println();
 244     }
 245 
 246     private static void printLocales() {
 247         Locale[] tlocales = Locale.getAvailableLocales();
 248         final int len = tlocales == null ? 0 : tlocales.length;
 249         if (len < 1 ) {
 250             return;
 251         }
 252         // Locale does not implement Comparable so we convert it to String
 253         // and sort it for pretty printing.
 254         Set<String> sortedSet = new TreeSet<>();
 255         for (Locale l : tlocales) {
 256             sortedSet.add(l.toString());
 257         }
 258 
 259         ostream.print(INDENT + "available locales = ");
 260         Iterator<String> iter = sortedSet.iterator();
 261         final int last = len - 1;
 262         for (int i = 0 ; iter.hasNext() ; i++) {
 263             String s = iter.next();
 264             ostream.print(s);
 265             if (i != last) {
 266                 ostream.print(", ");
 267             }
 268             // print columns of 8
 269             if ((i + 1) % 8 == 0) {
 270                 ostream.println();
 271                 ostream.print(INDENT + INDENT);
 272             }
 273         }
 274     }
 275 
 276     private enum SizePrefix {
 277 
 278         KILO(1024, "K"),
 279         MEGA(1024 * 1024, "M"),
 280         GIGA(1024 * 1024 * 1024, "G"),
 281         TERA(1024L * 1024L * 1024L * 1024L, "T");
 282         long size;
 283         String abbrev;
 284 
 285         SizePrefix(long size, String abbrev) {
 286             this.size = size;
 287             this.abbrev = abbrev;
 288         }
 289 
 290         private static String scale(long v, SizePrefix prefix) {
 291             return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size),
 292                     2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev;
 293         }
 294         /*
 295          * scale the incoming values to a human readable form, represented as
 296          * K, M, G and T, see java.c parse_size for the scaled values and
 297          * suffixes. The lowest possible scaled value is Kilo.
 298          */
 299         static String scaleValue(long v) {
 300             if (v < MEGA.size) {
 301                 return scale(v, KILO);
 302             } else if (v < GIGA.size) {
 303                 return scale(v, MEGA);
 304             } else if (v < TERA.size) {
 305                 return scale(v, GIGA);
 306             } else {
 307                 return scale(v, TERA);
 308             }
 309         }
 310     }
 311 
 312     /**
 313      * A private helper method to get a localized message and also
 314      * apply any arguments that we might pass.
 315      */
 316     private static String getLocalizedMessage(String key, Object... args) {
 317         String msg = ResourceBundleHolder.RB.getString(key);
 318         return (args != null) ? MessageFormat.format(msg, args) : msg;
 319     }
 320 
 321     /**
 322      * The java -help message is split into 3 parts, an invariant, followed
 323      * by a set of platform dependent variant messages, finally an invariant
 324      * set of lines.
 325      * This method initializes the help message for the first time, and also
 326      * assembles the invariant header part of the message.
 327      */
 328     static void initHelpMessage(String progname) {
 329         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header",
 330                 (progname == null) ? "java" : progname ));
 331         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel",
 332                 32));
 333         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel",
 334                 64));
 335     }
 336 
 337     /**
 338      * Appends the vm selection messages to the header, already created.
 339      * initHelpSystem must already be called.
 340      */
 341     static void appendVmSelectMessage(String vm1, String vm2) {
 342         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect",
 343                 vm1, vm2));
 344     }
 345 
 346     /**
 347      * Appends the vm synoym message to the header, already created.
 348      * initHelpSystem must be called before using this method.
 349      */
 350     static void appendVmSynonymMessage(String vm1, String vm2) {
 351         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot",
 352                 vm1, vm2));
 353     }
 354 
 355     /**
 356      * Appends the vm Ergo message to the header, already created.
 357      * initHelpSystem must be called before using this method.
 358      */
 359     static void appendVmErgoMessage(boolean isServerClass, String vm) {
 360         outBuf = outBuf.append(getLocalizedMessage("java.launcher.ergo.message1",
 361                 vm));
 362         outBuf = (isServerClass)
 363              ? outBuf.append(",\n" +
 364                 getLocalizedMessage("java.launcher.ergo.message2") + "\n\n")
 365              : outBuf.append(".\n\n");
 366     }
 367 
 368     /**
 369      * Appends the last invariant part to the previously created messages,
 370      * and finishes up the printing to the desired output stream.
 371      * initHelpSystem must be called before using this method.
 372      */
 373     static void printHelpMessage(boolean printToStderr) {
 374         initOutput(printToStderr);
 375         outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer",
 376                 File.pathSeparator));
 377         ostream.println(outBuf.toString());
 378     }
 379 
 380     /**
 381      * Prints the Xusage text to the desired output stream.
 382      */
 383     static void printXUsageMessage(boolean printToStderr) {
 384         initOutput(printToStderr);
 385         ostream.println(getLocalizedMessage("java.launcher.X.usage",
 386                 File.pathSeparator));
 387         if (System.getProperty("os.name").contains("OS X")) {
 388             ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage",
 389                         File.pathSeparator));
 390         }
 391     }
 392 
 393     static void initOutput(boolean printToStderr) {
 394         ostream =  (printToStderr) ? System.err : System.out;
 395     }
 396 
 397     static String getMainClassFromJar(String jarname) {
 398         String mainValue = null;
 399         try (JarFile jarFile = new JarFile(jarname)) {
 400             Manifest manifest = jarFile.getManifest();
 401             if (manifest == null) {
 402                 abort(null, "java.launcher.jar.error2", jarname);
 403             }
 404             Attributes mainAttrs = manifest.getMainAttributes();
 405             if (mainAttrs == null) {
 406                 abort(null, "java.launcher.jar.error3", jarname);
 407             }
 408             mainValue = mainAttrs.getValue(MAIN_CLASS);
 409             if (mainValue == null) {
 410                 abort(null, "java.launcher.jar.error3", jarname);
 411             }
 412             /*
 413              * Hand off to FXHelper if it detects a JavaFX application
 414              * This must be done after ensuring a Main-Class entry
 415              * exists to enforce compliance with the jar specification
 416              */
 417             if (mainAttrs.containsKey(
 418                     new Attributes.Name(FXHelper.JAVAFX_APPLICATION_MARKER))) {
 419                 return FXHelper.class.getName();
 420             }
 421             return mainValue.trim();
 422         } catch (IOException ioe) {
 423             abort(ioe, "java.launcher.jar.error1", jarname);
 424         }
 425         return null;
 426     }
 427 
 428     // From src/share/bin/java.c:
 429     //   enum LaunchMode { LM_UNKNOWN = 0, LM_CLASS, LM_JAR };
 430 
 431     private static final int LM_UNKNOWN = 0;
 432     private static final int LM_CLASS   = 1;
 433     private static final int LM_JAR     = 2;
 434 
 435     static void abort(Throwable t, String msgKey, Object... args) {
 436         if (msgKey != null) {
 437             ostream.println(getLocalizedMessage(msgKey, args));
 438         }
 439         if (trace) {
 440             if (t != null) {
 441                 t.printStackTrace();
 442             } else {
 443                 Thread.dumpStack();
 444             }
 445         }
 446         System.exit(1);
 447     }
 448 
 449     /**
 450      * This method does the following:
 451      * 1. gets the classname from a Jar's manifest, if necessary
 452      * 2. loads the class using the System ClassLoader
 453      * 3. ensures the availability and accessibility of the main method,
 454      *    using signatureDiagnostic method.
 455      *    a. does the class exist
 456      *    b. is there a main
 457      *    c. is the main public
 458      *    d. is the main static
 459      *    e. does the main take a String array for args
 460      * 4. if no main method and if the class extends FX Application, then call
 461      *    on FXHelper to determine the main class to launch
 462      * 5. and off we go......
 463      *
 464      * @param printToStderr if set, all output will be routed to stderr
 465      * @param mode LaunchMode as determined by the arguments passed on the
 466      * command line
 467      * @param what either the jar file to launch or the main class when using
 468      * LM_CLASS mode
 469      * @return the application's main class
 470      */
 471     public static Class<?> checkAndLoadMain(boolean printToStderr,
 472                                             int mode,
 473                                             String what) {
 474         initOutput(printToStderr);
 475         // get the class name
 476         String cn = null;
 477         switch (mode) {
 478             case LM_CLASS:
 479                 cn = what;
 480                 break;
 481             case LM_JAR:
 482                 cn = getMainClassFromJar(what);
 483                 break;
 484             default:
 485                 // should never happen
 486                 throw new InternalError("" + mode + ": Unknown launch mode");
 487         }
 488         cn = cn.replace('/', '.');
 489         Class<?> mainClass = null;
 490         try {
 491             mainClass = scloader.loadClass(cn);
 492         } catch (NoClassDefFoundError | ClassNotFoundException cnfe) {
 493             abort(cnfe, "java.launcher.cls.error1", cn);
 494         }
 495         // set to mainClass
 496         appClass = mainClass;
 497 
 498         /*
 499          * Check if FXHelper can launch it using the FX launcher. In an FX app,
 500          * the main class may or may not have a main method, so do this before
 501          * validating the main class.
 502          */
 503         if (mainClass.equals(FXHelper.class) ||
 504                 FXHelper.doesExtendFXApplication(mainClass)) {
 505             // Will abort() if there are problems with the FX runtime
 506             FXHelper.setFXLaunchParameters(what, mode);
 507             return FXHelper.class;
 508         }
 509 
 510         validateMainClass(mainClass);
 511         return mainClass;
 512     }
 513 
 514     /*
 515      * Accessor method called by the launcher after getting the main class via
 516      * checkAndLoadMain(). The "application class" is the class that is finally
 517      * executed to start the application and in this case is used to report
 518      * the correct application name, typically for UI purposes.
 519      */
 520     public static Class<?> getApplicationClass() {
 521         return appClass;
 522     }
 523 
 524     // Check the existence and signature of main and abort if incorrect
 525     static void validateMainClass(Class<?> mainClass) {
 526         Method mainMethod;
 527         try {
 528             mainMethod = mainClass.getMethod("main", String[].class);
 529         } catch (NoSuchMethodException nsme) {
 530             // invalid main or not FX application, abort with an error
 531             abort(null, "java.launcher.cls.error4", mainClass.getName(),
 532                   FXHelper.JAVAFX_APPLICATION_CLASS_NAME);
 533             return; // Avoid compiler issues
 534         }
 535 
 536         /*
 537          * getMethod (above) will choose the correct method, based
 538          * on its name and parameter type, however, we still have to
 539          * ensure that the method is static and returns a void.
 540          */
 541         int mod = mainMethod.getModifiers();
 542         if (!Modifier.isStatic(mod)) {
 543             abort(null, "java.launcher.cls.error2", "static",
 544                   mainMethod.getDeclaringClass().getName());
 545         }
 546         if (mainMethod.getReturnType() != java.lang.Void.TYPE) {
 547             abort(null, "java.launcher.cls.error3",
 548                   mainMethod.getDeclaringClass().getName());
 549         }
 550     }
 551 
 552     private static final String encprop = "sun.jnu.encoding";
 553     private static String encoding = null;
 554     private static boolean isCharsetSupported = false;
 555 
 556     /*
 557      * converts a c or a byte array to a platform specific string,
 558      * previously implemented as a native method in the launcher.
 559      */
 560     static String makePlatformString(boolean printToStderr, byte[] inArray) {
 561         initOutput(printToStderr);
 562         if (encoding == null) {
 563             encoding = System.getProperty(encprop);
 564             isCharsetSupported = Charset.isSupported(encoding);
 565         }
 566         try {
 567             String out = isCharsetSupported
 568                     ? new String(inArray, encoding)
 569                     : new String(inArray);
 570             return out;
 571         } catch (UnsupportedEncodingException uee) {
 572             abort(uee, null);
 573         }
 574         return null; // keep the compiler happy
 575     }
 576 
 577     static String[] expandArgs(String[] argArray) {
 578         List<StdArg> aList = new ArrayList<>();
 579         for (String x : argArray) {
 580             aList.add(new StdArg(x));
 581         }
 582         return expandArgs(aList);
 583     }
 584 
 585     static String[] expandArgs(List<StdArg> argList) {
 586         ArrayList<String> out = new ArrayList<>();
 587         if (trace) {
 588             System.err.println("Incoming arguments:");
 589         }
 590         for (StdArg a : argList) {
 591             if (trace) {
 592                 System.err.println(a);
 593             }
 594             if (a.needsExpansion) {
 595                 File x = new File(a.arg);
 596                 File parent = x.getParentFile();
 597                 String glob = x.getName();
 598                 if (parent == null) {
 599                     parent = new File(".");
 600                 }
 601                 try (DirectoryStream<Path> dstream =
 602                         Files.newDirectoryStream(parent.toPath(), glob)) {
 603                     int entries = 0;
 604                     for (Path p : dstream) {
 605                         out.add(p.normalize().toString());
 606                         entries++;
 607                     }
 608                     if (entries == 0) {
 609                         out.add(a.arg);
 610                     }
 611                 } catch (Exception e) {
 612                     out.add(a.arg);
 613                     if (trace) {
 614                         System.err.println("Warning: passing argument as-is " + a);
 615                         System.err.print(e);
 616                     }
 617                 }
 618             } else {
 619                 out.add(a.arg);
 620             }
 621         }
 622         String[] oarray = new String[out.size()];
 623         out.toArray(oarray);
 624 
 625         if (trace) {
 626             System.err.println("Expanded arguments:");
 627             for (String x : oarray) {
 628                 System.err.println(x);
 629             }
 630         }
 631         return oarray;
 632     }
 633 
 634     /* duplicate of the native StdArg struct */
 635     private static class StdArg {
 636         final String arg;
 637         final boolean needsExpansion;
 638         StdArg(String arg, boolean expand) {
 639             this.arg = arg;
 640             this.needsExpansion = expand;
 641         }
 642         // protocol: first char indicates whether expansion is required
 643         // 'T' = true ; needs expansion
 644         // 'F' = false; needs no expansion
 645         StdArg(String in) {
 646             this.arg = in.substring(1);
 647             needsExpansion = in.charAt(0) == 'T';
 648         }
 649         public String toString() {
 650             return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}';
 651         }
 652     }
 653 
 654     static final class FXHelper {
 655         // Marker entry in jar manifest that designates a JavaFX application jar
 656         private static final String JAVAFX_APPLICATION_MARKER =
 657                 "JavaFX-Application-Class";
 658         private static final String JAVAFX_APPLICATION_CLASS_NAME =
 659                 "javafx.application.Application";
 660         private static final String JAVAFX_LAUNCHER_CLASS_NAME =
 661                 "com.sun.javafx.application.LauncherImpl";
 662 
 663         /*
 664          * The launch method used to invoke the JavaFX launcher. These must
 665          * match the strings used in the launchApplication method.
 666          *
 667          * Command line                 JavaFX-App-Class  Launch mode  FX Launch mode
 668          * java -cp fxapp.jar FXClass   N/A               LM_CLASS     "LM_CLASS"
 669          * java -cp somedir FXClass     N/A               LM_CLASS     "LM_CLASS"
 670          * java -jar fxapp.jar          Present           LM_JAR       "LM_JAR"
 671          * java -jar fxapp.jar          Not Present       LM_JAR       "LM_JAR"
 672          */
 673         private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS";
 674         private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR";
 675 
 676         /*
 677          * FX application launcher and launch method, so we can launch
 678          * applications with no main method.
 679          */
 680         private static String fxLaunchName = null;
 681         private static String fxLaunchMode = null;
 682 
 683         private static Class<?> fxLauncherClass    = null;
 684         private static Method   fxLauncherMethod   = null;
 685 
 686         /*
 687          * Set the launch params according to what was passed to LauncherHelper
 688          * so we can use the same launch mode for FX. Abort if there is any
 689          * issue with loading the FX runtime or with the launcher method.
 690          */
 691         private static void setFXLaunchParameters(String what, int mode) {
 692             // Check for the FX launcher classes
 693             try {
 694                 fxLauncherClass = scloader.loadClass(JAVAFX_LAUNCHER_CLASS_NAME);
 695                 /*
 696                  * signature must be:
 697                  * public static void launchApplication(String launchName,
 698                  *     String launchMode, String[] args);
 699                  */
 700                 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication",
 701                         String.class, String.class, String[].class);
 702 
 703                 // verify launcher signature as we do when validating the main method
 704                 int mod = fxLauncherMethod.getModifiers();
 705                 if (!Modifier.isStatic(mod)) {
 706                     abort(null, "java.launcher.javafx.error1");
 707                 }
 708                 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) {
 709                     abort(null, "java.launcher.javafx.error1");
 710                 }
 711             } catch (ClassNotFoundException | NoSuchMethodException ex) {
 712                 abort(ex, "java.launcher.cls.error5", ex);
 713             }
 714 
 715             fxLaunchName = what;
 716             switch (mode) {
 717                 case LM_CLASS:
 718                     fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS;
 719                     break;
 720                 case LM_JAR:
 721                     fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR;
 722                     break;
 723                 default:
 724                     // should not have gotten this far...
 725                     throw new InternalError(mode + ": Unknown launch mode");
 726             }
 727         }
 728 
 729         /*
 730          * Check if the given class is a JavaFX Application class. This is done
 731          * in a way that does not cause the Application class to load or throw
 732          * ClassNotFoundException if the JavaFX runtime is not available.
 733          */
 734         private static boolean doesExtendFXApplication(Class<?> mainClass) {
 735             for (Class<?> sc = mainClass.getSuperclass(); sc != null;
 736                     sc = sc.getSuperclass()) {
 737                 if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
 738                     return true;
 739                 }
 740             }
 741             return false;
 742         }
 743 
 744         public static void main(String... args) throws Exception {
 745             if (fxLauncherMethod == null
 746                     || fxLaunchMode == null
 747                     || fxLaunchName == null) {
 748                 throw new RuntimeException("Invalid JavaFX launch parameters");
 749             }
 750             // launch appClass via fxLauncherMethod
 751             fxLauncherMethod.invoke(null,
 752                     new Object[] {fxLaunchName, fxLaunchMode, args});
 753         }
 754     }
 755 }