MrJman006 MrJman006 - 18 days ago 4x
Java Question

Javafx WebView can not access a member of class ... with modifiers "public"

In a project I am working on, I need to define an abstract class with some methods implemented in packageone. In packagetwo the remainder of the abstract methods will be implemented when the class is instantiated. The instance is then exposed to Javascript code in a JavaFX webview. (An online example from Oracle -

My problem is, the methods implemented in packagetwo are not callable from the Javascript environment. I get the following error:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class packagetwo.MainWindow$1 with modifiers "public"
at com.sun.webkit.dom.JSObject.fwkMakeException(
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(
at javafx.scene.web.WebEngine.executeScript(
at packagetwo.MainWindow.makeUpCall(
at packagetwo.MainWindow.access$000(
at packagetwo.MainWindow$2.changed(
at packagetwo.MainWindow$2.changed(
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(
at javafx.scene.web.WebEngine$LoadWorker.updateState(
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(
at javafx.scene.web.WebEngine$LoadWorker.access$1200(
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(
at com.sun.webkit.WebPage.fireLoadEvent(
at com.sun.webkit.WebPage.fwkFireLoadEvent(
at Method)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(
at Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(
at Method)
Caused by: java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class packagetwo.MainWindow$1 with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(
at java.lang.reflect.AccessibleObject.checkAccess(
at java.lang.reflect.Method.invoke(
at sun.reflect.misc.Trampoline.invoke(
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at sun.reflect.misc.MethodUtil.invoke(
at com.sun.webkit.Utilities.lambda$fwkInvokeWithContext$55(
at Method)
at com.sun.webkit.Utilities.fwkInvokeWithContext(
... 29 more

Though calling methods implemented in packageone works fine. Calling both sets of methods from Java also works fine. Below are My classes and Javascript calls.


package packagetwo;

import packageone.Calls;

import java.util.ResourceBundle;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;

public class MainWindow implements Initializable

private BorderPane mainWindow;

private WebView mainWebView;

private AnchorPane mainWebViewLayout;

public void initialize(URL url, ResourceBundle rb)
Calls jc = new Calls()
public void aMethod1()
System.out.printf("Calls: Abstract Method Impl 1 Called.\n");

// Add new load complete listener.
ChangeListener<Worker.State> onLoadComplete = buildOnLoadCompleteListener(mainWebView, jc);

// Attempt to load the current web package.
mainWebView.getEngine().load("file:///" + new File("index.html").getAbsolutePath());


private ChangeListener<Worker.State> buildOnLoadCompleteListener(
WebView webview, Calls javaUpCalls)
return new ChangeListener<Worker.State>()
public void changed(
ObservableValue<? extends Worker.State> ov,
Worker.State lastState, Worker.State currentState)
if (currentState == Worker.State.SUCCEEDED)
// Hook up Java up calls in the Javascript environment.
JSObject jsEnvironment = (JSObject) webview.getEngine().executeScript("window");
jsEnvironment.setMember("JavaUpCalls", javaUpCalls);


private void makeUpCall(WebView webview)
String setUpUpCall =
"function makeAnUpCall()" +
"{" +
"if(typeof JavaUpCalls !== \"undefined\")" +
"{" +
"JavaUpCalls.method2();" +
"JavaUpCalls.aMethod1();" +
"}" +



package packageone;

public abstract class Calls()
public abstract void aMethod1();
public void method2()
System.out.printf("Calls: Method 1 Called.\n");

Index.html is just a standard html page that load successfully with a
tag that says the page loaded.

Any idea why the Javascript environment can't call my methods implemented in packagetwo?


I researched a bit online and found this StackOverflow question:

I attempted the solution listed there (calling setAccessible(true) on the packagetwo method instance) and the error was still thrown.

I also tried initializing and calling the Javascript methods from packageone, but the still got the error.


The anonymous implementation of Calls isn't available as a public class, so the javascript engine can't get to it.

If you create a public implementation in packagetwo, even as an inner class within MainWindow and then create an instance of that to pass through it will work as you expect.