1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.launcher; 27 28 /* 29 * 30 * <p><b>This is NOT part of any API supported by Sun Microsystems. 31 * If you write code that depends on this, you do so at your own 32 * risk. This code and its internal interfaces are subject to change 33 * or deletion without notice.</b> 34 * 35 */ 36 37 /** 38 * A utility package for the java(1), javaw(1) launchers. 39 * The following are helper methods that the native launcher uses 40 * to perform checks etc. using JNI, see src/share/bin/java.c 41 */ 42 import java.io.File; 43 import java.io.IOException; 44 import java.io.PrintStream; 45 import java.io.UnsupportedEncodingException; 46 import java.lang.module.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 } 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 512 static void abort(Throwable t, String msgKey, Object... args) { 513 if (msgKey != null) { 514 ostream.println(getLocalizedMessage(msgKey, args)); 515 } 516 if (trace) { 517 if (t != null) { 518 t.printStackTrace(); 519 } else { 520 Thread.dumpStack(); 521 } 522 } 523 System.exit(1); 524 } 525 526 /** 527 * This method: 528 * 1. Loads the main class from the module or class path 529 * 2. Checks the public static void main method. 530 * 3. If the main class extends FX Application then call on FXHelper to 531 * perform the launch. 532 * 533 * @param printToStderr if set, all output will be routed to stderr 534 * @param mode LaunchMode as determined by the arguments passed on the 535 * command line 536 * @param what the module name[/class], JAR file, or the main class 537 * depending on the mode 538 * 539 * @return the application's main class 540 */ 541 public static Class<?> checkAndLoadMain(boolean printToStderr, 542 int mode, 543 String what) { 544 initOutput(printToStderr); 545 546 Class<?> mainClass = (mode == LM_MODULE) ? loadModuleMainClass(what) 547 : loadMainClass(mode, what); 548 549 // record the real main class for UI purposes 550 // neither method above can return null, they will abort() 551 appClass = mainClass; 552 553 /* 554 * Check if FXHelper can launch it using the FX launcher. In an FX app, 555 * the main class may or may not have a main method, so do this before 556 * validating the main class. 557 */ 558 if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) || 559 doesExtendFXApplication(mainClass)) { 560 // Will abort() if there are problems with FX runtime 561 FXHelper.setFXLaunchParameters(what, mode); 562 mainClass = FXHelper.class; 563 } 564 565 validateMainClass(mainClass); 566 return mainClass; 567 } 568 569 /** 570 * Returns the main class for a module. The query is either a module name 571 * or module-name/main-class. For the former then the module's main class 572 * is obtained from the module descriptor (MainClass attribute). 573 */ 574 private static Class<?> loadModuleMainClass(String what) { 575 int i = what.indexOf('/'); 576 String mainModule; 577 String mainClass; 578 if (i == -1) { 579 mainModule = what; 580 mainClass = null; 581 } else { 582 mainModule = what.substring(0, i); 583 mainClass = what.substring(i+1); 584 } 585 586 // main module is in the boot layer 587 ModuleLayer layer = ModuleLayer.boot(); 588 Optional<Module> om = layer.findModule(mainModule); 589 if (!om.isPresent()) { 590 // should not happen 591 throw new InternalError("Module " + mainModule + " not in boot Layer"); 592 } 593 Module m = om.get(); 594 595 // get main class 596 if (mainClass == null) { 597 Optional<String> omc = m.getDescriptor().mainClass(); 598 if (!omc.isPresent()) { 599 abort(null, "java.launcher.module.error1", mainModule); 600 } 601 mainClass = omc.get(); 602 } 603 604 // load the class from the module 605 Class<?> c = null; 606 try { 607 c = Class.forName(m, mainClass); 608 if (c == null && System.getProperty("os.name", "").contains("OS X") 609 && Normalizer.isNormalized(mainClass, Normalizer.Form.NFD)) { 610 611 String cn = Normalizer.normalize(mainClass, Normalizer.Form.NFC); 612 c = Class.forName(m, cn); 613 } 614 } catch (LinkageError le) { 615 abort(null, "java.launcher.module.error3", mainClass, m.getName(), 616 le.getClass().getName() + ": " + le.getLocalizedMessage()); 617 } 618 if (c == null) { 619 abort(null, "java.launcher.module.error2", mainClass, mainModule); 620 } 621 622 System.setProperty("jdk.module.main.class", c.getName()); 623 return c; 624 } 625 626 /** 627 * Loads the main class from the class path (LM_CLASS or LM_JAR). 628 */ 629 private static Class<?> loadMainClass(int mode, String what) { 630 // get the class name 631 String cn; 632 switch (mode) { 633 case LM_CLASS: 634 cn = what; 635 break; 636 case LM_JAR: 637 cn = getMainClassFromJar(what); 638 break; 639 default: 640 // should never happen 641 throw new InternalError("" + mode + ": Unknown launch mode"); 642 } 643 644 // load the main class 645 cn = cn.replace('/', '.'); 646 Class<?> mainClass = null; 647 ClassLoader scl = ClassLoader.getSystemClassLoader(); 648 try { 649 try { 650 mainClass = Class.forName(cn, false, scl); 651 } catch (NoClassDefFoundError | ClassNotFoundException cnfe) { 652 if (System.getProperty("os.name", "").contains("OS X") 653 && Normalizer.isNormalized(cn, Normalizer.Form.NFD)) { 654 try { 655 // On Mac OS X since all names with diacritical marks are 656 // given as decomposed it is possible that main class name 657 // comes incorrectly from the command line and we have 658 // to re-compose it 659 String ncn = Normalizer.normalize(cn, Normalizer.Form.NFC); 660 mainClass = Class.forName(ncn, false, scl); 661 } catch (NoClassDefFoundError | ClassNotFoundException cnfe1) { 662 abort(cnfe1, "java.launcher.cls.error1", cn, 663 cnfe1.getClass().getCanonicalName(), cnfe1.getMessage()); 664 } 665 } else { 666 abort(cnfe, "java.launcher.cls.error1", cn, 667 cnfe.getClass().getCanonicalName(), cnfe.getMessage()); 668 } 669 } 670 } catch (LinkageError le) { 671 abort(le, "java.launcher.cls.error6", cn, 672 le.getClass().getName() + ": " + le.getLocalizedMessage()); 673 } 674 return mainClass; 675 } 676 677 /* 678 * Accessor method called by the launcher after getting the main class via 679 * checkAndLoadMain(). The "application class" is the class that is finally 680 * executed to start the application and in this case is used to report 681 * the correct application name, typically for UI purposes. 682 */ 683 public static Class<?> getApplicationClass() { 684 return appClass; 685 } 686 687 /* 688 * Check if the given class is a JavaFX Application class. This is done 689 * in a way that does not cause the Application class to load or throw 690 * ClassNotFoundException if the JavaFX runtime is not available. 691 */ 692 private static boolean doesExtendFXApplication(Class<?> mainClass) { 693 for (Class<?> sc = mainClass.getSuperclass(); sc != null; 694 sc = sc.getSuperclass()) { 695 if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) { 696 return true; 697 } 698 } 699 return false; 700 } 701 702 // Check the existence and signature of main and abort if incorrect 703 static void validateMainClass(Class<?> mainClass) { 704 Method mainMethod = null; 705 try { 706 mainMethod = mainClass.getMethod("main", String[].class); 707 } catch (NoSuchMethodException nsme) { 708 // invalid main or not FX application, abort with an error 709 abort(null, "java.launcher.cls.error4", mainClass.getName(), 710 JAVAFX_APPLICATION_CLASS_NAME); 711 } catch (Throwable e) { 712 if (mainClass.getModule().isNamed()) { 713 abort(e, "java.launcher.module.error5", 714 mainClass.getName(), mainClass.getModule(), 715 e.getClass().getName(), e.getLocalizedMessage()); 716 } else { 717 abort(e, "java.launcher.cls.error7", mainClass.getName(), 718 e.getClass().getName(), e.getLocalizedMessage()); 719 } 720 } 721 722 /* 723 * getMethod (above) will choose the correct method, based 724 * on its name and parameter type, however, we still have to 725 * ensure that the method is static and returns a void. 726 */ 727 int mod = mainMethod.getModifiers(); 728 if (!Modifier.isStatic(mod)) { 729 abort(null, "java.launcher.cls.error2", "static", 730 mainMethod.getDeclaringClass().getName()); 731 } 732 if (mainMethod.getReturnType() != java.lang.Void.TYPE) { 733 abort(null, "java.launcher.cls.error3", 734 mainMethod.getDeclaringClass().getName()); 735 } 736 } 737 738 private static final String encprop = "sun.jnu.encoding"; 739 private static String encoding = null; 740 private static boolean isCharsetSupported = false; 741 742 /* 743 * converts a c or a byte array to a platform specific string, 744 * previously implemented as a native method in the launcher. 745 */ 746 static String makePlatformString(boolean printToStderr, byte[] inArray) { 747 initOutput(printToStderr); 748 if (encoding == null) { 749 encoding = System.getProperty(encprop); 750 isCharsetSupported = Charset.isSupported(encoding); 751 } 752 try { 753 String out = isCharsetSupported 754 ? new String(inArray, encoding) 755 : new String(inArray); 756 return out; 757 } catch (UnsupportedEncodingException uee) { 758 abort(uee, null); 759 } 760 return null; // keep the compiler happy 761 } 762 763 static String[] expandArgs(String[] argArray) { 764 List<StdArg> aList = new ArrayList<>(); 765 for (String x : argArray) { 766 aList.add(new StdArg(x)); 767 } 768 return expandArgs(aList); 769 } 770 771 static String[] expandArgs(List<StdArg> argList) { 772 ArrayList<String> out = new ArrayList<>(); 773 if (trace) { 774 System.err.println("Incoming arguments:"); 775 } 776 for (StdArg a : argList) { 777 if (trace) { 778 System.err.println(a); 779 } 780 if (a.needsExpansion) { 781 File x = new File(a.arg); 782 File parent = x.getParentFile(); 783 String glob = x.getName(); 784 if (parent == null) { 785 parent = new File("."); 786 } 787 try (DirectoryStream<Path> dstream = 788 Files.newDirectoryStream(parent.toPath(), glob)) { 789 int entries = 0; 790 for (Path p : dstream) { 791 out.add(p.normalize().toString()); 792 entries++; 793 } 794 if (entries == 0) { 795 out.add(a.arg); 796 } 797 } catch (Exception e) { 798 out.add(a.arg); 799 if (trace) { 800 System.err.println("Warning: passing argument as-is " + a); 801 System.err.print(e); 802 } 803 } 804 } else { 805 out.add(a.arg); 806 } 807 } 808 String[] oarray = new String[out.size()]; 809 out.toArray(oarray); 810 811 if (trace) { 812 System.err.println("Expanded arguments:"); 813 for (String x : oarray) { 814 System.err.println(x); 815 } 816 } 817 return oarray; 818 } 819 820 /* duplicate of the native StdArg struct */ 821 private static class StdArg { 822 final String arg; 823 final boolean needsExpansion; 824 StdArg(String arg, boolean expand) { 825 this.arg = arg; 826 this.needsExpansion = expand; 827 } 828 // protocol: first char indicates whether expansion is required 829 // 'T' = true ; needs expansion 830 // 'F' = false; needs no expansion 831 StdArg(String in) { 832 this.arg = in.substring(1); 833 needsExpansion = in.charAt(0) == 'T'; 834 } 835 public String toString() { 836 return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}'; 837 } 838 } 839 840 static final class FXHelper { 841 842 private static final String JAVAFX_GRAPHICS_MODULE_NAME = 843 "javafx.graphics"; 844 845 private static final String JAVAFX_LAUNCHER_CLASS_NAME = 846 "com.sun.javafx.application.LauncherImpl"; 847 848 /* 849 * The launch method used to invoke the JavaFX launcher. These must 850 * match the strings used in the launchApplication method. 851 * 852 * Command line JavaFX-App-Class Launch mode FX Launch mode 853 * java -cp fxapp.jar FXClass N/A LM_CLASS "LM_CLASS" 854 * java -cp somedir FXClass N/A LM_CLASS "LM_CLASS" 855 * java -jar fxapp.jar Present LM_JAR "LM_JAR" 856 * java -jar fxapp.jar Not Present LM_JAR "LM_JAR" 857 * java -m module/class [1] N/A LM_MODULE "LM_MODULE" 858 * java -m module N/A LM_MODULE "LM_MODULE" 859 * 860 * [1] - JavaFX-Application-Class is ignored when modular args are used, even 861 * if present in a modular jar 862 */ 863 private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS"; 864 private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR"; 865 private static final String JAVAFX_LAUNCH_MODE_MODULE = "LM_MODULE"; 866 867 /* 868 * FX application launcher and launch method, so we can launch 869 * applications with no main method. 870 */ 871 private static String fxLaunchName = null; 872 private static String fxLaunchMode = null; 873 874 private static Class<?> fxLauncherClass = null; 875 private static Method fxLauncherMethod = null; 876 877 /* 878 * Set the launch params according to what was passed to LauncherHelper 879 * so we can use the same launch mode for FX. Abort if there is any 880 * issue with loading the FX runtime or with the launcher method. 881 */ 882 private static void setFXLaunchParameters(String what, int mode) { 883 884 // find the module with the FX launcher 885 Optional<Module> om = ModuleLayer.boot().findModule(JAVAFX_GRAPHICS_MODULE_NAME); 886 if (!om.isPresent()) { 887 abort(null, "java.launcher.cls.error5"); 888 } 889 890 // load the FX launcher class 891 fxLauncherClass = Class.forName(om.get(), JAVAFX_LAUNCHER_CLASS_NAME); 892 if (fxLauncherClass == null) { 893 abort(null, "java.launcher.cls.error5"); 894 } 895 896 try { 897 /* 898 * signature must be: 899 * public static void launchApplication(String launchName, 900 * String launchMode, String[] args); 901 */ 902 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication", 903 String.class, String.class, String[].class); 904 905 // verify launcher signature as we do when validating the main method 906 int mod = fxLauncherMethod.getModifiers(); 907 if (!Modifier.isStatic(mod)) { 908 abort(null, "java.launcher.javafx.error1"); 909 } 910 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) { 911 abort(null, "java.launcher.javafx.error1"); 912 } 913 } catch (NoSuchMethodException ex) { 914 abort(ex, "java.launcher.cls.error5", ex); 915 } 916 917 fxLaunchName = what; 918 switch (mode) { 919 case LM_CLASS: 920 fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS; 921 break; 922 case LM_JAR: 923 fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR; 924 break; 925 case LM_MODULE: 926 fxLaunchMode = JAVAFX_LAUNCH_MODE_MODULE; 927 break; 928 default: 929 // should not have gotten this far... 930 throw new InternalError(mode + ": Unknown launch mode"); 931 } 932 } 933 934 public static void main(String... args) throws Exception { 935 if (fxLauncherMethod == null 936 || fxLaunchMode == null 937 || fxLaunchName == null) { 938 throw new RuntimeException("Invalid JavaFX launch parameters"); 939 } 940 // launch appClass via fxLauncherMethod 941 fxLauncherMethod.invoke(null, 942 new Object[] {fxLaunchName, fxLaunchMode, args}); 943 } 944 } 945 946 /** 947 * Called by the launcher to list the observable modules. 948 */ 949 static void listModules() { 950 initOutput(System.out); 951 952 ModuleBootstrap.limitedFinder().findAll().stream() 953 .sorted(new JrtFirstComparator()) 954 .forEach(LauncherHelper::showModule); 955 } 956 957 /** 958 * Called by the launcher to show the resolved modules 959 */ 960 static void showResolvedModules() { 961 initOutput(System.out); 962 963 ModuleLayer bootLayer = ModuleLayer.boot(); 964 Configuration cf = bootLayer.configuration(); 965 966 cf.modules().stream() 967 .map(ResolvedModule::reference) 968 .sorted(new JrtFirstComparator()) 969 .forEach(LauncherHelper::showModule); 970 } 971 972 /** 973 * Called by the launcher to describe a module 974 */ 975 static void describeModule(String moduleName) { 976 initOutput(System.out); 977 978 ModuleFinder finder = ModuleBootstrap.limitedFinder(); 979 ModuleReference mref = finder.find(moduleName).orElse(null); 980 if (mref == null) { 981 abort(null, "java.launcher.module.error4", moduleName); 982 } 983 ModuleDescriptor md = mref.descriptor(); 984 985 // one-line summary 986 showModule(mref); 987 988 // unqualified exports (sorted by package) 989 md.exports().stream() 990 .filter(e -> !e.isQualified()) 991 .sorted(Comparator.comparing(Exports::source)) 992 .map(e -> Stream.concat(Stream.of(e.source()), 993 toStringStream(e.modifiers())) 994 .collect(Collectors.joining(" "))) 995 .forEach(sourceAndMods -> ostream.format("exports %s%n", sourceAndMods)); 996 997 // dependences 998 for (Requires r : md.requires()) { 999 String nameAndMods = Stream.concat(Stream.of(r.name()), 1000 toStringStream(r.modifiers())) 1001 .collect(Collectors.joining(" ")); 1002 ostream.format("requires %s", nameAndMods); 1003 finder.find(r.name()) 1004 .map(ModuleReference::descriptor) 1005 .filter(ModuleDescriptor::isAutomatic) 1006 .ifPresent(any -> ostream.print(" automatic")); 1007 ostream.println(); 1008 } 1009 1010 // service use and provides 1011 for (String s : md.uses()) { 1012 ostream.format("uses %s%n", s); 1013 } 1014 for (Provides ps : md.provides()) { 1015 String names = ps.providers().stream().collect(Collectors.joining(" ")); 1016 ostream.format("provides %s with %s%n", ps.service(), names); 1017 1018 } 1019 1020 // qualified exports 1021 for (Exports e : md.exports()) { 1022 if (e.isQualified()) { 1023 String who = e.targets().stream().collect(Collectors.joining(" ")); 1024 ostream.format("qualified exports %s to %s%n", e.source(), who); 1025 } 1026 } 1027 1028 // open packages 1029 for (Opens opens: md.opens()) { 1030 if (opens.isQualified()) 1031 ostream.print("qualified "); 1032 String sourceAndMods = Stream.concat(Stream.of(opens.source()), 1033 toStringStream(opens.modifiers())) 1034 .collect(Collectors.joining(" ")); 1035 ostream.format("opens %s", sourceAndMods); 1036 if (opens.isQualified()) { 1037 String who = opens.targets().stream().collect(Collectors.joining(" ")); 1038 ostream.format(" to %s", who); 1039 } 1040 ostream.println(); 1041 } 1042 1043 // non-exported/non-open packages 1044 Set<String> concealed = new TreeSet<>(md.packages()); 1045 md.exports().stream().map(Exports::source).forEach(concealed::remove); 1046 md.opens().stream().map(Opens::source).forEach(concealed::remove); 1047 concealed.forEach(p -> ostream.format("contains %s%n", p)); 1048 } 1049 1050 /** 1051 * Prints a single line with the module name, version and modifiers 1052 */ 1053 private static void showModule(ModuleReference mref) { 1054 ModuleDescriptor md = mref.descriptor(); 1055 ostream.print(md.toNameAndVersion()); 1056 mref.location() 1057 .filter(uri -> !isJrt(uri)) 1058 .ifPresent(uri -> ostream.format(" %s", uri)); 1059 if (md.isOpen()) 1060 ostream.print(" open"); 1061 if (md.isAutomatic()) 1062 ostream.print(" automatic"); 1063 ostream.println(); 1064 } 1065 1066 /** 1067 * A ModuleReference comparator that considers modules in the run-time 1068 * image to be less than modules than not in the run-time image. 1069 */ 1070 private static class JrtFirstComparator implements Comparator<ModuleReference> { 1071 private final Comparator<ModuleReference> real; 1072 1073 JrtFirstComparator() { 1074 this.real = Comparator.comparing(ModuleReference::descriptor); 1075 } 1076 1077 @Override 1078 public int compare(ModuleReference a, ModuleReference b) { 1079 if (isJrt(a)) { 1080 return isJrt(b) ? real.compare(a, b) : -1; 1081 } else { 1082 return isJrt(b) ? 1 : real.compare(a, b); 1083 } 1084 } 1085 } 1086 1087 private static <T> Stream<String> toStringStream(Set<T> s) { 1088 return s.stream().map(e -> e.toString().toLowerCase()); 1089 } 1090 1091 private static boolean isJrt(ModuleReference mref) { 1092 return isJrt(mref.location().orElse(null)); 1093 } 1094 1095 private static boolean isJrt(URI uri) { 1096 return (uri != null && uri.getScheme().equalsIgnoreCase("jrt")); 1097 } 1098 1099 /** 1100 * Called by the launcher to validate the modules on the upgrade and 1101 * application module paths. 1102 * 1103 * @return {@code true} if no errors are found 1104 */ 1105 private static boolean validateModules() { 1106 initOutput(System.out); 1107 1108 ModuleValidator validator = new ModuleValidator(); 1109 1110 // upgrade module path 1111 String value = System.getProperty("jdk.module.upgrade.path"); 1112 if (value != null) { 1113 Stream.of(value.split(File.pathSeparator)) 1114 .map(Paths::get) 1115 .forEach(validator::scan); 1116 } 1117 1118 // system modules 1119 ModuleFinder.ofSystem().findAll().stream() 1120 .sorted(Comparator.comparing(ModuleReference::descriptor)) 1121 .forEach(validator::process); 1122 1123 // application module path 1124 value = System.getProperty("jdk.module.path"); 1125 if (value != null) { 1126 Stream.of(value.split(File.pathSeparator)) 1127 .map(Paths::get) 1128 .forEach(validator::scan); 1129 } 1130 1131 return !validator.foundErrors(); 1132 } 1133 1134 /** 1135 * A simple validator to check for errors and conflicts between modules. 1136 */ 1137 static class ModuleValidator { 1138 private static final String MODULE_INFO = "module-info.class"; 1139 1140 private Map<String, ModuleReference> nameToModule = new HashMap<>(); 1141 private Map<String, ModuleReference> packageToModule = new HashMap<>(); 1142 private boolean errorFound; 1143 1144 /** 1145 * Returns true if at least one error was found 1146 */ 1147 boolean foundErrors() { 1148 return errorFound; 1149 } 1150 1151 /** 1152 * Prints the module location and name. 1153 */ 1154 private void printModule(ModuleReference mref) { 1155 mref.location() 1156 .filter(uri -> !isJrt(uri)) 1157 .ifPresent(uri -> ostream.print(uri + " ")); 1158 ModuleDescriptor descriptor = mref.descriptor(); 1159 ostream.print(descriptor.name()); 1160 if (descriptor.isAutomatic()) 1161 ostream.print(" automatic"); 1162 ostream.println(); 1163 } 1164 1165 /** 1166 * Prints the module location and name, checks if the module is 1167 * shadowed by a previously seen module, and finally checks for 1168 * package conflicts with previously seen modules. 1169 */ 1170 void process(ModuleReference mref) { 1171 printModule(mref); 1172 1173 String name = mref.descriptor().name(); 1174 ModuleReference previous = nameToModule.putIfAbsent(name, mref); 1175 if (previous != null) { 1176 ostream.print(INDENT + "shadowed by "); 1177 printModule(previous); 1178 } else { 1179 // check for package conflicts when not shadowed 1180 for (String pkg : mref.descriptor().packages()) { 1181 previous = packageToModule.putIfAbsent(pkg, mref); 1182 if (previous != null) { 1183 String mn = previous.descriptor().name(); 1184 ostream.println(INDENT + "contains " + pkg 1185 + " conflicts with module " + mn); 1186 errorFound = true; 1187 } 1188 } 1189 } 1190 } 1191 1192 /** 1193 * Scan an element on a module path. The element is a directory 1194 * of modules, an exploded module, or a JAR file. 1195 */ 1196 void scan(Path entry) { 1197 BasicFileAttributes attrs; 1198 try { 1199 attrs = Files.readAttributes(entry, BasicFileAttributes.class); 1200 } catch (NoSuchFileException ignore) { 1201 return; 1202 } catch (IOException ioe) { 1203 ostream.println(entry + " " + ioe); 1204 errorFound = true; 1205 return; 1206 } 1207 1208 String fn = entry.getFileName().toString(); 1209 if (attrs.isRegularFile() && fn.endsWith(".jar")) { 1210 // JAR file, explicit or automatic module 1211 scanModule(entry).ifPresent(this::process); 1212 } else if (attrs.isDirectory()) { 1213 Path mi = entry.resolve(MODULE_INFO); 1214 if (Files.exists(mi)) { 1215 // exploded module 1216 scanModule(entry).ifPresent(this::process); 1217 } else { 1218 // directory of modules 1219 scanDirectory(entry); 1220 } 1221 } 1222 } 1223 1224 /** 1225 * Scan the JAR files and exploded modules in a directory. 1226 */ 1227 private void scanDirectory(Path dir) { 1228 try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) { 1229 Map<String, Path> moduleToEntry = new HashMap<>(); 1230 1231 for (Path entry : stream) { 1232 BasicFileAttributes attrs; 1233 try { 1234 attrs = Files.readAttributes(entry, BasicFileAttributes.class); 1235 } catch (IOException ioe) { 1236 ostream.println(entry + " " + ioe); 1237 errorFound = true; 1238 continue; 1239 } 1240 1241 ModuleReference mref = null; 1242 1243 String fn = entry.getFileName().toString(); 1244 if (attrs.isRegularFile() && fn.endsWith(".jar")) { 1245 mref = scanModule(entry).orElse(null); 1246 } else if (attrs.isDirectory()) { 1247 Path mi = entry.resolve(MODULE_INFO); 1248 if (Files.exists(mi)) { 1249 mref = scanModule(entry).orElse(null); 1250 } 1251 } 1252 1253 if (mref != null) { 1254 String name = mref.descriptor().name(); 1255 Path previous = moduleToEntry.putIfAbsent(name, entry); 1256 if (previous != null) { 1257 // same name as other module in the directory 1258 printModule(mref); 1259 ostream.println(INDENT + "contains same module as " 1260 + previous.getFileName()); 1261 errorFound = true; 1262 } else { 1263 process(mref); 1264 } 1265 } 1266 } 1267 } catch (IOException ioe) { 1268 ostream.println(dir + " " + ioe); 1269 errorFound = true; 1270 } 1271 } 1272 1273 /** 1274 * Scan a JAR file or exploded module. 1275 */ 1276 private Optional<ModuleReference> scanModule(Path entry) { 1277 ModuleFinder finder = ModuleFinder.of(entry); 1278 try { 1279 return finder.findAll().stream().findFirst(); 1280 } catch (FindException e) { 1281 ostream.println(entry); 1282 ostream.println(INDENT + e.getMessage()); 1283 Throwable cause = e.getCause(); 1284 if (cause != null) { 1285 ostream.println(INDENT + cause); 1286 } 1287 errorFound = true; 1288 return Optional.empty(); 1289 } 1290 } 1291 } 1292 }