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