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