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