1 /* 2 * Copyright (c) 2007, 2013, 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.reflect.Method; 47 import java.lang.reflect.Modifier; 48 import java.math.BigDecimal; 49 import java.math.RoundingMode; 50 import java.nio.charset.Charset; 51 import java.nio.file.DirectoryStream; 52 import java.nio.file.Files; 53 import java.nio.file.Path; 54 import java.text.Normalizer; 55 import java.util.ResourceBundle; 56 import java.text.MessageFormat; 57 import java.util.ArrayList; 58 import java.util.Collections; 59 import java.util.Iterator; 60 import java.util.List; 61 import java.util.Locale; 62 import java.util.Locale.Category; 63 import java.util.Properties; 64 import java.util.Set; 65 import java.util.TreeSet; 66 import java.util.jar.Attributes; 67 import java.util.jar.JarFile; 68 import java.util.jar.Manifest; 69 70 public enum LauncherHelper { 71 INSTANCE; 72 73 // used to identify JavaFX applications 74 private static final String JAVAFX_APPLICATION_MARKER = 75 "JavaFX-Application-Class"; 76 private static final String JAVAFX_APPLICATION_CLASS_NAME = 77 "javafx.application.Application"; 78 private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX = 79 "sun.launcher.LauncherHelper$FXHelper"; 80 private static final String MAIN_CLASS = "Main-Class"; 81 82 private static StringBuilder outBuf = new StringBuilder(); 83 84 private static final String INDENT = " "; 85 private static final String VM_SETTINGS = "VM settings:"; 86 private static final String PROP_SETTINGS = "Property settings:"; 87 private static final String LOCALE_SETTINGS = "Locale settings:"; 88 89 // sync with java.c and sun.misc.VM 90 private static final String diagprop = "sun.java.launcher.diag"; 91 final static boolean trace = sun.misc.VM.getSavedProperty(diagprop) != null; 92 93 private static final String defaultBundleName = 94 "sun.launcher.resources.launcher"; 95 private static class ResourceBundleHolder { 96 private static final ResourceBundle RB = 97 ResourceBundle.getBundle(defaultBundleName); 98 } 99 private static PrintStream ostream; 100 private static final ClassLoader scloader = ClassLoader.getSystemClassLoader(); 101 private static Class<?> appClass; // application class, for GUI/reporting purposes 102 103 /* 104 * A method called by the launcher to print out the standard settings, 105 * by default -XshowSettings is equivalent to -XshowSettings:all, 106 * Specific information may be gotten by using suboptions with possible 107 * values vm, properties and locale. 108 * 109 * printToStderr: choose between stdout and stderr 110 * 111 * optionFlag: specifies which options to print default is all other 112 * possible values are vm, properties, locale. 113 * 114 * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates 115 * this code should determine this value, using a suitable method or 116 * the line could be omitted. 117 * 118 * maxHeapSize: in bytes, as set by the launcher, a zero-value indicates 119 * this code should determine this value, using a suitable method. 120 * 121 * stackSize: in bytes, as set by the launcher, a zero-value indicates 122 * this code determine this value, using a suitable method or omit the 123 * line entirely. 124 */ 125 static void showSettings(boolean printToStderr, String optionFlag, 126 long initialHeapSize, long maxHeapSize, long stackSize, 127 boolean isServer) { 128 129 initOutput(printToStderr); 130 String opts[] = optionFlag.split(":"); 131 String optStr = (opts.length > 1 && opts[1] != null) 132 ? opts[1].trim() 133 : "all"; 134 switch (optStr) { 135 case "vm": 136 printVmSettings(initialHeapSize, maxHeapSize, 137 stackSize, isServer); 138 break; 139 case "properties": 140 printProperties(); 141 break; 142 case "locale": 143 printLocale(); 144 break; 145 default: 146 printVmSettings(initialHeapSize, maxHeapSize, stackSize, 147 isServer); 148 printProperties(); 149 printLocale(); 150 break; 151 } 152 } 153 154 /* 155 * prints the main vm settings subopt/section 156 */ 157 private static void printVmSettings( 158 long initialHeapSize, long maxHeapSize, 159 long stackSize, boolean isServer) { 160 161 ostream.println(VM_SETTINGS); 162 if (stackSize != 0L) { 163 ostream.println(INDENT + "Stack Size: " + 164 SizePrefix.scaleValue(stackSize)); 165 } 166 if (initialHeapSize != 0L) { 167 ostream.println(INDENT + "Min. Heap Size: " + 168 SizePrefix.scaleValue(initialHeapSize)); 169 } 170 if (maxHeapSize != 0L) { 171 ostream.println(INDENT + "Max. Heap Size: " + 172 SizePrefix.scaleValue(maxHeapSize)); 173 } else { 174 ostream.println(INDENT + "Max. Heap Size (Estimated): " 175 + SizePrefix.scaleValue(Runtime.getRuntime().maxMemory())); 176 } 177 ostream.println(INDENT + "Ergonomics Machine Class: " 178 + ((isServer) ? "server" : "client")); 179 ostream.println(INDENT + "Using VM: " 180 + System.getProperty("java.vm.name")); 181 ostream.println(); 182 } 183 184 /* 185 * prints the properties subopt/section 186 */ 187 private static void printProperties() { 188 Properties p = System.getProperties(); 189 ostream.println(PROP_SETTINGS); 190 List<String> sortedPropertyKeys = new ArrayList<>(); 191 sortedPropertyKeys.addAll(p.stringPropertyNames()); 192 Collections.sort(sortedPropertyKeys); 193 for (String x : sortedPropertyKeys) { 194 printPropertyValue(x, p.getProperty(x)); 195 } 196 ostream.println(); 197 } 198 199 private static boolean isPath(String key) { 200 return key.endsWith(".dirs") || key.endsWith(".path"); 201 } 202 203 private static void printPropertyValue(String key, String value) { 204 ostream.print(INDENT + key + " = "); 205 if (key.equals("line.separator")) { 206 for (byte b : value.getBytes()) { 207 switch (b) { 208 case 0xd: 209 ostream.print("\\r "); 210 break; 211 case 0xa: 212 ostream.print("\\n "); 213 break; 214 default: 215 // print any bizzare line separators in hex, but really 216 // shouldn't happen. 217 ostream.printf("0x%02X", b & 0xff); 218 break; 219 } 220 } 221 ostream.println(); 222 return; 223 } 224 if (!isPath(key)) { 225 ostream.println(value); 226 return; 227 } 228 String[] values = value.split(System.getProperty("path.separator")); 229 boolean first = true; 230 for (String s : values) { 231 if (first) { // first line treated specially 232 ostream.println(s); 233 first = false; 234 } else { // following lines prefix with indents 235 ostream.println(INDENT + INDENT + s); 236 } 237 } 238 } 239 240 /* 241 * prints the locale subopt/section 242 */ 243 private static void printLocale() { 244 Locale locale = Locale.getDefault(); 245 ostream.println(LOCALE_SETTINGS); 246 ostream.println(INDENT + "default locale = " + 247 locale.getDisplayLanguage()); 248 ostream.println(INDENT + "default display locale = " + 249 Locale.getDefault(Category.DISPLAY).getDisplayName()); 250 ostream.println(INDENT + "default format locale = " + 251 Locale.getDefault(Category.FORMAT).getDisplayName()); 252 printLocales(); 253 ostream.println(); 254 } 255 256 private static void printLocales() { 257 Locale[] tlocales = Locale.getAvailableLocales(); 258 final int len = tlocales == null ? 0 : tlocales.length; 259 if (len < 1 ) { 260 return; 261 } 262 // Locale does not implement Comparable so we convert it to String 263 // and sort it for pretty printing. 264 Set<String> sortedSet = new TreeSet<>(); 265 for (Locale l : tlocales) { 266 sortedSet.add(l.toString()); 267 } 268 269 ostream.print(INDENT + "available locales = "); 270 Iterator<String> iter = sortedSet.iterator(); 271 final int last = len - 1; 272 for (int i = 0 ; iter.hasNext() ; i++) { 273 String s = iter.next(); 274 ostream.print(s); 275 if (i != last) { 276 ostream.print(", "); 277 } 278 // print columns of 8 279 if ((i + 1) % 8 == 0) { 280 ostream.println(); 281 ostream.print(INDENT + INDENT); 282 } 283 } 284 } 285 286 private enum SizePrefix { 287 288 KILO(1024, "K"), 289 MEGA(1024 * 1024, "M"), 290 GIGA(1024 * 1024 * 1024, "G"), 291 TERA(1024L * 1024L * 1024L * 1024L, "T"); 292 long size; 293 String abbrev; 294 295 SizePrefix(long size, String abbrev) { 296 this.size = size; 297 this.abbrev = abbrev; 298 } 299 300 private static String scale(long v, SizePrefix prefix) { 301 return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size), 302 2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev; 303 } 304 /* 305 * scale the incoming values to a human readable form, represented as 306 * K, M, G and T, see java.c parse_size for the scaled values and 307 * suffixes. The lowest possible scaled value is Kilo. 308 */ 309 static String scaleValue(long v) { 310 if (v < MEGA.size) { 311 return scale(v, KILO); 312 } else if (v < GIGA.size) { 313 return scale(v, MEGA); 314 } else if (v < TERA.size) { 315 return scale(v, GIGA); 316 } else { 317 return scale(v, TERA); 318 } 319 } 320 } 321 322 /** 323 * A private helper method to get a localized message and also 324 * apply any arguments that we might pass. 325 */ 326 private static String getLocalizedMessage(String key, Object... args) { 327 String msg = ResourceBundleHolder.RB.getString(key); 328 return (args != null) ? MessageFormat.format(msg, args) : msg; 329 } 330 331 /** 332 * The java -help message is split into 3 parts, an invariant, followed 333 * by a set of platform dependent variant messages, finally an invariant 334 * set of lines. 335 * This method initializes the help message for the first time, and also 336 * assembles the invariant header part of the message. 337 */ 338 static void initHelpMessage(String progname) { 339 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.header", 340 (progname == null) ? "java" : progname )); 341 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", 342 32)); 343 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.datamodel", 344 64)); 345 } 346 347 /** 348 * Appends the vm selection messages to the header, already created. 349 * initHelpSystem must already be called. 350 */ 351 static void appendVmSelectMessage(String vm1, String vm2) { 352 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.vmselect", 353 vm1, vm2)); 354 } 355 356 /** 357 * Appends the vm synoym message to the header, already created. 358 * initHelpSystem must be called before using this method. 359 */ 360 static void appendVmSynonymMessage(String vm1, String vm2) { 361 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.hotspot", 362 vm1, vm2)); 363 } 364 365 /** 366 * Appends the vm Ergo message to the header, already created. 367 * initHelpSystem must be called before using this method. 368 */ 369 static void appendVmErgoMessage(boolean isServerClass, String vm) { 370 outBuf = outBuf.append(getLocalizedMessage("java.launcher.ergo.message1", 371 vm)); 372 outBuf = (isServerClass) 373 ? outBuf.append(",\n" + 374 getLocalizedMessage("java.launcher.ergo.message2") + "\n\n") 375 : outBuf.append(".\n\n"); 376 } 377 378 /** 379 * Appends the last invariant part to the previously created messages, 380 * and finishes up the printing to the desired output stream. 381 * initHelpSystem must be called before using this method. 382 */ 383 static void printHelpMessage(boolean printToStderr) { 384 initOutput(printToStderr); 385 outBuf = outBuf.append(getLocalizedMessage("java.launcher.opt.footer", 386 File.pathSeparator)); 387 ostream.println(outBuf.toString()); 388 } 389 390 /** 391 * Prints the Xusage text to the desired output stream. 392 */ 393 static void printXUsageMessage(boolean printToStderr) { 394 initOutput(printToStderr); 395 ostream.println(getLocalizedMessage("java.launcher.X.usage", 396 File.pathSeparator)); 397 if (System.getProperty("os.name").contains("OS X")) { 398 ostream.println(getLocalizedMessage("java.launcher.X.macosx.usage", 399 File.pathSeparator)); 400 } 401 } 402 403 static void initOutput(boolean printToStderr) { 404 ostream = (printToStderr) ? System.err : System.out; 405 } 406 407 static String getMainClassFromJar(String jarname) { 408 String mainValue = null; 409 try (JarFile jarFile = new JarFile(jarname)) { 410 Manifest manifest = jarFile.getManifest(); 411 if (manifest == null) { 412 abort(null, "java.launcher.jar.error2", jarname); 413 } 414 Attributes mainAttrs = manifest.getMainAttributes(); 415 if (mainAttrs == null) { 416 abort(null, "java.launcher.jar.error3", jarname); 417 } 418 mainValue = mainAttrs.getValue(MAIN_CLASS); 419 if (mainValue == null) { 420 abort(null, "java.launcher.jar.error3", jarname); 421 } 422 423 /* 424 * Hand off to FXHelper if it detects a JavaFX application 425 * This must be done after ensuring a Main-Class entry 426 * exists to enforce compliance with the jar specification 427 */ 428 if (mainAttrs.containsKey( 429 new Attributes.Name(JAVAFX_APPLICATION_MARKER))) { 430 FXHelper.setFXLaunchParameters(jarname, LM_JAR); 431 return FXHelper.class.getName(); 432 } 433 434 return mainValue.trim(); 435 } catch (IOException ioe) { 436 abort(ioe, "java.launcher.jar.error1", jarname); 437 } 438 return null; 439 } 440 441 // From src/share/bin/java.c: 442 // enum LaunchMode { LM_UNKNOWN = 0, LM_CLASS, LM_JAR }; 443 444 private static final int LM_UNKNOWN = 0; 445 private static final int LM_CLASS = 1; 446 private static final int LM_JAR = 2; 447 448 static void abort(Throwable t, String msgKey, Object... args) { 449 if (msgKey != null) { 450 ostream.println(getLocalizedMessage(msgKey, args)); 451 } 452 if (trace) { 453 if (t != null) { 454 t.printStackTrace(); 455 } else { 456 Thread.dumpStack(); 457 } 458 } 459 System.exit(1); 460 } 461 462 /** 463 * This method does the following: 464 * 1. gets the classname from a Jar's manifest, if necessary 465 * 2. loads the class using the System ClassLoader 466 * 3. ensures the availability and accessibility of the main method, 467 * using signatureDiagnostic method. 468 * a. does the class exist 469 * b. is there a main 470 * c. is the main public 471 * d. is the main static 472 * e. does the main take a String array for args 473 * 4. if no main method and if the class extends FX Application, then call 474 * on FXHelper to determine the main class to launch 475 * 5. and off we go...... 476 * 477 * @param printToStderr if set, all output will be routed to stderr 478 * @param mode LaunchMode as determined by the arguments passed on the 479 * command line 480 * @param what either the jar file to launch or the main class when using 481 * LM_CLASS mode 482 * @return the application's main class 483 */ 484 public static Class<?> checkAndLoadMain(boolean printToStderr, 485 int mode, 486 String what) { 487 initOutput(printToStderr); 488 // get the class name 489 String cn = null; 490 switch (mode) { 491 case LM_CLASS: 492 cn = what; 493 break; 494 case LM_JAR: 495 cn = getMainClassFromJar(what); 496 break; 497 default: 498 // should never happen 499 throw new InternalError("" + mode + ": Unknown launch mode"); 500 } 501 cn = cn.replace('/', '.'); 502 Class<?> mainClass = null; 503 try { 504 mainClass = scloader.loadClass(cn); 505 } catch (NoClassDefFoundError | ClassNotFoundException cnfe) { 506 if (System.getProperty("os.name", "").contains("OS X") 507 && Normalizer.isNormalized(cn, Normalizer.Form.NFD)) { 508 try { 509 // On Mac OS X since all names with diacretic symbols are given as decomposed it 510 // is possible that main class name comes incorrectly from the command line 511 // and we have to re-compose it 512 mainClass = scloader.loadClass(Normalizer.normalize(cn, Normalizer.Form.NFC)); 513 } catch (NoClassDefFoundError | ClassNotFoundException cnfe1) { 514 abort(cnfe, "java.launcher.cls.error1", cn); 515 } 516 } else { 517 abort(cnfe, "java.launcher.cls.error1", cn); 518 } 519 } 520 // set to mainClass 521 appClass = mainClass; 522 523 /* 524 * Check if FXHelper can launch it using the FX launcher. In an FX app, 525 * the main class may or may not have a main method, so do this before 526 * validating the main class. 527 */ 528 if (JAVAFX_FXHELPER_CLASS_NAME_SUFFIX.equals(mainClass.getName()) || 529 doesExtendFXApplication(mainClass)) { 530 // Will abort() if there are problems with FX runtime 531 FXHelper.setFXLaunchParameters(what, mode); 532 return FXHelper.class; 533 } 534 535 validateMainClass(mainClass); 536 return mainClass; 537 } 538 539 /* 540 * Accessor method called by the launcher after getting the main class via 541 * checkAndLoadMain(). The "application class" is the class that is finally 542 * executed to start the application and in this case is used to report 543 * the correct application name, typically for UI purposes. 544 */ 545 public static Class<?> getApplicationClass() { 546 return appClass; 547 } 548 549 /* 550 * Check if the given class is a JavaFX Application class. This is done 551 * in a way that does not cause the Application class to load or throw 552 * ClassNotFoundException if the JavaFX runtime is not available. 553 */ 554 private static boolean doesExtendFXApplication(Class<?> mainClass) { 555 for (Class<?> sc = mainClass.getSuperclass(); sc != null; 556 sc = sc.getSuperclass()) { 557 if (sc.getName().equals(JAVAFX_APPLICATION_CLASS_NAME)) { 558 return true; 559 } 560 } 561 return false; 562 } 563 564 // Check the existence and signature of main and abort if incorrect 565 static void validateMainClass(Class<?> mainClass) { 566 Method mainMethod; 567 try { 568 mainMethod = mainClass.getMethod("main", String[].class); 569 } catch (NoSuchMethodException nsme) { 570 // invalid main or not FX application, abort with an error 571 abort(null, "java.launcher.cls.error4", mainClass.getName(), 572 JAVAFX_APPLICATION_CLASS_NAME); 573 return; // Avoid compiler issues 574 } 575 576 /* 577 * getMethod (above) will choose the correct method, based 578 * on its name and parameter type, however, we still have to 579 * ensure that the method is static and returns a void. 580 */ 581 int mod = mainMethod.getModifiers(); 582 if (!Modifier.isStatic(mod)) { 583 abort(null, "java.launcher.cls.error2", "static", 584 mainMethod.getDeclaringClass().getName()); 585 } 586 if (mainMethod.getReturnType() != java.lang.Void.TYPE) { 587 abort(null, "java.launcher.cls.error3", 588 mainMethod.getDeclaringClass().getName()); 589 } 590 } 591 592 private static final String encprop = "sun.jnu.encoding"; 593 private static String encoding = null; 594 private static boolean isCharsetSupported = false; 595 596 /* 597 * converts a c or a byte array to a platform specific string, 598 * previously implemented as a native method in the launcher. 599 */ 600 static String makePlatformString(boolean printToStderr, byte[] inArray) { 601 initOutput(printToStderr); 602 if (encoding == null) { 603 encoding = System.getProperty(encprop); 604 isCharsetSupported = Charset.isSupported(encoding); 605 } 606 try { 607 String out = isCharsetSupported 608 ? new String(inArray, encoding) 609 : new String(inArray); 610 return out; 611 } catch (UnsupportedEncodingException uee) { 612 abort(uee, null); 613 } 614 return null; // keep the compiler happy 615 } 616 617 static String[] expandArgs(String[] argArray) { 618 List<StdArg> aList = new ArrayList<>(); 619 for (String x : argArray) { 620 aList.add(new StdArg(x)); 621 } 622 return expandArgs(aList); 623 } 624 625 static String[] expandArgs(List<StdArg> argList) { 626 ArrayList<String> out = new ArrayList<>(); 627 if (trace) { 628 System.err.println("Incoming arguments:"); 629 } 630 for (StdArg a : argList) { 631 if (trace) { 632 System.err.println(a); 633 } 634 if (a.needsExpansion) { 635 File x = new File(a.arg); 636 File parent = x.getParentFile(); 637 String glob = x.getName(); 638 if (parent == null) { 639 parent = new File("."); 640 } 641 try (DirectoryStream<Path> dstream = 642 Files.newDirectoryStream(parent.toPath(), glob)) { 643 int entries = 0; 644 for (Path p : dstream) { 645 out.add(p.normalize().toString()); 646 entries++; 647 } 648 if (entries == 0) { 649 out.add(a.arg); 650 } 651 } catch (Exception e) { 652 out.add(a.arg); 653 if (trace) { 654 System.err.println("Warning: passing argument as-is " + a); 655 System.err.print(e); 656 } 657 } 658 } else { 659 out.add(a.arg); 660 } 661 } 662 String[] oarray = new String[out.size()]; 663 out.toArray(oarray); 664 665 if (trace) { 666 System.err.println("Expanded arguments:"); 667 for (String x : oarray) { 668 System.err.println(x); 669 } 670 } 671 return oarray; 672 } 673 674 /* duplicate of the native StdArg struct */ 675 private static class StdArg { 676 final String arg; 677 final boolean needsExpansion; 678 StdArg(String arg, boolean expand) { 679 this.arg = arg; 680 this.needsExpansion = expand; 681 } 682 // protocol: first char indicates whether expansion is required 683 // 'T' = true ; needs expansion 684 // 'F' = false; needs no expansion 685 StdArg(String in) { 686 this.arg = in.substring(1); 687 needsExpansion = in.charAt(0) == 'T'; 688 } 689 public String toString() { 690 return "StdArg{" + "arg=" + arg + ", needsExpansion=" + needsExpansion + '}'; 691 } 692 } 693 694 static final class FXHelper { 695 696 private static final String JAVAFX_LAUNCHER_CLASS_NAME = 697 "com.sun.javafx.application.LauncherImpl"; 698 699 /* 700 * The launch method used to invoke the JavaFX launcher. These must 701 * match the strings used in the launchApplication method. 702 * 703 * Command line JavaFX-App-Class Launch mode FX Launch mode 704 * java -cp fxapp.jar FXClass N/A LM_CLASS "LM_CLASS" 705 * java -cp somedir FXClass N/A LM_CLASS "LM_CLASS" 706 * java -jar fxapp.jar Present LM_JAR "LM_JAR" 707 * java -jar fxapp.jar Not Present LM_JAR "LM_JAR" 708 */ 709 private static final String JAVAFX_LAUNCH_MODE_CLASS = "LM_CLASS"; 710 private static final String JAVAFX_LAUNCH_MODE_JAR = "LM_JAR"; 711 712 /* 713 * FX application launcher and launch method, so we can launch 714 * applications with no main method. 715 */ 716 private static String fxLaunchName = null; 717 private static String fxLaunchMode = null; 718 719 private static Class<?> fxLauncherClass = null; 720 private static Method fxLauncherMethod = null; 721 722 /* 723 * Set the launch params according to what was passed to LauncherHelper 724 * so we can use the same launch mode for FX. Abort if there is any 725 * issue with loading the FX runtime or with the launcher method. 726 */ 727 private static void setFXLaunchParameters(String what, int mode) { 728 // Check for the FX launcher classes 729 try { 730 fxLauncherClass = scloader.loadClass(JAVAFX_LAUNCHER_CLASS_NAME); 731 /* 732 * signature must be: 733 * public static void launchApplication(String launchName, 734 * String launchMode, String[] args); 735 */ 736 fxLauncherMethod = fxLauncherClass.getMethod("launchApplication", 737 String.class, String.class, String[].class); 738 739 // verify launcher signature as we do when validating the main method 740 int mod = fxLauncherMethod.getModifiers(); 741 if (!Modifier.isStatic(mod)) { 742 abort(null, "java.launcher.javafx.error1"); 743 } 744 if (fxLauncherMethod.getReturnType() != java.lang.Void.TYPE) { 745 abort(null, "java.launcher.javafx.error1"); 746 } 747 } catch (ClassNotFoundException | NoSuchMethodException ex) { 748 abort(ex, "java.launcher.cls.error5", ex); 749 } 750 751 fxLaunchName = what; 752 switch (mode) { 753 case LM_CLASS: 754 fxLaunchMode = JAVAFX_LAUNCH_MODE_CLASS; 755 break; 756 case LM_JAR: 757 fxLaunchMode = JAVAFX_LAUNCH_MODE_JAR; 758 break; 759 default: 760 // should not have gotten this far... 761 throw new InternalError(mode + ": Unknown launch mode"); 762 } 763 } 764 765 public static void main(String... args) throws Exception { 766 if (fxLauncherMethod == null 767 || fxLaunchMode == null 768 || fxLaunchName == null) { 769 throw new RuntimeException("Invalid JavaFX launch parameters"); 770 } 771 // launch appClass via fxLauncherMethod 772 fxLauncherMethod.invoke(null, 773 new Object[] {fxLaunchName, fxLaunchMode, args}); 774 } 775 } 776 }