Domanda

I've the same issue like here Not able to resolve JSObject in a java applet project :

  • JSObject is taken from the jfxrt.jar (JavaFX) inside the jdk instead from the plugin.jar, so there is no JSObject.getWindow method and compilation fails.

Problem here is that I've a build with java 8 and maven so I can't remove jfxrt.jar from the build path and it seems that the order of JDK and maven dependencies can't be changed.

So is there a way to exclude JavaFX somehow or is there an alternative to JSObject.getWindow to call some JavaScript from the hosting website?

È stato utile?

Soluzione

Finally found a solution (mainly taken from http://apache-geronimo.328035.n3.nabble.com/Maven-compiler-endorsed-libraries-tp693448p702566.html):

  • The java compiler has an option "endorseddirs" to overwrite the bootstrap classes
  • If you place the plugin.jar inside one of these folders it overwrites the JSObject from JavaFX
  • In your maven poms you have to tell the compiler plugin which folders to search and you can use the dependency plugin to copy dependencies/artifacts to that folders

Compiler plugin:

<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
  <compilerArguments>
   <endorseddirs>${project.build.directory}/endorsed</endorseddirs>
  </compilerArguments>
 </configuration>
</plugin>

(If you have the plugin.jar as a maven dependency:)

<plugin>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
   <execution>
    <id>copy-endorsed-dependencies</id>
    <phase>generate-sources</phase>
    <goals>
     <goal>copy-dependencies</goal>
    </goals>
    <configuration>
     <includeArtifactIds>plugin</includeArtifactIds>
     <outputDirectory>${project.build.directory}/endorsed</outputDirectory>
    </configuration>
   </execution>
    ...
 </plugin>

Altri suggerimenti

Another option, which worked for me, is to include plugin.jar and call getWindow by reflection.

public JSObject getJSObject() {
    try {
        Method m = JSObject.class.getMethod("getWindow", Applet.class);
        return (JSObject)m.invoke(null, Applet.this);
    }
    catch (Exception e) {
        // do something
        return null;
    }
}

Changing the order of dependencies isn't required.

EDIT: It was working when running in Eclipse but had a NoSuchMethodException when running in the browser.

But I found another option :

Include plugin.jar, don't change the order. Then, include this method in a class.

public static JSObject getWindow(Applet applet) {
    if (applet != null) {
        AppletContext context = applet.getAppletContext();

        if (context instanceof JSContext) {
            JSContext jsContext = (JSContext)context;
            JSObject jsObject = jsContext.getJSObject();

            if (jsObject != null) {
                return jsObject;
            }
        }
    }

    throw new JSException();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top