5

Until Java 8u51, the following worked fine:

  • Have an HTML file containing <script src="javascript.js"></script>
  • Load this HTML file into a WebView
  • Call a function contained in javascript.js using webView.getEngine().executeScript()

After switching to 8u60 however, it no longer works:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: TypeError: undefined is not a function

So the file javascript.js is no longer loaded. It works when started from an IDE, where JavaScript file just lies on the file system. However, it no longer works when the application is packaged and therefore javascript.js is inside a JAR file.

Any idea what has changed and how this can be fixed?

Michel Jung
  • 2,966
  • 6
  • 31
  • 51
  • It's not JDK/jre fault, it doesn't find your js file(you may locate it beside the jar and a have a try), I believe you set the target js file path wrong –  Sep 06 '15 at 23:28
  • Why did it work before but not anymore then? – Michel Jung Sep 07 '15 at 05:26
  • Becasue IDE does not generate a jar file then run it, and you are doing that way, you need to specify the javascript file it's a embed jar resource –  Sep 07 '15 at 14:05
  • By "it *no longer works* when the application is packaged" I meant it works with u51 but doesn't with u60 - both run from JAR files. – Michel Jung Sep 07 '15 at 16:11
  • Most likely the problem with the javascript code inside the file. Is it possible for you to post the javascript file here? – kucing_terbang Sep 20 '15 at 14:37
  • 1
    It's not the content. Specifying the file as an URL instead of a relative path works. – Michel Jung Sep 22 '15 at 09:21

1 Answers1

6

Since Java 8u60, local javascript files referenced in html files are not executed anymore in the WebView. When running the program with an older version, it still works. I found no information that this change in behaviour was intended by Oracle.

As a workaround you can either place the javascript code directly into the HTML file, or load in java the contents of the javascript file into a String and execute it with webView.getEngine().executeScript()

gogotox
  • 795
  • 8
  • 14
  • 3
    Yeah I couldn't find any information either. My workaround is having a placeholder {javascript-file}, reading the HTML into a string and then then string-replace the placeholder with `getResource("javascript.js").toURI()` or so. Ugly but at least it works. – Michel Jung Sep 22 '15 at 09:23
  • Actually, this is the solution I also ended up using. It's not pretty, but it works for now. – gogotox Sep 22 '15 at 09:45
  • 1
    Thanks, had same situtaion, a fix is coming in 8u72: https://bugs.openjdk.java.net/browse/JDK-8136466 – grudolf Jan 13 '16 at 09:56