1 /*
   2  * Copyright (c) 2007, 2012, 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             // if this is not a full JRE then the Profile attribute must be
 418             // present with the Main-Class attribute so as to indicate the minimum
 419             // profile required. Note that we need to suppress checking of the Profile
 420             // attribute after we detect an error. This is because the abort may
 421             // need to lookup resources and this may involve opening additional JAR
 422             // files that would result in errors that suppress the main error.
 423             String profile = mainAttrs.getValue(PROFILE);
 424             if (profile == null) {
 425                 if (!Version.isFullJre()) {
 426                     URLClassPath.suppressProfileCheckForLauncher();
 427                     abort(null, "java.launcher.jar.error4", jarname);
 428                 }
 429             } else {
 430                 if (!Version.supportsProfile(profile)) {
 431                     URLClassPath.suppressProfileCheckForLauncher();
 432                     abort(null, "java.launcher.jar.error5", profile, jarname);
 433                 }
 434             }
 435             return mainValue.trim();
 436         } catch (IOException ioe) {
 437             abort(ioe, "java.launcher.jar.error1", jarname);
 438         }
 439         return null;
 440     }
 441 
 442     // From src/share/bin/java.c:
 443     //   enum LaunchMode { LM_UNKNOWN = 0, LM_CLASS, LM_JAR };
 444 
 445     private static final int LM_UNKNOWN = 0;
 446     private static final int LM_CLASS   = 1;
 447     private static final int LM_JAR     = 2;
 448 
 449     static void abort(Throwable t, String msgKey, Object... args) {
 450         if (msgKey != null) {
 451             ostream.println(getLocalizedMessage(msgKey, args));
 452         }
 453         if (trace) {
 454             if (t != null) {
 455                 t.printStackTrace();
 456             } else {
 457                 Thread.dumpStack();
 458             }
 459         }
 460         System.exit(1);
 461     }
 462 
 463     /**
 464      * This method does the following:
 465      * 1. gets the classname from a Jar's manifest, if necessary
 466      * 2. loads the class using the System ClassLoader
 467      * 3. ensures the availability and accessibility of the main method,
 468      *    using signatureDiagnostic method.
 469      *    a. does the class exist
 470      *    b. is there a main
 471      *    c. is the main public
 472      *    d. is the main static
 473      *    e. does the main take a String array for args
 474      * 4. if no main method and if the class extends FX Application, then call
 475      *    on FXHelper to determine the main class to launch
 476      * 5. and off we go......
 477      *
 478      * @param printToStderr if set, all output will be routed to stderr
 479      * @param mode LaunchMode as determined by the arguments passed on the
 480      * command line
 481      * @param what either the jar file to launch or the main class when using
 482      * LM_CLASS mode
 483      * @return the application's main class
 484      */
 485     public static Class<?> checkAndLoadMain(boolean printToStderr,
 486                                             int mode,
 487                                             String what) {
 488         initOutput(printToStderr);
 489         // get the class name
 490         String cn = null;
 491         switch (mode) {
 492             case LM_CLASS:
 493                 cn = what;
 494                 break;
 495             case LM_JAR:
 496                 cn = getMainClassFromJar(what);
 497                 break;
 498             default:
 499                 // should never happen
 500                 throw new InternalError("" + mode + ": Unknown launch mode");
 501         }
 502         cn = cn.replace('/', '.');
 503         Class<?> mainClass = null;
 504         try {
 505             mainClass = scloader.loadClass(cn);
 506         } catch (NoClassDefFoundError | ClassNotFoundException cnfe) {
 507             abort(cnfe, "java.launcher.cls.error1", cn);
 508         }
 509         // set to mainClass, FXHelper may return something else
 510         appClass = mainClass;
 511 
 512         Method m = getMainMethod(mainClass);
 513         if (m != null) {
 514             // this will abort if main method has the wrong signature
 515             validateMainMethod(m);
 516             return mainClass;
 517         }
 518 
 519         // Check if FXHelper can launch it using the FX launcher
 520         Class<?> fxClass = FXHelper.getFXMainClass(mainClass);
 521         if (fxClass != null) {
 522             return fxClass;
 523         }
 524 
 525         // not an FX application either, abort with an error
 526         abort(null, "java.launcher.cls.error4", mainClass.getName(),
 527               FXHelper.JAVAFX_APPLICATION_CLASS_NAME);
 528         return null; // avoid compiler error...
 529     }
 530 
 531     /*
 532      * Accessor method called by the launcher after getting the main class via
 533      * checkAndLoadMain(). The "application class" is the class that is finally
 534      * executed to start the application and in this case is used to report
 535      * the correct application name, typically for UI purposes.
 536      */
 537     public static Class<?> getApplicationClass() {
 538         return appClass;
 539     }
 540 
 541     // Check for main method or return null if not found
 542     static Method getMainMethod(Class<?> clazz) {
 543         try {
 544             return clazz.getMethod("main", String[].class);
 545         } catch (NoSuchMethodException nsme) {}
 546         return null;
 547     }
 548 
 549     // Check the signature of main and abort if it's incorrect
 550     static void validateMainMethod(Method mainMethod) {
 551         /*
 552          * getMethod (above) will choose the correct method, based
 553          * on its name and parameter type, however, we still have to
 554          * ensure that the method is static and returns a void.
 555          */
 556         int mod = mainMethod.getModifiers();
 557         if (!Modifier.isStatic(mod)) {
 558             abort(null, "java.launcher.cls.error2", "static",
 559                   mainMethod.getDeclaringClass().getName());
 560         }
 561         if (mainMethod.getReturnType() != java.lang.Void.TYPE) {
 562             abort(null, "java.launcher.cls.error3",
 563                   mainMethod.getDeclaringClass().getName());
 564         }
 565     }
 566 
 567     private static final String encprop = "sun.jnu.encoding";
 568     private static String encoding = null;
 569     private static boolean isCharsetSupported = false;
 570 
 571     /*
 572      * converts a c or a byte array to a platform specific string,
 573      * previously implemented as a native method in the launcher.
 574      */
 575     static String makePlatformString(boolean printToStderr, byte[] inArray) {
 576         initOutput(printToStderr);
 577         if (encoding == null) {
 578             encoding = System.getProperty(encprop);
 579             isCharsetSupported = Charset.isSupported(encoding);
 580         }
 581         try {
 582             String out = isCharsetSupported
 583                     ? new String(inArray, encoding)
 584                     : new String(inArray);
 585             return out;
 586         } catch (UnsupportedEncodingException uee) {
 587             abort(uee, null);
 588         }
 589         return null; // keep the compiler happy
 590     }
 591 
 592     static String[] expandArgs(String[] argArray) {
 593         List<StdArg> aList = new ArrayList<>();
 594         for (String x : argArray) {
 595             aList.add(new StdArg(x));
 596         }
 597         return expandArgs(aList);
 598     }
 599 
 600     static String[] expandArgs(List<StdArg> argList) {
 601         ArrayList<String> out = new ArrayList<>();
 602         if (trace) {
 603             System.err.println("Incoming arguments:");
 604         }
 605         for (StdArg a : argList) {
 606             if (trace) {
 607                 System.err.println(a);
 608             }
 609             if (a.needsExpansion) {
 610                 File x = new File(a.arg);
 611                 File parent = x.getParentFile();
 612                 String glob = x.getName();
 613                 if (parent == null) {
 614                     parent = new File(".");
 615                 }
 616                 try (DirectoryStream<Path> dstream =
 617                         Files.newDirectoryStream(parent.toPath(), glob)) {
 618                     int entries = 0;
 619                     for (Path p : dstream) {
 620                         out.add(p.normalize().toString());
 621                         entries++;
 622                     }
 623                     if (entries == 0) {
 624                         out.add(a.arg);
 625                     }
 626                 } catch (Exception e) {
 627                     out.add(a.arg);
 628                     if (trace) {
 629                         System.err.println("Warning: passing argument as-is " + a);
 630                         System.err.print(e);
 631                     }
 632                 }
 633             } else {
 634                 out.add(a.arg);
 635             }
 636         }
 637         String[] oarray = new String[out.size()];
 638         out.toArray(oarray);
 639 
 640         if (trace) {
 641             System.err.println("Expanded arguments:");
 642             for (String x : oarray) {
 643                 System.err.println(x);
 644             }
 645         }
 646         return oarray;
 647     }
 648 
 649     /* duplicate of the native StdArg struct */
 650     private static class StdArg {
 651         final String arg;
 652         final boolean needsExpansion;
 653         StdArg(String arg, boolean expand) {
 654             this.arg = arg;
 655             this.needsExpansion = expand;
 656         }
 657         // protocol: first char indicates whether expansion is required
 658         // 'T' = true ; needs expansion
 659         // 'F' = false; needs no expansion
 660         StdArg(String in) {
 661             this.arg = in.substring(1);
 662             needsExpansion = in.charAt(0) == 'T';
 663         }
 664         public String toString() {
 665             return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}';
 666         }
 667     }
 668 
 669     static final class FXHelper {
 670         private static final String JAVAFX_APPLICATION_CLASS_NAME =
 671                 "javafx.application.Application";
 672         private static final String JAVAFX_LAUNCHER_CLASS_NAME =
 673                 "com.sun.javafx.application.LauncherImpl";
 674 
 675         /*
 676          * FX application launcher and launch method, so we can launch
 677          * applications with no main method.
 678          */
 679         private static Class<?> fxLauncherClass    = null;
 680         private static Method   fxLauncherMethod   = null;
 681 
 682         /*
 683          * We can assume that the class does NOT have a main method or it would
 684          * have been handled already. We do, however, need to check if the class
 685          * extends Application and the launcher is available and abort with an
 686          * error if it's not.
 687          */
 688         private static Class<?> getFXMainClass(Class<?> mainClass) {
 689             // Check if mainClass extends Application
 690             if (!doesExtendFXApplication(mainClass)) {
 691                 return null;
 692             }
 693 
 694             // Check for the FX launcher classes
 695             try {
 696                 fxLauncherClass = scloader.loadClass(JAVAFX_LAUNCHER_CLASS_NAME);
 697                 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication",
 698                         Class.class, String[].class);
 699             } catch (ClassNotFoundException | NoSuchMethodException ex) {
 700                 abort(ex, "java.launcher.cls.error5", ex);
 701             }
 702 
 703             // That's all, return this class so we can launch later
 704             return FXHelper.class;
 705         }
 706 
 707         /*
 708          * Check if the given class is a JavaFX Application class. This is done
 709          * in a way that does not cause the Application class to load or throw
 710          * ClassNotFoundException if the JavaFX runtime is not available.
 711          */
 712         private static boolean doesExtendFXApplication(Class<?> mainClass) {
 713             for (Class<?> sc = mainClass.getSuperclass(); sc != null;
 714                     sc = sc.getSuperclass()) {
 715                 if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
 716                     return true;
 717                 }
 718             }
 719             return false;
 720         }
 721 
 722         // preloader ?
 723         public static void main(String... args) throws Exception {
 724             // launch appClass via fxLauncherMethod
 725             fxLauncherMethod.invoke(null, new Object[] {appClass, args});
 726         }
 727     }
 728 }