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