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 = scl.loadClass(cn);
 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 given as decomposed it
 593                     // is possible that main class name comes incorrectly from the command line
 594                     // and we have to re-compose it
 595                     mainClass = scl.loadClass(Normalizer.normalize(cn, Normalizer.Form.NFC));
 596                 } catch (NoClassDefFoundError | ClassNotFoundException cnfe1) {
 597                     abort(cnfe, "java.launcher.cls.error1", cn);
 598                 }
 599             } else {
 600                 abort(cnfe, "java.launcher.cls.error1", cn);
 601             }
 602         }
 603 
 604         // record the main class
 605         appClass = mainClass;
 606 
 607         /*
 608          * Check if FXHelper can launch it using the FX launcher. In an FX app,
 609          * the main class may or may not have a main method, so do this before
 610          * validating the main class.
 611          */
 612         if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) ||
 613             doesExtendFXApplication(mainClass)) {
 614             // Will abort() if there are problems with FX runtime
 615             FXHelper.setFXLaunchParameters(what, mode);
 616             return FXHelper.class;
 617         }
 618         return mainClass;
 619     }
 620 
 621     /*
 622      * Accessor method called by the launcher after getting the main class via
 623      * checkAndLoadMain(). The "application class" is the class that is finally
 624      * executed to start the application and in this case is used to report
 625      * the correct application name, typically for UI purposes.
 626      */
 627     public static Class<?> getApplicationClass() {
 628         return appClass;
 629     }
 630 
 631     /*
 632      * Check if the given class is a JavaFX Application class. This is done
 633      * in a way that does not cause the Application class to load or throw
 634      * ClassNotFoundException if the JavaFX runtime is not available.
 635      */
 636     private static boolean doesExtendFXApplication(Class<?> mainClass) {
 637         for (Class<?> sc = mainClass.getSuperclass(); sc != null;
 638                 sc = sc.getSuperclass()) {
 639             if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) {
 640                 return true;
 641             }
 642         }
 643         return false;
 644     }
 645 
 646     // Check the existence and signature of main and abort if incorrect
 647     static void validateMainClass(Class<?> mainClass) {
 648         Method mainMethod;
 649         try {
 650             mainMethod = mainClass.getMethod("main", String[].class);
 651         } catch (NoSuchMethodException nsme) {
 652             // invalid main or not FX application, abort with an error
 653             abort(null, "java.launcher.cls.error4", mainClass.getName(),
 654                   JAVAFX_APPLICATION_CLASS_NAME);
 655             return; // Avoid compiler issues
 656         }
 657 
 658         /*
 659          * getMethod (above) will choose the correct method, based
 660          * on its name and parameter type, however, we still have to
 661          * ensure that the method is static and returns a void.
 662          */
 663         int mod = mainMethod.getModifiers();
 664         if (!Modifier.isStatic(mod)) {
 665             abort(null, "java.launcher.cls.error2", "static",
 666                   mainMethod.getDeclaringClass().getName());
 667         }
 668         if (mainMethod.getReturnType() != java.lang.Void.TYPE) {
 669             abort(null, "java.launcher.cls.error3",
 670                   mainMethod.getDeclaringClass().getName());
 671         }
 672     }
 673 
 674     private static final String encprop = "sun.jnu.encoding";
 675     private static String encoding = null;
 676     private static boolean isCharsetSupported = false;
 677 
 678     /*
 679      * converts a c or a byte array to a platform specific string,
 680      * previously implemented as a native method in the launcher.
 681      */
 682     static String makePlatformString(boolean printToStderr, byte[] inArray) {
 683         initOutput(printToStderr);
 684         if (encoding == null) {
 685             encoding = System.getProperty(encprop);
 686             isCharsetSupported = Charset.isSupported(encoding);
 687         }
 688         try {
 689             String out = isCharsetSupported
 690                     ? new String(inArray, encoding)
 691                     : new String(inArray);
 692             return out;
 693         } catch (UnsupportedEncodingException uee) {
 694             abort(uee, null);
 695         }
 696         return null; // keep the compiler happy
 697     }
 698 
 699     static String[] expandArgs(String[] argArray) {
 700         List<StdArg> aList = new ArrayList<>();
 701         for (String x : argArray) {
 702             aList.add(new StdArg(x));
 703         }
 704         return expandArgs(aList);
 705     }
 706 
 707     static String[] expandArgs(List<StdArg> argList) {
 708         ArrayList<String> out = new ArrayList<>();
 709         if (trace) {
 710             System.err.println("Incoming arguments:");
 711         }
 712         for (StdArg a : argList) {
 713             if (trace) {
 714                 System.err.println(a);
 715             }
 716             if (a.needsExpansion) {
 717                 File x = new File(a.arg);
 718                 File parent = x.getParentFile();
 719                 String glob = x.getName();
 720                 if (parent == null) {
 721                     parent = new File(".");
 722                 }
 723                 try (DirectoryStream<Path> dstream =
 724                         Files.newDirectoryStream(parent.toPath(), glob)) {
 725                     int entries = 0;
 726                     for (Path p : dstream) {
 727                         out.add(p.normalize().toString());
 728                         entries++;
 729                     }
 730                     if (entries == 0) {
 731                         out.add(a.arg);
 732                     }
 733                 } catch (Exception e) {
 734                     out.add(a.arg);
 735                     if (trace) {
 736                         System.err.println("Warning: passing argument as-is " + a);
 737                         System.err.print(e);
 738                     }
 739                 }
 740             } else {
 741                 out.add(a.arg);
 742             }
 743         }
 744         String[] oarray = new String[out.size()];
 745         out.toArray(oarray);
 746 
 747         if (trace) {
 748             System.err.println("Expanded arguments:");
 749             for (String x : oarray) {
 750                 System.err.println(x);
 751             }
 752         }
 753         return oarray;
 754     }
 755 
 756     /* duplicate of the native StdArg struct */
 757     private static class StdArg {
 758         final String arg;
 759         final boolean needsExpansion;
 760         StdArg(String arg, boolean expand) {
 761             this.arg = arg;
 762             this.needsExpansion = expand;
 763         }
 764         // protocol: first char indicates whether expansion is required
 765         // 'T' = true ; needs expansion
 766         // 'F' = false; needs no expansion
 767         StdArg(String in) {
 768             this.arg = in.substring(1);
 769             needsExpansion = in.charAt(0) == 'T';
 770         }
 771         public String toString() {
 772             return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}';
 773         }
 774     }
 775 
 776     static final class FXHelper {
 777 
 778         private static final String JAVAFX_GRAPHICS_MODULE_NAME =
 779                 "javafx.graphics";
 780 
 781         private static final String JAVAFX_LAUNCHER_CLASS_NAME =
 782                 "com.sun.javafx.application.LauncherImpl";
 783 
 784         /*
 785          * The launch method used to invoke the JavaFX launcher. These must
 786          * match the strings used in the launchApplication method.
 787          *
 788          * Command line                 JavaFX-App-Class  Launch mode  FX Launch mode
 789          * java -cp fxapp.jar FXClass   N/A               LM_CLASS     "LM_CLASS"
 790          * java -cp somedir FXClass     N/A               LM_CLASS     "LM_CLASS"
 791          * java -jar fxapp.jar          Present           LM_JAR       "LM_JAR"
 792          * java -jar fxapp.jar          Not Present       LM_JAR       "LM_JAR"
 793          */
 794         private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS";
 795         private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR";
 796 
 797         /*
 798          * FX application launcher and launch method, so we can launch
 799          * applications with no main method.
 800          */
 801         private static String fxLaunchName = null;
 802         private static String fxLaunchMode = null;
 803 
 804         private static Class<?> fxLauncherClass    = null;
 805         private static Method   fxLauncherMethod   = null;
 806 
 807         /*
 808          * Set the launch params according to what was passed to LauncherHelper
 809          * so we can use the same launch mode for FX. Abort if there is any
 810          * issue with loading the FX runtime or with the launcher method.
 811          */
 812         private static void setFXLaunchParameters(String what, int mode) {
 813 
 814             // find the module with the FX launcher
 815             Optional<Module> om = Layer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME);
 816             if (!om.isPresent()) {
 817                 abort(null, "java.launcher.cls.error5");
 818             }
 819 
 820             // load the FX launcher class
 821             fxLauncherClass = Class.forName(om.get(), JAVAFX_LAUNCHER_CLASS_NAME);
 822             if (fxLauncherClass == null) {
 823                 abort(null, "java.launcher.cls.error5");
 824             }
 825 
 826             try {
 827                 /*
 828                  * signature must be:
 829                  * public static void launchApplication(String launchName,
 830                  *     String launchMode, String[] args);
 831                  */
 832                 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication",
 833                         String.class, String.class, String[].class);
 834 
 835                 // verify launcher signature as we do when validating the main method
 836                 int mod = fxLauncherMethod.getModifiers();
 837                 if (!Modifier.isStatic(mod)) {
 838                     abort(null, "java.launcher.javafx.error1");
 839                 }
 840                 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) {
 841                     abort(null, "java.launcher.javafx.error1");
 842                 }
 843             } catch (NoSuchMethodException ex) {
 844                 abort(ex, "java.launcher.cls.error5", ex);
 845             }
 846 
 847             fxLaunchName = what;
 848             switch (mode) {
 849                 case LM_CLASS:
 850                     fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS;
 851                     break;
 852                 case LM_JAR:
 853                     fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR;
 854                     break;
 855                 default:
 856                     // should not have gotten this far...
 857                     throw new InternalError(mode + ": Unknown launch mode");
 858             }
 859         }
 860 
 861         public static void main(String... args) throws Exception {
 862             if (fxLauncherMethod == null
 863                     || fxLaunchMode == null
 864                     || fxLaunchName == null) {
 865                 throw new RuntimeException("Invalid JavaFX launch parameters");
 866             }
 867             // launch appClass via fxLauncherMethod
 868             fxLauncherMethod.invoke(null,
 869                     new Object[] {fxLaunchName, fxLaunchMode, args});
 870         }
 871     }
 872 
 873     private static void formatCommaList(PrintStream out,
 874                                         String prefix,
 875                                         Collection<?> list)
 876     {
 877         if (list.isEmpty())
 878             return;
 879         out.format("%s", prefix);
 880         boolean first = true;
 881         for (Object ob : list) {
 882             if (first) {
 883                 out.format(" %s", ob);
 884                 first = false;
 885             } else {
 886                 out.format(", %s", ob);
 887             }
 888         }
 889         out.format("%n");
 890     }
 891 
 892     /**
 893      * Called by the launcher to list the observable modules.
 894      * If called without any sub-options then the output is a simple list of
 895      * the modules. If called with sub-options then the sub-options are the
 896      * names of the modules to list (-listmods:java.base,java.desktop for
 897      * example).
 898      */
 899     static void listModules(boolean printToStderr, String optionFlag)
 900         throws IOException, ClassNotFoundException
 901     {
 902         initOutput(printToStderr);
 903 
 904         ModuleFinder finder = jdk.internal.module.ModuleBootstrap.finder();
 905 
 906         int colon = optionFlag.indexOf(':');
 907         if (colon == -1) {
 908             finder.findAll().stream()
 909                 .sorted(Comparator.comparing(ModuleReference::descriptor))
 910                 .forEach(md -> {
 911                     ostream.println(midAndLocation(md.descriptor(),
 912                                                    md.location()));
 913                 });
 914         } else {
 915             String[] names = optionFlag.substring(colon+1).split(",");
 916             for (String name: names) {
 917                 ModuleReference mref = finder.find(name).orElse(null);
 918                 if (mref == null) {
 919                     // not found
 920                     continue;
 921                 }
 922 
 923                 ModuleDescriptor md = mref.descriptor();
 924                 ostream.println(midAndLocation(md, mref.location()));
 925 
 926                 for (Requires d : md.requires()) {
 927                     ostream.format("  requires %s%n", d);
 928                 }
 929                 for (String s : md.uses()) {
 930                     ostream.format("  uses %s%n", s);
 931                 }
 932 
 933                 // sorted exports
 934                 Set<Exports> exports = new TreeSet<>(Comparator.comparing(Exports::source));
 935                 exports.addAll(md.exports());
 936                 for (Exports e : exports) {
 937                     ostream.format("  exports %s", e.source());
 938                     if (e.isQualified()) {
 939                         formatCommaList(ostream, " to", e.targets());
 940                     } else {
 941                         ostream.println();
 942                     }
 943                 }
 944 
 945                 // concealed packages
 946                 new TreeSet<>(md.conceals())
 947                     .forEach(p -> ostream.format("  conceals %s%n", p));
 948 
 949                 Map<String, Provides> provides = md.provides();
 950                 for (Provides ps : provides.values()) {
 951                     for (String impl : ps.providers())
 952                         ostream.format("  provides %s with %s%n", ps.service(), impl);
 953                 }
 954             }
 955         }
 956     }
 957 
 958     static String midAndLocation(ModuleDescriptor md, Optional<URI> location ) {
 959         URI loc = location.orElse(null);
 960         if (loc == null || loc.getScheme().equalsIgnoreCase("jrt"))
 961             return md.toNameAndVersion();
 962         else
 963             return md.toNameAndVersion() + " (" + loc + ")";
 964     }
 965 }