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