1 /* 2 * Copyright (c) 2007, 2018, 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.Configuration; 47 import java.lang.module.FindException; 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.module.ModuleFinder; 54 import java.lang.module.ModuleReference; 55 import java.lang.module.ResolvedModule; 56 import java.lang.reflect.InvocationTargetException; 57 import java.lang.reflect.Method; 58 import java.lang.reflect.Modifier; 59 import java.math.BigDecimal; 60 import java.math.RoundingMode; 61 import java.net.URI; 62 import java.nio.charset.Charset; 63 import java.nio.file.DirectoryStream; 64 import java.nio.file.Files; 65 import java.nio.file.NoSuchFileException; 66 import java.nio.file.Path; 67 import java.nio.file.Paths; 68 import java.nio.file.attribute.BasicFileAttributes; 69 import java.text.Normalizer; 70 import java.text.MessageFormat; 71 import java.util.ArrayList; 72 import java.util.Collections; 73 import java.util.Comparator; 74 import java.util.HashMap; 75 import java.util.Iterator; 76 import java.util.List; 77 import java.util.Locale; 78 import java.util.Locale.Category; 79 import java.util.Map; 80 import java.util.Optional; 81 import java.util.Properties; 82 import java.util.ResourceBundle; 83 import java.util.Set; 84 import java.util.TreeSet; 85 import java.util.jar.Attributes; 86 import java.util.jar.JarFile; 87 import java.util.jar.Manifest; 88 import java.util.stream.Collectors; 89 import java.util.stream.Stream; 90 91 import jdk.internal.misc.VM; 92 import jdk.internal.module.ModuleBootstrap; 93 import jdk.internal.module.Modules; 94 95 public final class LauncherHelper { 96 97 // No instantiation 98 private LauncherHelper() {} 99 100 // used to identify JavaFX applications 101 private static final String JAVAFX_APPLICATION_MARKER = 102 "JavaFX-Application-Class"; 103 private static final String JAVAFX_APPLICATION_CLASS_NAME = 104 "javafx.application.Application"; 105 private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX = 106 "sun.launcher.LauncherHelper$FXHelper"; 107 private static final String LAUNCHER_AGENT_CLASS = "Launcher-Agent-Class"; 108 private static final String MAIN_CLASS = "Main-Class"; 109 private static final String ADD_EXPORTS = "Add-Exports"; 110 private static final String ADD_OPENS = "Add-Opens"; 111 112 private static StringBuilder outBuf = new StringBuilder(); 113 114 private static final String INDENT = " "; 115 private static final String VM_SETTINGS = "VM settings:"; 116 private static final String PROP_SETTINGS = "Property settings:"; 117 private static final String LOCALE_SETTINGS = "Locale settings:"; 118 119 // sync with java.c and jdk.internal.misc.VM 120 private static final String diagprop = "sun.java.launcher.diag"; 121 static final boolean trace = VM.getSavedProperty(diagprop) != null; 122 123 private static final String defaultBundleName = 124 "sun.launcher.resources.launcher"; 125 private static class ResourceBundleHolder { 126 private static final ResourceBundle RB = 127 ResourceBundle.getBundle(defaultBundleName); 128 } 129 private static PrintStream ostream; 130 private static Class<?> appClass; // application class, for GUI/reporting purposes 131 132 /* 133 * A method called by the launcher to print out the standard settings, 134 * by default -XshowSettings is equivalent to -XshowSettings:all, 135 * Specific information may be gotten by using suboptions with possible 136 * values vm, properties and locale. 137 * 138 * printToStderr: choose between stdout and stderr 139 * 140 * optionFlag: specifies which options to print default is all other 141 * possible values are vm, properties, locale. 142 * 143 * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates 144 * this code should determine this value, using a suitable method or 145 * the line could be omitted. 146 * 147 * maxHeapSize: in bytes, as set by the launcher, a zero-value indicates 148 * this code should determine this value, using a suitable method. 149 * 150 * stackSize: in bytes, as set by the launcher, a zero-value indicates 151 * this code determine this value, using a suitable method or omit the 152 * line entirely. 153 */ 154 static void showSettings(boolean printToStderr, String optionFlag, 155 long initialHeapSize, long maxHeapSize, long stackSize) { 156 157 initOutput(printToStderr); 158 String opts[] = optionFlag.split(":"); 159 String optStr = (opts.length > 1 && opts[1] != null) 160 ? opts[1].trim() 161 : "all"; 162 switch (optStr) { 163 case "vm": 164 printVmSettings(initialHeapSize, maxHeapSize, stackSize); 165 break; 166 case "properties": 167 printProperties(); 168 break; 169 case "locale": 170 printLocale(); 171 break; 172 default: 173 printVmSettings(initialHeapSize, maxHeapSize, stackSize); 174 printProperties(); 175 printLocale(); 176 break; 177 } 178 } 179 180 /* 181 * prints the main vm settings subopt/section 182 */ 183 private static void printVmSettings( 184 long initialHeapSize, long maxHeapSize, 185 long stackSize) { 186 187 ostream.println(VM_SETTINGS); 188 if (stackSize != 0L) { 189 ostream.println(INDENT + "Stack Size: " + 190 SizePrefix.scaleValue(stackSize)); 191 } 192 if (initialHeapSize != 0L) { 193 ostream.println(INDENT + "Min. Heap Size: " + 194 SizePrefix.scaleValue(initialHeapSize)); 195 } 196 if (maxHeapSize != 0L) { 197 ostream.println(INDENT + "Max. Heap Size: " + 198 SizePrefix.scaleValue(maxHeapSize)); 199 } else { 200 ostream.println(INDENT + "Max. Heap Size (Estimated): " 201 + SizePrefix.scaleValue(Runtime.getRuntime().maxMemory())); 202 } 203 ostream.println(INDENT + "Using VM: " 204 + System.getProperty("java.vm.name")); 205 ostream.println(); 206 } 207 208 /* 209 * prints the properties subopt/section 210 */ 211 private static void printProperties() { 212 Properties p = System.getProperties(); 213 ostream.println(PROP_SETTINGS); 214 List<String> sortedPropertyKeys = new ArrayList<>(); 215 sortedPropertyKeys.addAll(p.stringPropertyNames()); 216 Collections.sort(sortedPropertyKeys); 217 for (String x : sortedPropertyKeys) { 218 printPropertyValue(x, p.getProperty(x)); 219 } 220 ostream.println(); 221 } 222 223 private static boolean isPath(String key) { 224 return key.endsWith(".dirs") || key.endsWith(".path"); 225 } 226 227 private static void printPropertyValue(String key, String value) { 228 ostream.print(INDENT + key + " = "); 229 if (key.equals("line.separator")) { 230 for (byte b : value.getBytes()) { 231 switch (b) { 232 case 0xd: 233 ostream.print("\\r "); 234 break; 235 case 0xa: 236 ostream.print("\\n "); 237 break; 238 default: 239 // print any bizzare line separators in hex, but really 240 // shouldn't happen. 241 ostream.printf("0x%02X", b & 0xff); 242 break; 243 } 244 } 245 ostream.println(); 246 return; 247 } 248 if (!isPath(key)) { 249 ostream.println(value); 250 return; 251 } 252 String[] values = value.split(System.getProperty("path.separator")); 253 boolean first = true; 254 for (String s : values) { 255 if (first) { // first line treated specially 256 ostream.println(s); 257 first = false; 258 } else { // following lines prefix with indents 259 ostream.println(INDENT + INDENT + s); 260 } 261 } 262 } 263 264 /* 265 * prints the locale subopt/section 266 */ 267 private static void printLocale() { 268 Locale locale = Locale.getDefault(); 269 ostream.println(LOCALE_SETTINGS); 270 ostream.println(INDENT + "default locale = " + 271 locale.getDisplayName()); 272 ostream.println(INDENT + "default display locale = " + 273 Locale.getDefault(Category.DISPLAY).getDisplayName()); 274 ostream.println(INDENT + "default format locale = " + 275 Locale.getDefault(Category.FORMAT).getDisplayName()); 276 printLocales(); 277 ostream.println(); 278 } 279 280 private static void printLocales() { 281 Locale[] tlocales = Locale.getAvailableLocales(); 282 final int len = tlocales == null ? 0 : tlocales.length; 283 if (len < 1 ) { 284 return; 285 } 286 // Locale does not implement Comparable so we convert it to String 287 // and sort it for pretty printing. 288 Set<String> sortedSet = new TreeSet<>(); 289 for (Locale l : tlocales) { 290 sortedSet.add(l.toString()); 291 } 292 293 ostream.print(INDENT + "available locales = "); 294 Iterator<String> iter = sortedSet.iterator(); 295 final int last = len - 1; 296 for (int i = 0 ; iter.hasNext() ; i++) { 297 String s = iter.next(); 298 ostream.print(s); 299 if (i != last) { 300 ostream.print(", "); 301 } 302 // print columns of 8 303 if ((i + 1) % 8 == 0) { 304 ostream.println(); 305 ostream.print(INDENT + INDENT); 306 } 307 } 308 } 309 310 private enum SizePrefix { 311 312 KILO(1024, "K"), 313 MEGA(1024 * 1024, "M"), 314 GIGA(1024 * 1024 * 1024, "G"), 315 TERA(1024L * 1024L * 1024L * 1024L, "T"); 316 long size; 317 String abbrev; 318 319 SizePrefix(long size, String abbrev) { 320 this.size = size; 321 this.abbrev = abbrev; 322 } 323 324 private static String scale(long v, SizePrefix prefix) { 325 return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size), 326 2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev; 327 } 328 /* 329 * scale the incoming values to a human readable form, represented as 330 * K, M, G and T, see java.c parse_size for the scaled values and 331 * suffixes. The lowest possible scaled value is Kilo. 332 */ 333 static String scaleValue(long v) { 334 if (v < MEGA.size) { 335 return scale(v, KILO); 336 } else if (v < GIGA.size) { 337 return scale(v, MEGA); 338 } else if (v < TERA.size) { 339 return scale(v, GIGA); 340 } else { 341 return scale(v, TERA); 342 } 343 } 344 } 345 346 /** 347 * A private helper method to get a localized message and also 348 * apply any arguments that we might pass. 349 */ 350 private static String getLocalizedMessage(String key, Object... args) { 351 String msg = ResourceBundleHolder.RB.getString(key); 352 return (args != null) ? MessageFormat.format(msg, args) : msg; 353 } 354 355 /** 356 * The java -help message is split into 3 parts, an invariant, followed 357 * by a set of platform dependent variant messages, finally an invariant 358 * set of lines. 359 * This method initializes the help message for the first time, and also 360 * assembles the invariant header part of the message. 361 */ 362 static void initHelpMessage(String progname) { 363 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header", 364 (progname == null) ? "java" : progname )); 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 void initOutput(PrintStream ps) { 415 ostream = ps; 416 } 417 418 static String getMainClassFromJar(String jarname) { 419 String mainValue; 420 try (JarFile jarFile = new JarFile(jarname)) { 421 Manifest manifest = jarFile.getManifest(); 422 if (manifest == null) { 423 abort(null, "java.launcher.jar.error2", jarname); 424 } 425 Attributes mainAttrs = manifest.getMainAttributes(); 426 if (mainAttrs == null) { 427 abort(null, "java.launcher.jar.error3", jarname); 428 } 429 430 // Main-Class 431 mainValue = mainAttrs.getValue(MAIN_CLASS); 432 if (mainValue == null) { 433 abort(null, "java.launcher.jar.error3", jarname); 434 } 435 436 // Launcher-Agent-Class (only check for this when Main-Class present) 437 String agentClass = mainAttrs.getValue(LAUNCHER_AGENT_CLASS); 438 if (agentClass != null) { 439 ModuleLayer.boot().findModule("java.instrument").ifPresent(m -> { 440 try { 441 String cn = "sun.instrument.InstrumentationImpl"; 442 Class<?> clazz = Class.forName(cn, false, null); 443 Method loadAgent = clazz.getMethod("loadAgent", String.class); 444 loadAgent.invoke(null, jarname); 445 } catch (Throwable e) { 446 if (e instanceof InvocationTargetException) e = e.getCause(); 447 abort(e, "java.launcher.jar.error4", jarname); 448 } 449 }); 450 } 451 452 // Add-Exports and Add-Opens 453 String exports = mainAttrs.getValue(ADD_EXPORTS); 454 if (exports != null) { 455 addExportsOrOpens(exports, false); 456 } 457 String opens = mainAttrs.getValue(ADD_OPENS); 458 if (opens != null) { 459 addExportsOrOpens(opens, true); 460 } 461 462 /* 463 * Hand off to FXHelper if it detects a JavaFX application 464 * This must be done after ensuring a Main-Class entry 465 * exists to enforce compliance with the jar specification 466 */ 467 if (mainAttrs.containsKey( 468 new Attributes.Name(JAVAFX_APPLICATION_MARKER))) { 469 FXHelper.setFXLaunchParameters(jarname, LM_JAR); 470 return FXHelper.class.getName(); 471 } 472 473 return mainValue.trim(); 474 } catch (IOException ioe) { 475 abort(ioe, "java.launcher.jar.error1", jarname); 476 } 477 return null; 478 } 479 480 /** 481 * Process the Add-Exports or Add-Opens value. The value is 482 * {@code <module>/<package> ( <module>/<package>)*}. 483 */ 484 static void addExportsOrOpens(String value, boolean open) { 485 for (String moduleAndPackage : value.split(" ")) { 486 String[] s = moduleAndPackage.trim().split("/"); 487 if (s.length == 2) { 488 String mn = s[0]; 489 String pn = s[1]; 490 ModuleLayer.boot() 491 .findModule(mn) 492 .filter(m -> m.getDescriptor().packages().contains(pn)) 493 .ifPresent(m -> { 494 if (open) { 495 Modules.addOpensToAllUnnamed(m, pn); 496 } else { 497 Modules.addExportsToAllUnnamed(m, pn); 498 } 499 }); 500 } 501 } 502 } 503 504 // From src/share/bin/java.c: 505 // enum LaunchMode { LM_UNKNOWN = 0, LM_CLASS, LM_JAR, LM_MODULE, LM_SOURCE } 506 507 private static final int LM_UNKNOWN = 0; 508 private static final int LM_CLASS = 1; 509 private static final int LM_JAR = 2; 510 private static final int LM_MODULE = 3; 511 private static final int LM_SOURCE = 4; 512 513 static void abort(Throwable t, String msgKey, Object... args) { 514 if (msgKey != null) { 515 ostream.println(getLocalizedMessage(msgKey, args)); 516 } 517 if (trace) { 518 if (t != null) { 519 t.printStackTrace(); 520 } else { 521 Thread.dumpStack(); 522 } 523 } 524 System.exit(1); 525 } 526 527 /** 528 * This method: 529 * 1. Loads the main class from the module or class path 530 * 2. Checks the public static void main method. 531 * 3. If the main class extends FX Application then call on FXHelper to 532 * perform the launch. 533 * 534 * @param printToStderr if set, all output will be routed to stderr 535 * @param mode LaunchMode as determined by the arguments passed on the 536 * command line 537 * @param what the module name[/class], JAR file, or the main class 538 * depending on the mode 539 * 540 * @return the application's main class 541 */ 542 @SuppressWarnings("fallthrough") 543 public static Class<?> checkAndLoadMain(boolean printToStderr, 544 int mode, 545 String what) { 546 initOutput(printToStderr); 547 548 Class<?> mainClass = null; 549 switch (mode) { 550 case LM_MODULE: case LM_SOURCE: 551 mainClass = loadModuleMainClass(what); 552 break; 553 default: 554 mainClass = loadMainClass(mode, what); 555 break; 556 } 557 558 // record the real main class for UI purposes 559 // neither method above can return null, they will abort() 560 appClass = mainClass; 561 562 /* 563 * Check if FXHelper can launch it using the FX launcher. In an FX app, 564 * the main class may or may not have a main method, so do this before 565 * validating the main class. 566 */ 567 if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) || 568 doesExtendFXApplication(mainClass)) { 569 // Will abort() if there are problems with FX runtime 570 FXHelper.setFXLaunchParameters(what, mode); 571 mainClass = FXHelper.class; 572 } 573 574 validateMainClass(mainClass); 575 return mainClass; 576 } 577 578 /** 579 * Returns the main class for a module. The query is either a module name 580 * or module-name/main-class. For the former then the module's main class 581 * is obtained from the module descriptor (MainClass attribute). 582 */ 583 private static Class<?> loadModuleMainClass(String what) { 584 int i = what.indexOf('/'); 585 String mainModule; 586 String mainClass; 587 if (i == -1) { 588 mainModule = what; 589 mainClass = null; 590 } else { 591 mainModule = what.substring(0, i); 592 mainClass = what.substring(i+1); 593 } 594 595 // main module is in the boot layer 596 ModuleLayer layer = ModuleLayer.boot(); 597 Optional<Module> om = layer.findModule(mainModule); 598 if (!om.isPresent()) { 599 // should not happen 600 throw new InternalError("Module " + mainModule + " not in boot Layer"); 601 } 602 Module m = om.get(); 603 604 // get main class 605 if (mainClass == null) { 606 Optional<String> omc = m.getDescriptor().mainClass(); 607 if (!omc.isPresent()) { 608 abort(null, "java.launcher.module.error1", mainModule); 609 } 610 mainClass = omc.get(); 611 } 612 613 // load the class from the module 614 Class<?> c = null; 615 try { 616 c = Class.forName(m, mainClass); 617 if (c == null && System.getProperty("os.name", "").contains("OS X") 618 && Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) { 619 620 String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC); 621 c = Class.forName(m, cn); 622 } 623 } catch (LinkageError le) { 624 abort(null, "java.launcher.module.error3", mainClass, m.getName(), 625 le.getClass().getName() + ": " + le.getLocalizedMessage()); 626 } 627 if (c == null) { 628 abort(null, "java.launcher.module.error2", mainClass, mainModule); 629 } 630 631 System.setProperty("jdk.module.main.class", c.getName()); 632 return c; 633 } 634 635 /** 636 * Loads the main class from the class path (LM_CLASS or LM_JAR). 637 */ 638 private static Class<?> loadMainClass(int mode, String what) { 639 // get the class name 640 String cn; 641 switch (mode) { 642 case LM_CLASS: 643 cn = what; 644 break; 645 case LM_JAR: 646 cn = getMainClassFromJar(what); 647 break; 648 default: 649 // should never happen 650 throw new InternalError("" + mode + ": Unknown launch mode"); 651 } 652 653 // load the main class 654 cn = cn.replace('/', '.'); 655 Class<?> mainClass = null; 656 ClassLoader scl = ClassLoader.getSystemClassLoader(); 657 try { 658 try { 659 mainClass = Class.forName(cn, false, scl); 660 } catch (NoClassDefFoundError | ClassNotFoundException cnfe) { 661 if (System.getProperty("os.name", "").contains("OS X") 662 && Normalizer.isNormalized(cn, Normalizer.Form.NFD)) { 663 try { 664 // On Mac OS X since all names with diacritical marks are 665 // given as decomposed it is possible that main class name 666 // comes incorrectly from the command line and we have 667 // to re-compose it 668 String ncn = Normalizer.normalize(cn, Normalizer.Form.NFC); 669 mainClass = Class.forName(ncn, false, scl); 670 } catch (NoClassDefFoundError | ClassNotFoundException cnfe1) { 671 abort(cnfe1, "java.launcher.cls.error1", cn, 672 cnfe1.getClass().getCanonicalName(), cnfe1.getMessage()); 673 } 674 } else { 675 abort(cnfe, "java.launcher.cls.error1", cn, 676 cnfe.getClass().getCanonicalName(), cnfe.getMessage()); 677 } 678 } 679 } catch (LinkageError le) { 680 abort(le, "java.launcher.cls.error6", cn, 681 le.getClass().getName() + ": " + le.getLocalizedMessage()); 682 } 683 return mainClass; 684 } 685 686 /* 687 * Accessor method called by the launcher after getting the main class via 688 * checkAndLoadMain(). The "application class" is the class that is finally 689 * executed to start the application and in this case is used to report 690 * the correct application name, typically for UI purposes. 691 */ 692 public static Class<?> getApplicationClass() { 693 return appClass; 694 } 695 696 /* 697 * Check if the given class is a JavaFX Application class. This is done 698 * in a way that does not cause the Application class to load or throw 699 * ClassNotFoundException if the JavaFX runtime is not available. 700 */ 701 private static boolean doesExtendFXApplication(Class<?> mainClass) { 702 for (Class<?> sc = mainClass.getSuperclass(); sc != null; 703 sc = sc.getSuperclass()) { 704 if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) { 705 return true; 706 } 707 } 708 return false; 709 } 710 711 // Check the existence and signature of main and abort if incorrect 712 static void validateMainClass(Class<?> mainClass) { 713 Method mainMethod = null; 714 try { 715 mainMethod = mainClass.getMethod("main", String[].class); 716 } catch (NoSuchMethodException nsme) { 717 // invalid main or not FX application, abort with an error 718 abort(null, "java.launcher.cls.error4", mainClass.getName(), 719 JAVAFX_APPLICATION_CLASS_NAME); 720 } catch (Throwable e) { 721 if (mainClass.getModule().isNamed()) { 722 abort(e, "java.launcher.module.error5", 723 mainClass.getName(), mainClass.getModule(), 724 e.getClass().getName(), e.getLocalizedMessage()); 725 } else { 726 abort(e, "java.launcher.cls.error7", mainClass.getName(), 727 e.getClass().getName(), e.getLocalizedMessage()); 728 } 729 } 730 731 /* 732 * getMethod (above) will choose the correct method, based 733 * on its name and parameter type, however, we still have to 734 * ensure that the method is static and returns a void. 735 */ 736 int mod = mainMethod.getModifiers(); 737 if (!Modifier.isStatic(mod)) { 738 abort(null, "java.launcher.cls.error2", "static", 739 mainMethod.getDeclaringClass().getName()); 740 } 741 if (mainMethod.getReturnType() != java.lang.Void.TYPE) { 742 abort(null, "java.launcher.cls.error3", 743 mainMethod.getDeclaringClass().getName()); 744 } 745 } 746 747 private static final String encprop = "sun.jnu.encoding"; 748 private static String encoding = null; 749 private static boolean isCharsetSupported = false; 750 751 /* 752 * converts a c or a byte array to a platform specific string, 753 * previously implemented as a native method in the launcher. 754 */ 755 static String makePlatformString(boolean printToStderr, byte[] inArray) { 756 initOutput(printToStderr); 757 if (encoding == null) { 758 encoding = System.getProperty(encprop); 759 isCharsetSupported = Charset.isSupported(encoding); 760 } 761 try { 762 String out = isCharsetSupported 763 ? new String(inArray, encoding) 764 : new String(inArray); 765 return out; 766 } catch (UnsupportedEncodingException uee) { 767 abort(uee, null); 768 } 769 return null; // keep the compiler happy 770 } 771 772 static String[] expandArgs(String[] argArray) { 773 List<StdArg> aList = new ArrayList<>(); 774 for (String x : argArray) { 775 aList.add(new StdArg(x)); 776 } 777 return expandArgs(aList); 778 } 779 780 static String[] expandArgs(List<StdArg> argList) { 781 ArrayList<String> out = new ArrayList<>(); 782 if (trace) { 783 System.err.println("Incoming arguments:"); 784 } 785 for (StdArg a : argList) { 786 if (trace) { 787 System.err.println(a); 788 } 789 if (a.needsExpansion) { 790 File x = new File(a.arg); 791 File parent = x.getParentFile(); 792 String glob = x.getName(); 793 if (parent == null) { 794 parent = new File("."); 795 } 796 try (DirectoryStream<Path> dstream = 797 Files.newDirectoryStream(parent.toPath(), glob)) { 798 int entries = 0; 799 for (Path p : dstream) { 800 out.add(p.normalize().toString()); 801 entries++; 802 } 803 if (entries == 0) { 804 out.add(a.arg); 805 } 806 } catch (Exception e) { 807 out.add(a.arg); 808 if (trace) { 809 System.err.println("Warning: passing argument as-is " + a); 810 System.err.print(e); 811 } 812 } 813 } else { 814 out.add(a.arg); 815 } 816 } 817 String[] oarray = new String[out.size()]; 818 out.toArray(oarray); 819 820 if (trace) { 821 System.err.println("Expanded arguments:"); 822 for (String x : oarray) { 823 System.err.println(x); 824 } 825 } 826 return oarray; 827 } 828 829 /* duplicate of the native StdArg struct */ 830 private static class StdArg { 831 final String arg; 832 final boolean needsExpansion; 833 StdArg(String arg, boolean expand) { 834 this.arg = arg; 835 this.needsExpansion = expand; 836 } 837 // protocol: first char indicates whether expansion is required 838 // 'T' = true ; needs expansion 839 // 'F' = false; needs no expansion 840 StdArg(String in) { 841 this.arg = in.substring(1); 842 needsExpansion = in.charAt(0) == 'T'; 843 } 844 public String toString() { 845 return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}'; 846 } 847 } 848 849 static final class FXHelper { 850 851 private static final String JAVAFX_GRAPHICS_MODULE_NAME = 852 "javafx.graphics"; 853 854 private static final String JAVAFX_LAUNCHER_CLASS_NAME = 855 "com.sun.javafx.application.LauncherImpl"; 856 857 /* 858 * The launch method used to invoke the JavaFX launcher. These must 859 * match the strings used in the launchApplication method. 860 * 861 * Command line JavaFX-App-Class Launch mode FX Launch mode 862 * java -cp fxapp.jar FXClass N/A LM_CLASS "LM_CLASS" 863 * java -cp somedir FXClass N/A LM_CLASS "LM_CLASS" 864 * java -jar fxapp.jar Present LM_JAR "LM_JAR" 865 * java -jar fxapp.jar Not Present LM_JAR "LM_JAR" 866 * java -m module/class [1] N/A LM_MODULE "LM_MODULE" 867 * java -m module N/A LM_MODULE "LM_MODULE" 868 * 869 * [1] - JavaFX-Application-Class is ignored when modular args are used, even 870 * if present in a modular jar 871 */ 872 private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS"; 873 private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR"; 874 private static final String JAVAFX_LAUNCH_MODE_MODULE = "LM_MODULE"; 875 876 /* 877 * FX application launcher and launch method, so we can launch 878 * applications with no main method. 879 */ 880 private static String fxLaunchName = null; 881 private static String fxLaunchMode = null; 882 883 private static Class<?> fxLauncherClass = null; 884 private static Method fxLauncherMethod = null; 885 886 /* 887 * Set the launch params according to what was passed to LauncherHelper 888 * so we can use the same launch mode for FX. Abort if there is any 889 * issue with loading the FX runtime or with the launcher method. 890 */ 891 private static void setFXLaunchParameters(String what, int mode) { 892 893 // find the module with the FX launcher 894 Optional<Module> om = ModuleLayer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME); 895 if (!om.isPresent()) { 896 abort(null, "java.launcher.cls.error5"); 897 } 898 899 // load the FX launcher class 900 fxLauncherClass = Class.forName(om.get(), JAVAFX_LAUNCHER_CLASS_NAME); 901 if (fxLauncherClass == null) { 902 abort(null, "java.launcher.cls.error5"); 903 } 904 905 try { 906 /* 907 * signature must be: 908 * public static void launchApplication(String launchName, 909 * String launchMode, String[] args); 910 */ 911 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication", 912 String.class, String.class, String[].class); 913 914 // verify launcher signature as we do when validating the main method 915 int mod = fxLauncherMethod.getModifiers(); 916 if (!Modifier.isStatic(mod)) { 917 abort(null, "java.launcher.javafx.error1"); 918 } 919 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) { 920 abort(null, "java.launcher.javafx.error1"); 921 } 922 } catch (NoSuchMethodException ex) { 923 abort(ex, "java.launcher.cls.error5", ex); 924 } 925 926 fxLaunchName = what; 927 switch (mode) { 928 case LM_CLASS: 929 fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS; 930 break; 931 case LM_JAR: 932 fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR; 933 break; 934 case LM_MODULE: 935 fxLaunchMode = JAVAFX_LAUNCH_MODE_MODULE; 936 break; 937 default: 938 // should not have gotten this far... 939 throw new InternalError(mode + ": Unknown launch mode"); 940 } 941 } 942 943 public static void main(String... args) throws Exception { 944 if (fxLauncherMethod == null 945 || fxLaunchMode == null 946 || fxLaunchName == null) { 947 throw new RuntimeException("Invalid JavaFX launch parameters"); 948 } 949 // launch appClass via fxLauncherMethod 950 fxLauncherMethod.invoke(null, 951 new Object[] {fxLaunchName, fxLaunchMode, args}); 952 } 953 } 954 955 /** 956 * Called by the launcher to list the observable modules. 957 */ 958 static void listModules() { 959 initOutput(System.out); 960 961 ModuleBootstrap.limitedFinder().findAll().stream() 962 .sorted(new JrtFirstComparator()) 963 .forEach(LauncherHelper::showModule); 964 } 965 966 /** 967 * Called by the launcher to show the resolved modules 968 */ 969 static void showResolvedModules() { 970 initOutput(System.out); 971 972 ModuleLayer bootLayer = ModuleLayer.boot(); 973 Configuration cf = bootLayer.configuration(); 974 975 cf.modules().stream() 976 .map(ResolvedModule::reference) 977 .sorted(new JrtFirstComparator()) 978 .forEach(LauncherHelper::showModule); 979 } 980 981 /** 982 * Called by the launcher to describe a module 983 */ 984 static void describeModule(String moduleName) { 985 initOutput(System.out); 986 987 ModuleFinder finder = ModuleBootstrap.limitedFinder(); 988 ModuleReference mref = finder.find(moduleName).orElse(null); 989 if (mref == null) { 990 abort(null, "java.launcher.module.error4", moduleName); 991 } 992 ModuleDescriptor md = mref.descriptor(); 993 994 // one-line summary 995 showModule(mref); 996 997 // unqualified exports (sorted by package) 998 md.exports().stream() 999 .filter(e -> !e.isQualified()) 1000 .sorted(Comparator.comparing(Exports::source)) 1001 .map(e -> Stream.concat(Stream.of(e.source()), 1002 toStringStream(e.modifiers())) 1003 .collect(Collectors.joining(" "))) 1004 .forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods)); 1005 1006 // dependences 1007 for (Requires r : md.requires()) { 1008 String nameAndMods = Stream.concat(Stream.of(r.name()), 1009 toStringStream(r.modifiers())) 1010 .collect(Collectors.joining(" ")); 1011 ostream.format("requires %s", nameAndMods); 1012 finder.find(r.name()) 1013 .map(ModuleReference::descriptor) 1014 .filter(ModuleDescriptor::isAutomatic) 1015 .ifPresent(any -> ostream.print(" automatic")); 1016 ostream.println(); 1017 } 1018 1019 // service use and provides 1020 for (String s : md.uses()) { 1021 ostream.format("uses %s%n", s); 1022 } 1023 for (Provides ps : md.provides()) { 1024 String names = ps.providers().stream().collect(Collectors.joining(" ")); 1025 ostream.format("provides %s with %s%n", ps.service(), names); 1026 1027 } 1028 1029 // qualified exports 1030 for (Exports e : md.exports()) { 1031 if (e.isQualified()) { 1032 String who = e.targets().stream().collect(Collectors.joining(" ")); 1033 ostream.format("qualified exports %s to %s%n", e.source(), who); 1034 } 1035 } 1036 1037 // open packages 1038 for (Opens opens: md.opens()) { 1039 if (opens.isQualified()) 1040 ostream.print("qualified "); 1041 String sourceAndMods = Stream.concat(Stream.of(opens.source()), 1042 toStringStream(opens.modifiers())) 1043 .collect(Collectors.joining(" ")); 1044 ostream.format("opens %s", sourceAndMods); 1045 if (opens.isQualified()) { 1046 String who = opens.targets().stream().collect(Collectors.joining(" ")); 1047 ostream.format(" to %s", who); 1048 } 1049 ostream.println(); 1050 } 1051 1052 // non-exported/non-open packages 1053 Set<String> concealed = new TreeSet<>(md.packages()); 1054 md.exports().stream().map(Exports::source).forEach(concealed::remove); 1055 md.opens().stream().map(Opens::source).forEach(concealed::remove); 1056 concealed.forEach(p -> ostream.format("contains %s%n", p)); 1057 } 1058 1059 /** 1060 * Prints a single line with the module name, version and modifiers 1061 */ 1062 private static void showModule(ModuleReference mref) { 1063 ModuleDescriptor md = mref.descriptor(); 1064 ostream.print(md.toNameAndVersion()); 1065 mref.location() 1066 .filter(uri -> !isJrt(uri)) 1067 .ifPresent(uri -> ostream.format(" %s", uri)); 1068 if (md.isOpen()) 1069 ostream.print(" open"); 1070 if (md.isAutomatic()) 1071 ostream.print(" automatic"); 1072 ostream.println(); 1073 } 1074 1075 /** 1076 * A ModuleReference comparator that considers modules in the run-time 1077 * image to be less than modules than not in the run-time image. 1078 */ 1079 private static class JrtFirstComparator implements Comparator<ModuleReference> { 1080 private final Comparator<ModuleReference> real; 1081 1082 JrtFirstComparator() { 1083 this.real = Comparator.comparing(ModuleReference::descriptor); 1084 } 1085 1086 @Override 1087 public int compare(ModuleReference a, ModuleReference b) { 1088 if (isJrt(a)) { 1089 return isJrt(b) ? real.compare(a, b) : -1; 1090 } else { 1091 return isJrt(b) ? 1 : real.compare(a, b); 1092 } 1093 } 1094 } 1095 1096 private static <T> Stream<String> toStringStream(Set<T> s) { 1097 return s.stream().map(e -> e.toString().toLowerCase()); 1098 } 1099 1100 private static boolean isJrt(ModuleReference mref) { 1101 return isJrt(mref.location().orElse(null)); 1102 } 1103 1104 private static boolean isJrt(URI uri) { 1105 return (uri != null && uri.getScheme().equalsIgnoreCase("jrt")); 1106 } 1107 1108 /** 1109 * Called by the launcher to validate the modules on the upgrade and 1110 * application module paths. 1111 * 1112 * @return {@code true} if no errors are found 1113 */ 1114 private static boolean validateModules() { 1115 initOutput(System.out); 1116 1117 ModuleValidator validator = new ModuleValidator(); 1118 1119 // upgrade module path 1120 String value = System.getProperty("jdk.module.upgrade.path"); 1121 if (value != null) { 1122 Stream.of(value.split(File.pathSeparator)) 1123 .map(Paths::get) 1124 .forEach(validator::scan); 1125 } 1126 1127 // system modules 1128 ModuleFinder.ofSystem().findAll().stream() 1129 .sorted(Comparator.comparing(ModuleReference::descriptor)) 1130 .forEach(validator::process); 1131 1132 // application module path 1133 value = System.getProperty("jdk.module.path"); 1134 if (value != null) { 1135 Stream.of(value.split(File.pathSeparator)) 1136 .map(Paths::get) 1137 .forEach(validator::scan); 1138 } 1139 1140 return !validator.foundErrors(); 1141 } 1142 1143 /** 1144 * A simple validator to check for errors and conflicts between modules. 1145 */ 1146 static class ModuleValidator { 1147 private static final String MODULE_INFO = "module-info.class"; 1148 1149 private Map<String, ModuleReference> nameToModule = new HashMap<>(); 1150 private Map<String, ModuleReference> packageToModule = new HashMap<>(); 1151 private boolean errorFound; 1152 1153 /** 1154 * Returns true if at least one error was found 1155 */ 1156 boolean foundErrors() { 1157 return errorFound; 1158 } 1159 1160 /** 1161 * Prints the module location and name. 1162 */ 1163 private void printModule(ModuleReference mref) { 1164 mref.location() 1165 .filter(uri -> !isJrt(uri)) 1166 .ifPresent(uri -> ostream.print(uri + " ")); 1167 ModuleDescriptor descriptor = mref.descriptor(); 1168 ostream.print(descriptor.name()); 1169 if (descriptor.isAutomatic()) 1170 ostream.print(" automatic"); 1171 ostream.println(); 1172 } 1173 1174 /** 1175 * Prints the module location and name, checks if the module is 1176 * shadowed by a previously seen module, and finally checks for 1177 * package conflicts with previously seen modules. 1178 */ 1179 void process(ModuleReference mref) { 1180 printModule(mref); 1181 1182 String name = mref.descriptor().name(); 1183 ModuleReference previous = nameToModule.putIfAbsent(name, mref); 1184 if (previous != null) { 1185 ostream.print(INDENT + "shadowed by "); 1186 printModule(previous); 1187 } else { 1188 // check for package conflicts when not shadowed 1189 for (String pkg : mref.descriptor().packages()) { 1190 previous = packageToModule.putIfAbsent(pkg, mref); 1191 if (previous != null) { 1192 String mn = previous.descriptor().name(); 1193 ostream.println(INDENT + "contains " + pkg 1194 + " conflicts with module " + mn); 1195 errorFound = true; 1196 } 1197 } 1198 } 1199 } 1200 1201 /** 1202 * Scan an element on a module path. The element is a directory 1203 * of modules, an exploded module, or a JAR file. 1204 */ 1205 void scan(Path entry) { 1206 BasicFileAttributes attrs; 1207 try { 1208 attrs = Files.readAttributes(entry, BasicFileAttributes.class); 1209 } catch (NoSuchFileException ignore) { 1210 return; 1211 } catch (IOException ioe) { 1212 ostream.println(entry + " " + ioe); 1213 errorFound = true; 1214 return; 1215 } 1216 1217 String fn = entry.getFileName().toString(); 1218 if (attrs.isRegularFile() && fn.endsWith(".jar")) { 1219 // JAR file, explicit or automatic module 1220 scanModule(entry).ifPresent(this::process); 1221 } else if (attrs.isDirectory()) { 1222 Path mi = entry.resolve(MODULE_INFO); 1223 if (Files.exists(mi)) { 1224 // exploded module 1225 scanModule(entry).ifPresent(this::process); 1226 } else { 1227 // directory of modules 1228 scanDirectory(entry); 1229 } 1230 } 1231 } 1232 1233 /** 1234 * Scan the JAR files and exploded modules in a directory. 1235 */ 1236 private void scanDirectory(Path dir) { 1237 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { 1238 Map<String, Path> moduleToEntry = new HashMap<>(); 1239 1240 for (Path entry : stream) { 1241 BasicFileAttributes attrs; 1242 try { 1243 attrs = Files.readAttributes(entry, BasicFileAttributes.class); 1244 } catch (IOException ioe) { 1245 ostream.println(entry + " " + ioe); 1246 errorFound = true; 1247 continue; 1248 } 1249 1250 ModuleReference mref = null; 1251 1252 String fn = entry.getFileName().toString(); 1253 if (attrs.isRegularFile() && fn.endsWith(".jar")) { 1254 mref = scanModule(entry).orElse(null); 1255 } else if (attrs.isDirectory()) { 1256 Path mi = entry.resolve(MODULE_INFO); 1257 if (Files.exists(mi)) { 1258 mref = scanModule(entry).orElse(null); 1259 } 1260 } 1261 1262 if (mref != null) { 1263 String name = mref.descriptor().name(); 1264 Path previous = moduleToEntry.putIfAbsent(name, entry); 1265 if (previous != null) { 1266 // same name as other module in the directory 1267 printModule(mref); 1268 ostream.println(INDENT + "contains same module as " 1269 + previous.getFileName()); 1270 errorFound = true; 1271 } else { 1272 process(mref); 1273 } 1274 } 1275 } 1276 } catch (IOException ioe) { 1277 ostream.println(dir + " " + ioe); 1278 errorFound = true; 1279 } 1280 } 1281 1282 /** 1283 * Scan a JAR file or exploded module. 1284 */ 1285 private Optional<ModuleReference> scanModule(Path entry) { 1286 ModuleFinder finder = ModuleFinder.of(entry); 1287 try { 1288 return finder.findAll().stream().findFirst(); 1289 } catch (FindException e) { 1290 ostream.println(entry); 1291 ostream.println(INDENT + e.getMessage()); 1292 Throwable cause = e.getCause(); 1293 if (cause != null) { 1294 ostream.println(INDENT + cause); 1295 } 1296 errorFound = true; 1297 return Optional.empty(); 1298 } 1299 } 1300 } 1301 }