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