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