8075550: Error "JavaFX runtime not found" in nashorn when load predefines scripts to import JavaFX packages
authorjlaskey
Mon, 11 Apr 2016 10:01:39 -0300
changeset 36980 2ae529a820e1
parent 36979 f431c83343d7
child 36981 c8671481e814
8075550: Error "JavaFX runtime not found" in nashorn when load predefines scripts to import JavaFX packages Reviewed-by: kcr, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/base.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/bootstrap.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/controls.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/fxml.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/graphics.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/media.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/swing.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/swt.js
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/web.js
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/base.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/base.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,135 +23,92 @@
  * questions.
  */
 
-var JFX_BASE_CLASSES     = [];
-var JFX_GRAPHICS_CLASSES = [];
-var JFX_CONTROLS_CLASSES = [];
-var JFX_FXML_CLASSES     = [];
-var JFX_WEB_CLASSES      = [];
-var JFX_MEDIA_CLASSES    = [];
-var JFX_SWING_CLASSES    = [];
-var JFX_SWT_CLASSES      = [];
+var JFX_CLASSES = {
+    "javafx.base": [],
+    "javafx.controls": [],
+    "javafx.deploy": [],
+    "javafx.fxml": [],
+    "javafx.graphics": [],
+    "javafx.media": [],
+    "javafx.swing": [],
+    "javafx.web": []
+};
 
-function LOAD_FX_CLASSES(clsList) {
-    for each (var cls in clsList) {
-        // Ex. Stage = Java.type("javafx.stage.Stage");
-        this[cls[cls.length - 1]] = Java.type(cls.join("."));
+function LOAD_FX_CLASSES(global, module) {
+    if (JFX_CLASSES[module]) {
+        for each (var cls in JFX_CLASSES[module]) {
+            // Ex. Stage = Java.type("javafx.stage.Stage");
+            var name = cls.join(".");
+            var type = Java.type(name);
+            global[cls[cls.length - 1]] = type;
+        }
+
+        JFX_CLASSES[module] = undefined;
     }
 }
 
 (function() {
-    var System           = Java.type("java.lang.System");
-    var ZipFile          = Java.type("java.util.zip.ZipFile");
-
-    var SUFFIX_LENGTH    = ".class".length;
-
-    // TODO - temporary patch until fx is moved to module system.
-    // <patch>
-    var jfxrtJar;
-    try {
-        jfxrtJar = new ZipFile(System.getProperty("java.home") + "/lib/jfxrt.jar");
-    } catch (ex1) {
-        try {
-            jfxrtJar = new ZipFile(System.getProperty("java.home") + "/lib/ext/jfxrt.jar");
-        } catch (ex2) {
-            throw new Error("JavaFX runtime not found");
-        }
-    }
-    // </patch>
-
-    var entries = jfxrtJar.entries();
-
-    while (entries.hasMoreElements()) {
-        var entry = entries.nextElement();
-
-        if (entry.isDirectory()) {
-            continue;
-        }
+    var Files = Java.type("java.nio.file.Files");
+    var FileSystems = Java.type("java.nio.file.FileSystems");
+    var FileVisitor = Java.type("java.nio.file.FileVisitor");
+    var FileVisitResult = Java.type("java.nio.file.FileVisitResult");
+    var CONTINUE = FileVisitResult.CONTINUE;
+    var SKIP_SUBTREE = FileVisitResult.SKIP_SUBTREE;
 
-        var name = entry.name;
-
-        if (!name.endsWith(".class")) {
-            continue;
-        }
-
-        name = name.substring(0, name.length - SUFFIX_LENGTH);
-        cls = name.split("/");
-
-        if (cls[0] != "javafx") {
-            continue;
-        }
+    var URI = Java.type("java.net.URI");
+    var uri = new URI("jrt:/");
+    var jrtfs = FileSystems.getFileSystem(uri);
+    var rootDirectories = jrtfs.getRootDirectories();
 
-        var last = cls[cls.length - 1];
-        var nested = last.lastIndexOf("$");
+    var JRTFSWalker = Java.extend(FileVisitor, {
+        preVisitDirectory: function(path, attrs) {
+            var name = path.toString();
 
-        // If class name ends with $nnn
-        if (nested != -1 && !(last.substring(nested) - 0)) {
-            continue;
-        }
+            if (name.startsWith("/packages")) {
+                 return SKIP_SUBTREE;
+            }
 
-        switch (cls[1]) {
-        case "stage":
-            if (cls[2] == "Stage") {
-                JFX_BASE_CLASSES.push(cls);
-            } else {
-                JFX_GRAPHICS_CLASSES.push(cls);
+            if (name.startsWith("/modules") && !name.equals("/modules") && !name.startsWith("/modules/javafx")) {
+                return SKIP_SUBTREE;
             }
-            break;
 
-        case "scene":
-            switch (cls[2]) {
-            case "Scene":
-            case "Group":
-                JFX_BASE_CLASSES.push(cls);
-                break;
-
-            case "chart":
-            case "control":
-                JFX_CONTROLS_CLASSES.push(cls);
-                break;
+            return CONTINUE;
+        },
 
-            case "web":
-                JFX_WEB_CLASSES.push(cls);
-                break;
+        postVisitDirectory: function(path, attrs) {
+            return CONTINUE;
+        },
 
-            case "media":
-                JFX_MEDIA_CLASSES.push(cls);
-                break;
+        visitFile: function(file, attrs) {
+            var name = file.toString();
 
-            default:
-                JFX_GRAPHICS_CLASSES.push(cls);
-                break;
+            if (!name.endsWith(".class") || name.endsWith("module-info.class")) {
+                return CONTINUE;
             }
-            break;
 
-        case "beans":
-        case "collections":
-        case "events":
-        case "util":
-            JFX_BASE_CLASSES.push(cls);
-            break;
+            var parts = name.split("/");
+            parts = parts.slice(2);
+            var module = parts.shift();
+            var path = parts;
+            var cls = path.pop();
+            cls = cls.substring(0, cls.length() - 6);
+            path.push(cls);
+
+            if (path[0] !== "javafx" || /\$\d+$/.test(cls)) {
+                return CONTINUE;
+            }
 
-        case "animation":
-        case "application":
-        case "concurrent":
-        case "css":
-        case "geometry":
-            JFX_GRAPHICS_CLASSES.push(cls);
-            break;
+            JFX_CLASSES[module].push(path);
+
+            return CONTINUE;
+        },
 
-        case "fxml":
-            JFX_FXML_CLASSES.push(cls);
-            break;
+        visitFileFailed: function(file, ex) {
+            return CONTINUE;
+        }
+    });
 
-        case "embed":
-            if (cls[2] == "swing") {
-                JFX_SWING_CLASSES.push(cls);
-            } else {
-                JFX_SWT_CLASSES.push(cls);
-            }
-            break;
-        }
-    }
+    Files.walkFileTree(rootDirectories[0], new JRTFSWalker());
 })();
 
-LOAD_FX_CLASSES(JFX_BASE_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.base");
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/bootstrap.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/bootstrap.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,7 @@
 }
 
 // Extend the javafx.application.Application class overriding init, start and stop.
-com.sun.javafx.application.LauncherImpl.launchApplication((Java.extend(javafx.application.Application, {
+javafx.application.Application.launch((Java.extend(javafx.application.Application, {
     // Overridden javafx.application.Application.init();
     init: function() {
         // Java FX packages and classes must be defined here because
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/controls.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/controls.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_CONTROLS_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.controls");
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/fxml.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/fxml.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_FXML_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.fxml");
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/graphics.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/graphics.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,10 +23,10 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_GRAPHICS_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.graphics");
 
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/media.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/media.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_MEDIA_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.media");
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/swing.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/swing.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_SWING_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.swing");
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/swt.js	Thu Apr 07 10:07:10 2016 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-if (!this.JFX_BASE_CLASSES) {
-    load("fx:base.js")
-}
-
-LOAD_FX_CLASSES(JFX_SWT_CLASSES);
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/web.js	Thu Apr 07 10:07:10 2016 -0700
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/resources/fx/web.js	Mon Apr 11 10:01:39 2016 -0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,8 +23,8 @@
  * questions.
  */
 
-if (!this.JFX_BASE_CLASSES) {
+if (!this.JFX_CLASSES) {
     load("fx:base.js")
 }
 
-LOAD_FX_CLASSES(JFX_WEB_CLASSES);
+LOAD_FX_CLASSES(this, "javafx.web");