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